关于repaint方法
import java.awt.Color;import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestRepaint extends JFrame implements Runnable{
int x,y;
Graphics g;
public TestRepaint(){
this.setTitle("画图");
this.setSize(1000,1000);
this.setVisible(true);
JPanel panel = new JPanel();
this.add(panel);
}
@Override
public void paint(Graphics g){
g.setColor(Color.RED);
g.fillOval((int)(Math.random()*1000), (int)(Math.random()*1000),20, 20);
}
public static void main(String[] args) {
TestRepaint tr = new TestRepaint();
new Thread(tr).start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
repaint();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}如何做到:当生成一个实心圆的时候上一个院被清除。也就是说画布上只有一个随机圆在跳动