先放上源代碼。
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel{
private int posx = 0;
private int posy = 0;
public void paint(Graphics
g){
super.paint(g);
Graphics2D g2d = (Graphics2D)
g;
g2d.setColor(Color.RED);
g2d.fillOval(posx, posy, 50 ,
50);
posx += 1;
posy += 1;
}
public static void main(String[]
args) throws InterruptedException{
JFrame jframe = new JFrame();
jframe.setTitle("test");
Game game = new Game();
jframe.add(game);
jframe.setSize(300,300);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while(true){
game.repaint();
Thread.sleep(10);
}
}
}
然後設斷點,進行調試。
paint(Graphics g)是由AWT線程自動運行的。可以看到右邊有Thread[AWT-EventQueue-0]這個線程。
第一個斷點super.paint(g);用戶重新刷新graphics。
第二個斷點game.repaint()是用來告訴AWT線程再次執行paint();
java_AWT之移動的小球。,古老的榕树,5-wow.com