有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中删除形状的绘图

我试图画出每秒都会出现的圆圈,我本来可以这样做的,但是我如何才能让旧的形状消失呢

 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();
} 

}


共 (3) 个答案

  1. # 1 楼答案

    只需获得背景色,并使用它来覆盖一个背景色圆圈旧的圆圈

  2. # 2 楼答案

    尝试在循环中首先调用super.paint(g);

  3. # 3 楼答案

    这里最简单的方法是在paint()方法的顶部调用g.clearRect(WIDTH, HEIGHT);