如何从Java中的ArrayList绘制对象

时间:2018-11-15 16:32:00

标签: java class object arraylist jframe

好的,所以我试图制作一个程序,该程序绘制一堆随机在屏幕上移动的矩形。我有一个Dot类,其中每个点都包含其x和y值,在我的绘画类中,我随机更改x和y值,然后重新绘制()。我现在所拥有的除了空白JFrame之外不会加载任何其他东西。我怀疑我把每个点画错了。以下是我的代码:

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

点类:

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 个答案:

答案 0 :(得分:2)

如果您在绘制自己的组件后调用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();
        }
    });

答案 1 :(得分:0)

在绘制点后,您似乎正在呼叫FROM python:3.6-slim RUN apt-get update && apt-get install -y vim RUN pip install --install-option="--prefix=/temp" deepdiff CMD cp -pr /temp /volumes/shared/dependencies 。不仅如此,您似乎在循环内调用它,在绘制完点后每次绘制点时都调用它。

因此,您继续画一个点,让super.paintComponent画一个透明的面板,然后画另一个点,然后再次撤消它,再画另一个,撤消...,依此类推。

致电super.paintComponent一次,并在进行自定义绘画之前进行呼叫。