打扰一下,请问java重画重复的问题,
我遇到重复使用制图表达Pacman打开和关闭嘴巴动作的麻烦。
但是这个程序只有一次不会动...
有人可以帮助我解决这个问题...
非常感谢!:D

我的代码如下:

package Strive;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

class CDrawF extends JFrame {
    CDrawF (){
        setTitle("繪製各式圖形");                       //JFrame interface
        setBounds(50, 50, 490, 260);        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        for(int i = 0; i<= 360; i++){              //repeatly 360 times
        repaint();
        g2.setColor(new Color(1.0f, 0.0f, 1.0f));
        g2.fill(new Arc2D.Double(100, 100, 80, 80, 30, 300, Arc2D.PIE));
        //PacMan close mouth
        repaint();
        try{            //Delay setions
                Thread.sleep(1000);
             }catch(InterruptedException ex){}
        g2.fill(new Arc2D.Double(100, 100, 80, 80, 10, 340, Arc2D.PIE));
        //PacMan open mouth
        repaint();
        }
    }
}

public class NewClass {          //Main
    public static void main(String[] args){
        new CDrawF ();
    }
}

最佳答案

意见建议:


不要直接在JFrame中绘制
不要使用paint(...)方法。
切勿在Swing事件线程上调用Thread.sleep(...)
特别是不要在paint(...)paintComponent(...)方法中调用它。
而是绘制一个JPanel或JComponent的paintComponent(...)方法
阅读Java图形教程,因为它们将解释所有这些。
不要在paint(...)或paintComponent(...)内部调用repaint()
在动画循环中使用Swing计时器。同样,本教程将帮助您做到这一点。

09-25 18:55
查看更多