【发布时间】:2011-06-02 23:59:02
【问题描述】:
我正在尝试绘制每秒出现的圆圈,我能够做到,但如何让旧形状消失?
public void paint(Graphics g) {
try {
while (true) {
Shape circle = new Ellipse2D.Double(500*Math.random(),500*Math.random(), 50.0f, 50.0f);
Graphics2D ga = (Graphics2D)g;
ga.draw(circle);
ga.setPaint(Color.green);
ga.fill(circle);
ga.setPaint(Color.red);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
1) 为了尽快获得更好的帮助,请发布SSCCE 2)
paint(Graphics)在这个千年不要编码 AWT。如果在 Swing 组件中自定义绘画,请使用paintComponent(Graphics)3)while (true)不要在绘画方法中启动无限循环。 4)Thread.sleep(1000);不要在paint方法中等待。