1

我花了最后一个小时在互联网上仔细研究如何在我的程序中创建一个超级简单的显示。情况如下:

1.) 我有一个运行任意次数的 for 循环。每次迭代,它都会产生一个“城市”,即随机生成的 x 坐标和 y 坐标(均为整数)。

2.) 我想创建最简单的显示。没有什么花哨。也就是说,理想情况下,一个大小为 600x600 的窗口会在 for 循环的每次迭代中为每个城市绘制一个黑点。

这是我到目前为止所拥有的:

    f.setPreferredSize(new Dimension(600, 600));
    f.pack();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    for (int i = 0; i <= nodeArray.length-1; i++)
    {
        nodeArray[i] = new Node();
        nodeArray[i].x = 0 + (600 - 0) * r.nextDouble();
        nodeArray[i].y = 0 + (600 - 0) * r.nextDouble();

        //DRAW DOT HERE?
    }

对于这个 for 循环的每次迭代,我想在这个窗口中绘制一个带有 x 坐标 (int)nodeArray[i].x 和 y 坐标 (int)nodeArray[i].y 的黑点

我真的很感激任何帮助。这是一个有点高级的算法课程,我有点尴尬,我似乎无法弄清楚图形在 Java 中是如何工作的......

4

1 回答 1

2

答案取决于您想要实现的目标,但基本上,您需要某种方式将结果绘制到屏幕上。

如果数据不经常更改,最好使用后备缓冲区,因为渲染器速度更快,而不必每次都循环遍历数据集。

查看执行自定义绘画

动画

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Dotty {

    public static void main(String[] args) {
        new Dotty();
    }

    public Dotty() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int count = 0;
        private int dotCount = 1000;
        private BufferedImage background;

        public TestPane() {
            background = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB);

            Timer timer;
            timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    count++;
                    if (count < dotCount) {
                        int x = (int) Math.round((Math.random() * 600));
                        int y = (int) Math.round((Math.random() * 600));
                        Graphics2D g2d = background.createGraphics();
                        g2d.setColor(Color.BLACK);
                        g2d.drawRect(x, y, 1, 1);
                        g2d.dispose();
                        repaint();
                    } else {
                        ((Timer) e.getSource()).stop();
                    }
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - background.getWidth()) / 2;
            int y = (getHeight() - background.getHeight()) / 2;
            g2d.drawImage(background, x, y, this);
            g2d.dispose();
        }
    }
}

直接渲染

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Dotty {

    public static void main(String[] args) {
        new Dotty();
    }

    public Dotty() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int count = 0;
        private int dotCount = 1000;
        private BufferedImage background;

        public TestPane() {
            background = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB);

            for (int count = 0; count < dotCount; count++) {
                int x = (int) Math.round((Math.random() * 600));
                int y = (int) Math.round((Math.random() * 600));
                Graphics2D g2d = background.createGraphics();
                g2d.setColor(Color.BLACK);
                g2d.drawRect(x, y, 1, 1);
                g2d.dispose();
                repaint();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - background.getWidth()) / 2;
            int y = (getHeight() - background.getHeight()) / 2;
            g2d.drawImage(background, x, y, this);
            g2d.dispose();
        }
    }
}
于 2013-04-03T03:33:41.340 回答