【问题标题】:How to draw objects from an ArrayList in Java如何从 Java 中的 ArrayList 中绘制对象
【发布时间】:2018-11-15 16:32:00
【问题描述】:

好的,所以我正在尝试制作一个程序来绘制一堆在屏幕上随机移动的矩形。我有一个 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;
    }

}

感谢所有帮助。

【问题讨论】:

  • 您的ActionListener 是否触发过?你调试过你的代码吗?

标签: java class object arraylist jframe


【解决方案1】:

如果您在绘制自己的组件后调用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】:

    你似乎在给super.paintComponent打电话你画点之后。不仅如此,您似乎在循环内部调用它,每次绘制点时调用它,在绘制之后调用它。

    所以你一直画一个点,让super.paintComponent 画一个清晰的面板,然后画另一个点,然后再撤消它,再画一个,撤消......等等。

    致电super.paintComponent 一次,然后在进行自定义绘画之前进行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      相关资源
      最近更新 更多