Re-painting

Hello everyone,
I have an image that displayed on a JFrame.
I added a key listener and when i press right or left the image moves but I don't know how to "clean" the last place the image been.
Here is a picture to demonstrate:
So how do I clean my tracks?

Re: Re-painting

Thank you!
It's working now but I got another problem now.
When I click the picture disapear and then re-apear in the new position, this proccess takes to long so my picture kinda disapear for alot of time (less the a second of course)
so when I give a long press on the button the picture disapear.

Re: Re-painting

I read this 2 links.
I know how to work with threads, and looks like the answer is in the first link.
But I don't understand how to use the Timer class.. it needs an actionListener as a parameter but my image moves by the keys which is a key listener.
And I'm confused how to use the timer.. I'm confused.

edit:
I tryed to make a thread let it sleep for a while then re paint but I still get the same results.

You should override de update(Graphics) method and implement a double buffer technique, It's not as hard as it sounds. What you basically do is create an image the size of you component, draw on it, and when it's done, copy it to the Graphics of the component.
Here's (Java Cooperation: double buffering) a link I found googling for "java double buffer paint". Experiment with that.

You should override de update(Graphics) method and implement a double buffer technique, It's not as hard as it sounds. What you basically do is create an image the size of you component, draw on it, and when it's done, copy it to the Graphics of the component.
Here's (Java Cooperation: double buffering) a link I found googling for "java double buffer paint". Experiment with that.

Sorry, but I disagree with this advice. Darryl had it right when he recommended understanding Swing's threading better. It seems like the OP is tying up the EDT, which is A Very Bad Thing. But like I said, without an SSCCE, we're just guessing.

Re: Re-painting

Originally Posted by KevinWorkman

Sorry, but I disagree with this advice. Darryl had it right when he recommended understanding Swing's threading better. It seems like the OP is tying up the EDT, which is A Very Bad Thing. But like I said, without an SSCCE, we're just guessing.

I think we're both right. The EDT is tied up, and the image is not cleared when Kenny is moved. :) An SSCCE would indeed be appreciated.

Again, the problem is when I click left or right the image DISAPEAR for x time and then re-appear in the new position and this proccess takes to much time so it looks bad you can test it.
I can't show you a screen shot because of this situation xD

Re: Re-painting

Do not use run() on a Thread. Instead, use start() to actually spawn the new Thread (Update.class, line 9).

Other than that, with the current code, the image is visible all the time here when I move it left and right, so I don't have your problem. What version JDK are you using?

The threading issues are going to make this program's behavior unpredictable on different platforms. Threads are completely unnecessary here, so I'd highly recommend simply taking the threading out entirely.

Re: Re-painting

Originally Posted by KevinWorkman

The threading issues are going to make this program's behavior unpredictable on different platforms. Threads are completely unnecessary here, so I'd highly recommend simply taking the threading out entirely.

True. A simple repaint() will do for now. I agree with all your comments above. I wasn't trying to change his mind, or yours for that matter, just pointed him to a basic Thread error.