1

请问,请问java repaint 重复的问题,遇到了一个麻烦,就是用重复drafting 来表达Pacman Open&Close 的嘴巴动作。但是这个程序只有一次不会动......有人可以帮我解决这个问题......非常感谢!: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 ();
    }
}
4

1 回答 1

9

建议:

  • 不要直接在 JFrame 中绘制
  • 不要在paint(...) 方法中绘制。
  • 从不调用Thread.sleep(...)Swing 事件线程
  • 特别是不要在paint(...)orpaintComponent(...)方法中调用它。
  • 而是在 JPanel 或 JComponent 的 paintComponent(...) 方法中绘制
  • 阅读 Java 图形教程,因为它们将解释所有这些。
  • 不要repaint()在paint(...) 或paintComponent(...) 内部调用
  • 为您的动画循环使用 Swing Timer。教程将再次帮助您做到这一点。
于 2013-03-03T07:10:17.307 回答