Remove Painted Image From Graphics
Hello. I have just started programming a top down first person shooter game. In order to paint the player, I need to use Graphics to put it on the screen. Below is some code:
Code:
*note: this class implements Runnable*
int i = 0;
Thread t = new Thread(this);
public CLASSNAME() {
t.start();
//do everything else
}
/* All Painting Code Goes Here */
public void paint(Graphics g) {
g.drawImage("http://www.java-forums.org/images/player.png", i, 50, null); //Arguments: image, x-length (from left), y-length (from top), null
}
/* Main/Update Loop */
public void run() {
for(;;) {
//update game state (update position and animation)
i += 10;
repaint(); //calls paint(Graphics g)
try {
t.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
//delay
}
}
The paint method repeatedly paints the player image every time it is called, creating more images on the screen every time it is called. How can I make it so there is only one image on the screen at a time and make it so that the previous one gets removed?
Also, I have tried the clearRect() method which fills a selected area with the background color. However, the image still stays in the memory, so this doesn't completely remove it.
Thanks in advance for helpful replies!