一个小错误,在for循环中,每次repaint()的时候,记得先把frame涂成白色的。等于擦掉原来的痕迹·。

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 import javax.swing.*;
 4 
 5 public class Animate {
 6     int x=1;
 7     int y=1;
 8     public static void main(String[] args){
 9         Animate gui=new Animate();
10         gui.go();
11     }
12     public void go(){
13         JFrame frame=new JFrame();
14         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15         MyDrawP drawP=new MyDrawP();
16         frame.getContentPane().add(drawP);
17         frame.setSize(500,270);
18         frame.setVisible(true);
19         for(int i=0;i<124;i++,x++,y++){//这样也可以啊、必须的。
20             x++;
21             drawP.repaint();
22             try{
23                 Thread.sleep(50);
24             }catch(Exception ex){}    
25         }
26     }
27 
28    class MyDrawP extends JPanel{
29     public void paintComponent(Graphics g){
30         g.setColor(Color.white);//忘记了要先弄成白板了。
31         g.fillRect(0, 0, 500, 250);//忘记了要先弄成白板了。
32         g.setColor(Color.blue);
33         g.fillRect(x, y, 500-x*2, 250-y*2);
34         
35     }
36   }
37 }

 

posted @ 2013-08-08 16:09  婷婷玉立的成长之家  阅读(292)  评论(0编辑  收藏  举报