单击JButton后如何重绘()?

时间:2016-11-10 20:56:28

标签: java swing drawing

我想制作迷宫路径的动画。我在循环中使用drawLine(x1,y1,x2,y2)和repaint()来逐个方形地绘制路径。 但它没有刷新,只显示路径的最后一步......:Maze

感谢您的帮助。

public class Fenetre extends JFrame {
    private JButton go = new JButton ("GO");
    private Panneau panneau = new Panneau();

    /* ... */

    public Fenetre() {             
        /* ... */

        this.go.addActionListener(new BoutonListener());
        this.top.add(this.go);

        this.container.add(this.top, BorderLayout.NORTH);
        this.container.add(this.panneau, BorderLayout.CENTER);

        this.setContentPane(this.container);
        this.setVisible(true);
    }

    private void go() {     
        /* ... */

        for (int i = 0; i < this.panneau.getChemin().size() - 1; i++) {
            this.panneau.setX1(this.panneau.getChemin().get(i).getX());
            this.panneau.setY1(this.panneau.getChemin().get(i).getY());
            this.panneau.setX2(this.panneau.getChemin().get(i + 1).getX());
            this.panneau.setY2(this.panneau.getChemin().get(i + 1).getY());

            this.panneau.repaint();

            try {
                Thread.sleep(100);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public class BoutonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            go();
        }
    }
}
public class Panneau extends JPanel {
    /* ... */

    public void paintComponent(Graphics g) {
        /* ... */

        g.setColor(Color.red);
        g.drawLine(this.posX + 5 + (10 * this.x1), this.posY + 5 + (10 * this.y1), this.posX + 5 + (10 * this.x2), this.posY + 5 + (10 * this.y2));
    }

    /* ... */
}

0 个答案:

没有答案