• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make this picture goes down slowly?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello , i made a simple code for a picture(jLabel1) but its not going , i think it should be with thread but dont know how to do it.

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ertan Hasani wrote: . . . i think it should be with thread but dont know how to do it.
. . .

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.
 
Ertan Hasani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



This want an ActionListener , and i cant add an ActionListener to a jLabel , because the picture is as jLabel .

I made this one but the MouseClick Listener is not working when i'm clicking on the picture.

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ertan Hasani wrote: . . .
This want an ActionListener , and i cant add an ActionListener to a jLabel , because the picture is as jLabel .
. . .

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
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



The problem here is that the picture is not recognizing the Mouse Click Event , and I dont know why?

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



How should I make picture move without Threads?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ertan Hasani wrote:How should I make picture move without Threads?



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.)

 
Ertan Hasani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)



Can you please help me with that because I dont know how to do it .

p.s I need MouseClick because , my require is that: when the image is clicked , it starts go down.

Thanks in advance.

Here's what i did but its not recognizing the Mouse Click Event.

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the Timer link which Paul C and I posted?
Or something like that. You need to call the start() method somewhere.
 
Ertan Hasani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Yep i read it , heres the same one and i called start.



I made this one " System.out.println("2"); " to know if the program is going here .

But when I'm clicking on the picture does not shows anything.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ertan Hasani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Still the same. y = 50; and dy = 1;

But i think that the problem is somewhere at MouseClick Event , because the image is not secognnizing it .
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is the loop that is the problem. The loop executes so quickly when the action is fired that the image cannot keep up with it. Then next time the action is fired y is already 200 so nothing happens.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ertan Hasani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Can you help me with how to display panel ? Does the code should be like this?

 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Ertan Hasani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)


Thank you so much i made it but its happening something , i think its because repaint();

here's code:



Heres whats happening :
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ertan Hasani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



I add a new line on paint() method.



Now its just as I wanted, but picture dissapers and shows again so fast while its going down , like four or five times.(like in pictures)


 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Ertan Hasani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)



but when I'm changing this:


to this:


is showing no image.
 
reply
    Bookmark Topic Watch Topic
  • New Topic