I have a problem.. I have a image painted on my java applet and a for loop to make the x move up 5 every time.. but it leaves behind the old image for a bit then it will erase it..
for(int tohouse = 0; tohouse <= 520; tohouse+=2) {
try {
x+=5;
tohouse+=10;
if (pos == 0) {
rectX+=5;
g.drawImage(picture1,x,150,this);
pos = 1;
} else if (pos == 1) {
rectX+=4;
g.drawImage(picture2,x,150,this);
pos = 2;
} else if (pos == 2) {
rectX+=5;
g.drawImage(picture3,x,150,this);
pos = 0;
}
}
}
Heres a image:
You can’t do animation by looping in your
paintComponent()orpaint()method. Those methods are asking you to paint yourself at a specific point in time (i.e. the current frame).Instead, you need to decouple the logic of “moving” your sprite from “rendering” your sprite. Look for something like this