我试图画出每秒出现的圆圈,我能够这样做,但我如何让旧的形状消失?
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();
}
}
答案 0 :(得分:1)
只需获取背景颜色并使用它覆盖带有背景色圆圈的旧圆圈。
答案 1 :(得分:0)
首先尝试在循环中调用super.paint(g);
。
答案 2 :(得分:0)
此处最简单的方法是简单地在g.clearRect(WIDTH, HEIGHT);
方法的顶部调用paint()
。