I am writing a program that shows a image moving across the screen, however for the assignment I must use the drawImage method. I create a new Image called turtle, and then draw the Image at the one point, then I draw it again at a later point, however, the first drawn Image is still visible, how can I hide/make this disappear. I am new to Java so please explain this as simply as possible.
Thanks!
Here is a sample of my code:
import java.awt.*;
import java.applet.*;
public class Race extends Applet {
Image tortoise;
public void init() {
setSize(1275,1000);
tortoise = getImage( getDocumentBase(), "resources/tortoise.png" );
}
public void paint( Graphics g) {
g.drawImage(turtle, 100, 100, this);
g.drawImage(turtle, 200, 200, this); // Both images are shown.
}
}
You will have to overwrite its previous position with the background before you redraw it.
clearRectwould be a good option for this.If you are drawing more than the one image to the screen, you will have to redraw all images which overlap the area you clear, in the correct z order, to maintain the screen state.