显示带有点的窗口的最简单方法

时间:2013-04-03 03:19:59

标签: java swing animation jframe

我花了最后一小时阅读互联网,了解如何在我的程序中创建超级简单的显示。情况如下:

1。)我有一个运行任意次数的for循环。每次迭代,它产生一个" city",即随机生成的x坐标和y坐标(两个int)。

2.)我想创建SIMPLEST显示。没有什么花哨。也就是说,理想情况下,一个大小为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中是如何工作的......

1 个答案:

答案 0 :(得分:2)

答案取决于你想要达到的目标,但基本上,你需要一些方法将结果绘制到屏幕上。

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

查看Performing Custom Painting

<强>动画

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();
        }
    }
}