No, you should not use a thread. Swing is not thread‑safe. Try a Swing Timer to move the image and call repaint() on the panel.
Ertan Hasani wrote: . . . i think it should be with thread but dont know how to do it.
. . .
Campbell Ritchie wrote:No, you should not use a thread. Swing is not thread‑safe. Try a Swing Timer to move the image and call repaint() on the panel.
Beware: a Timer fires after an interval of however many milliseconds. It takes a certain time to repaint an image. So if it takes 10ms to repaint and you fire the timer every 5ms, you will miss out repaint calls and get a poor effect.
Also don't try using null Layout.
Don't you add the listener to the timer? I can't remember at the moment. Create an action listener class which moves the image by a few px and add that.
Ertan Hasani wrote: . . .
This want an ActionListener , and i cant add an ActionListener to a jLabel , because the picture is as jLabel .
. . .
Campbell Ritchie wrote:Don't you add the listener to the timer? I can't remember at the moment. Create an action listener class which moves the image by a few px and add that.
Campbell Ritchie wrote:If you are using Swing get rid of the Runnable.
What mouse cliick event?
Don't use low level events like mouse click. Use high level events like action event. And where do you have a listener for that mouse event?
Ertan Hasani wrote:How should I make picture move without Threads?
Paul Clapham wrote:
As already suggested, you use a Swing Timer. Each time the timer's action listener is called, you draw the picture in a different position -- presumably one step farther down. That's all you need to do. Mouse clicks are not required. If it makes you more comfortable, there's a thread involved, it's just that you don't get to control it directly. Which is a good thing.
If "Use a Swing Timer" isn't enough information for you and you need an example or an explanation, then there's a tutorial: How to Use Swing Timers. (That applies throughout Java -- if you want a tutorial about how to use feature X in Java then "Java X tutorial" are your web search keywords, and they should generally lead you to the Oracle tutorial.)
Campbell Ritchie wrote:Have you read the Timer link which Paul C and I posted?
Or something like that. You need to call the start() method somewhere.
Campbell Ritchie wrote:Does it make any difference that you have the timer as a local variable in that method? Try moving the timer declaration elsewhere.
Where does y come from? Is it a field? What is its initial value?
Paul Clapham wrote:You have totally misunderstood the usage of Swing timers, not to mention everything people have said in this thread about them.
Your Timer's action listener should display the panel ONCE. Do not write a loop with repaint() on the inside; the Timer will provide the repetition for you.
Paul Clapham wrote:You're on the right track, but didn't you want the image to move down? If you do, then don't you have to change the value of y somewhere? (Hint: you'll want to do that in some code which is executed repeatedly and not in code which is executed only once.)
And you've included code to stop the timer, which is a good thing too. But you've put the stop-test in the wrong place. (Hint: same as previous hint.)
Paul Clapham wrote:That looks good to me, but although you didn't say so, I am guessing you want something different. So... remember that calling repaint() on a component eventually causes Swing to call that component's paintComponent() method. Does your implementation of that method paint the whole component? It looks like it doesn't.
And I'm going to move this thread to the Swing forum, because this kind of thing gets discussed over there all the time.
Paul Clapham wrote:So I see you're overriding the component's paint() method. You should override its paintComponent() method. (There seems to be an inexhaustible supply of tutorials from the last century which still override paint().)
And when you do that, you need to call super.paintComponent() as the first step. That should clear the display. (Note that you didn't do that, you called super.some other method which does something else.)