Object.drawItself 在 PaintComponent 中不起作用

Object.drawItself in PaintComponent doesnt work

PaintComponent 绘制图形。什么也没有发生,干净的 Jframe 出现了。 我认为列表或我调用方法的方式有问题 列表在 class 中,Paint Component

public class Paint extends JPanel implements ActionListener {
     List<Figures> figuresList = new ArrayList<Figures>();
     Timer t = new Timer(5, this);

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    for (Figures figure : figuresList) {
        figure.drawItself(g, figure.getLocationX(), figure.getLocationY());
    }
    t.start();
}

@Override
public void actionPerformed(ActionEvent e) {

    {
        for (Figures figure : figuresList) {
            if (figure.getLocationX() < 0 || figure.getLocationX() > 540) {
                figure.setVelocityX(-figure.getVelocityX());
            }
            if (figure.getLocationY() < 0 || figure.getLocationX() > 220) {
                figure.setVelocityY(-figure.getVelocityY());
            }
            figure.setLocationX(figure.getLocationX()
                    + figure.getVelocityX());
            figure.setLocationY(figure.getLocationY()
                    + figure.getVelocityY());
        }
    }
    repaint();

}

然后自己画:

public class Circle implements Figures {    
    public int locationX = 12;
    public int locationY = 12;
    public int velocityX =1;
    public int velocityY =1;


    public void drawItself(Graphics g, int locationX, int locationY){
        this.locationX = locationX;
        this.locationY = locationY;
        g.drawOval(locationX, locationY, 40, 40);  
        g.fillOval(locationX, locationY, 40, 40);
    }

主要:

public static void main(String[] args) {

    Circle c = new Circle();
    Quadrat q = new Quadrat();
    Paint p = new Paint();
    p.figuresList.add(c);
    p.figuresList.add(q);
    GUI.Configuration();


    }

图形界面

public class GUI {


    public static void Configuration(){
        JFrame frame = new JFrame("Figures Animation");
        frame.setSize(600,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new Paint();
        frame.getContentPane().add(BorderLayout.CENTER, panel);
    }

您在此处创建并添加一个 Paint 实例:

public class GUI {
    public static void Configuration(){
        JFrame frame = new JFrame("Figures Animation");
        frame.setSize(600,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new Paint(); // *** new Paint is here, but nothing is added
        frame.getContentPane().add(BorderLayout.CENTER, panel);
    }

但是没有添加任何有用的东西。所有重要的东西都添加到一个完全不同的 Paint JPanel,一个永远不会显示的面板:

public static void main(String[] args) {
    Circle c = new Circle();
    Quadrat q = new Quadrat();
    Paint p = new Paint();  // **** ANOTHER new Paint is here, and it gets goodies
    p.figuresList.add(c);
    p.figuresList.add(q);

    // but is never added to a JFrame and is never displayed.

    GUI.Configuration();
}

不要这样做。创建一个 Paint JPanel,只有一个,将重要的组件添加到其中,然后只将那个添加到JFrame。最重要的是,不要只是输入代码,在将程序提交到代码之前思考和计划你的程序,这样你就不会看到这样的错误。

同样,不要从 paintComponent 中启动 Timer,也不要在那里创建 Circle。您可以在 paintComponent 中绘制 Circle 实例,但在 Paint 构造函数中创建它并启动您的 Timer。