Search code examples
javadrawingjpanelerase

erasing a shape in java


i am trying to draw circles that appear every second, i was able to do so but how do i make the old shape disappear ?

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

}


Solution

  • Just get the background color and use it to cover the old circle with a background-color circle.