java—单击框架中的按钮后重新开始绘制

ss2ws0br  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(386)

我试图重新开始画后,点击重新启动按钮的框架。
但单击按钮后,两个动画似乎开始协同工作,如下面的gif所示:

点击按钮后,两个计数标签似乎同时工作,但绘画保持不变,并没有从初始位置开始。
我已经在按钮的动作侦听器中发表了评论。
代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.*;

class Count {
    private int num;

    public Count() {
        this.num = 1;
    }

    // Generate Next Number
    public void generate(int currentNumber) {
        this.num = currentNumber;
        this.num += 1;
    }

    public int getNumber() {
        return this.num;
    }
}

interface Callback {
    public void action(int cnt);
}

class Panel extends JPanel {
    private final BufferedImage image;
    private Count count;
    private Callback onUpdate;

    public void setOnUpdateAction(Callback action) {
        this.onUpdate = action;
    }

    public Panel() {
        this.image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
        this.count = new Count();
        Timer timer = new Timer(0, ae -> createImage(image));
        timer.setDelay(1000);
        timer.start();
    }

    public void createImage(BufferedImage image) {
        Graphics g = image.getGraphics();

        int number = this.count.getNumber();

        // Set field on frame which will be added to bottomPanel
        for (int i = 0; i < (number * 20); i++) {
            for (int j = 0; j < (number * 20); j++) {
                g.setColor(Color.GRAY);
                g.fillRect(i, j, 20, 20);
                g.setColor(Color.GREEN);
                g.drawRect(i, j, 20, 20);
            }
        }

        // Generating next number
        this.count.generate(number);
        onUpdate.action(this.count.getNumber());
        repaint();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }
}

class GUI extends JFrame {
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JLabel countLabel;
    private JButton restartButton;

    public GUI() {

        /*Panels*/
        leftPanel = new JPanel();
        rightPanel = new JPanel();

        /* Button */
        restartButton = new JButton(UIManager.getIcon("FileView.directoryIcon"));

        leftPanel.add(restartButton);

        updateLabel(0);

        Panel matrixPanel = new Panel();
        matrixPanel.setPreferredSize(new Dimension(300, 300));

        matrixPanel.setOnUpdateAction(new Callback() {
            @Override
            public void action(int cnt) {
                countLabel.setText("Count #" + cnt);
            }
        });

        /* Restart Button, Action Listener */
        restartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Panel mPanel = new Panel();
                mPanel.setPreferredSize(new Dimension(300, 300));
                System.out.println("New Defined");
                mPanel.setOnUpdateAction(new Callback() {
                    @Override
                    public void action(int cnt) {
                        countLabel.setText("Count #" + cnt);
                    }
                });
                rightPanel.add(mPanel);
            }

        });
        /* Action Listener Part End */

        rightPanel.add(matrixPanel);

        setLayout(new GridLayout(1, 2));

        add(leftPanel);
        add(rightPanel); 

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        pack();
    }

    public void updateLabel(int number) {
        /*Label - 1*/
        countLabel = new JLabel();
        countLabel.setName("CountLabel");
        countLabel.setText("Count #" + number);
        countLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
        countLabel.setBounds(10, 5, 300, 20);

        leftPanel.add(countLabel);
    }
}

public class NumberPresentation {
    public static void main(String[] args) {
        new GUI().setVisible(true);
    }
}

在button的action listener中,我再次定义了panel类的对象,并将其添加到添加到框架中的右侧面板中。
如何编程按钮,使其在单击后重新开始绘制?

5fjcxozz

5fjcxozz1#

按下按钮时(在其指定的操作侦听器中):
呼叫 .stop() 在摇摆计时器上
创建一个新的bufferedimage并将其分配给bufferedimage变量
将计数对象重新设置回起始状态,并重置用于绘制图形的所有状态字段
然后重新启动摆动计时器
底线和总体目标是,您希望将gui返回到其原始状态,不管“状态”是如何定义的。通常这意味着将关键点状态字段返回到其原始值,并重新创建任何已启动的动画循环(这里是swing计时器)。

相关问题