代码之家  ›  专栏  ›  技术社区  ›  Maximus

如何在Java中从ArrayList绘制对象

  •  0
  • Maximus  · 技术社区  · 6 年前

    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Random;
    
    import javax.swing.AbstractAction;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Movement extends JFrame {
        public ArrayList<Dot> dots = new ArrayList<Dot>();
        Random rn = new Random();
        DrawPanel drawPanel = new DrawPanel();
        public Movement() {
            for(int i = 0; i < 100; i ++) {
                Dot dot = new Dot(5, 5);
                dots.add(dot);
            }
            ActionListener listener = new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                        for(int i = 0; i < dots.size();i++) {
                            dots.get(i).setX(dots.get(i).getX() + rn.nextInt(20)-10);
                            dots.get(i).setY(dots.get(i).getY() + rn.nextInt(20)-10);
                        }
                        drawPanel.repaint();
    
                }
            };
            Timer timer = new Timer(100, listener);
            timer.start();
            add(drawPanel);
    
            pack();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
            setBounds(100, 100, 500, 500);
        }
    
        private class DrawPanel extends JPanel {
    
            protected void paintComponent(Graphics g) {
                 for(int i = 0; i < dots.size(); i ++) {
                    g.fillRect(dots.get(i).getX(), dots.get(i).getY(), 5, 5);;
                    super.paintComponent(g);
                 }
    
    
            }
    
        }
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Movement();
                }
            });
        }
    }
    

    Dot类别:

    public class Dot {
        private int x, y;
        public Dot(int x, int y) {
            this.x = x;
            this.y = y;
    
        }
        public int getX() {
            return x;
        }
        public void setX(int x) {
            this.x = x;
        }
        public int getY() {
            return y;
        }
        public void setY(int y) {
            this.y = y;
        }
    
    }
    

    非常感谢您的帮助。

    2 回复  |  直到 6 年前
        1
  •  2
  •   K.Nicholas    6 年前

    如果你打电话 super.paintComponent(g); 在您绘制自己的组件之后,您已经擦除了自己的绘制。所以

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g;
             for(int i = 0; i < dots.size(); i ++) {
                 g2d.fillRect(dots.get(i).x, dots.get(i).y, 5, 5);
             }
        }
    

    也,

    // don't repeat type in constructor
    // use built in point instead of custom class
    public ArrayList<Point> dots = new ArrayList<>();
    

        ActionListener listener = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                    for(int i = 0; i < dots.size();i++) {
                        dots.get(i).x = dots.get(i).x + rn.nextInt(20)-10;
                        dots.get(i).y = dots.get(i).y + rn.nextInt(20)-10;
                    }
                    drawPanel.repaint();
            }
        };
    

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Movement mf = new Movement();
            }
        });
    
        2
  •  0
  •   Loduwijk    6 年前

    你好像在打电话 super.paintComponent 每次你画一个点 ,在你油漆它之后。

    所以你继续画一个点,让 画一个清晰的面板,然后画另一个点,然后再次撤销它,再画一个,撤销。。。等等

    呼叫 一次,在你做你的定制画之前先做。