Repaint轨迹保留?(待处理,待编辑)
1 import java.awt.Color; 2 import java.awt.Graphics; 3 4 import javax.swing.JFrame; 5 import javax.swing.JPanel; 6 7 8 public class SimpleAnimation { 9 10 /** 11 * @param args 12 */ 13 int x=0,y=0; 14 public static void main(String[] args) { 15 // TODO Auto-generated method stub 16 SimpleAnimation gui=new SimpleAnimation(); 17 gui.test(); 18 } 19 public void test() { 20 JFrame frame=new JFrame(); 21 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 22 23 MyDrawPanel drawPanel=new MyDrawPanel(); 24 25 frame.getContentPane().add(drawPanel); 26 frame.setSize(500, 500); 27 frame.setVisible(true); 28 29 for (int i = 0; i < frame.getHeight()-60; i++) { 30 x++; 31 y++; 32 drawPanel.repaint(); 33 try { 34 Thread.sleep((int)(Math.random()*140)); 35 } catch (Exception ex) { 36 // TODO: handle exception 37 ex.printStackTrace(); 38 } 39 } 40 } 41 class MyDrawPanel extends JPanel{ 42 public void paintComponent(Graphics g) { 43 int red=(int)(Math.random()*255); 44 int green=(int)(Math.random()*255); 45 int blue=(int)(Math.random()*255); 46 g.setColor(Color.red); 47 g.fillRect(0, 0, this.getWidth(), this.getHeight()); 48 g.setColor(new Color(red,green,blue)); 49 g.fillOval(x, y, 40, 40); 50 } 51 } 52 53 }