I made this very simple program as a test to experiment with animation. I have successfully animated the circle yes, but it leaves a black trail for every iteration of the circle.
How do I "undraw" the circle for every iteration so that visually it appears as just a circle moving with no trail? Do I use the repaint method? I have inserted repaint into the code, but it didn't work... Here's my code:
import java.awt.*; import jpb.*; public class moveCircle { public static void main(String[] args) throws java.lang.InterruptedException { // window is created DrawableFrame window = new DrawableFrame("Check out the moving circle, yeah"); window.show(); window.setSize(1010, 700); Graphics g = window.getGraphicsContext(); // graphics allowed within window // circle is created int xCoordinate = 795; while(true) { Thread.sleep(10); xCoordinate++; if(xCoordinate > 1010) xCoordinate = -75; g.setColor(Color.lightGray); g.fillOval(xCoordinate, 295, 60, 60); g.setColor(Color.black); g.drawOval(xCoordinate, 295, 60, 60); window.repaint(); } } }