aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing Animation.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing Animation...." Watch "Swing Animation...." New topic
Author

Swing Animation....

paul beppe
Ranch Hand

Joined: Aug 09, 2012
Posts: 86
Hello to all! I have a problem with the animation .... in my program I have 4 images at their speed ... if the first image is scrolling and I click on another image ... does not respect the speed increases ... the speed ... but if before clicking on another image I click "stop" then meets the speed ...

this is code:



where is the error???

Sorry for my English ... I hope I was clear
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 650
    
  11
Hi Paul,

I'm afraid your question is not very clear, no.

There are many remarks to be made, bu first and most important: get your indentation correct!
Without this, it is nearly impossible to follow what is going on. Just look at your code snippet: what do you think it looks like?
I learned myself the following trick, to be applied when a class or method ends in a lot of braces:



Now, for your question about speed. Many things could be wrong here, but the first thing I noticed is this:

See line 30: timer2 has a fixed time interval of 200 ms. In the action there seems to be a constant decrease of the Y value of 1. So where's any speed change supposed to come from?

Well, much more could be said about the code, but I hope you have some clues to get you going.

Greetings,
Piet
paul beppe
Ranch Hand

Joined: Aug 09, 2012
Posts: 86
i write you all code...so you can understand my problem....



Example: i click to "buttomImage1" and start animation....if i click to "buttonImage2" when the image is in scroolling i can't to "stop" or "restart" the second animation...whay??? i haven't control on button "pause" and "play"...

this is problem...
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 650
    
  11
Hi Paul,

well, indentation is now a lot better!

I find your code a bit hard to follow, because it is not very well structured. What I suspect to be the trouble is the following:

when you press buttonImage1, then you add anonymous actionlisteners to the pause and play buttons. The same happens when you press buttonImage2.
That means (I think, I have no experience with this) that you now have TWO actionlisteners for both the play and pause buttons.
Adding an actionlistener does just that: add a new actionlistener to whatever was there before. It does not replace the old one(s).
The next thing is that in the AL's you simply redefine a timer by: timer = new(...). I think that the old timer just goes on throwing its old action events, picked
up by the old actionlisteners! Of course, causing a lot of unexpected outcomes.

As I said: since I have never programmed things this way, I can't be sure of my answers. But I should try this:

1) define two actionlisteners for the play and the pause buttons and add these to the two buttons.
2) when pause is clicked, then stop the timer; when play is clicked, restart the timer.
3) in the actionlistener for the two image buttons, first stop the timer, then redefine the timer depending on which button is clicked. Still, maybe it is a lot easier
to define two timers, making sure that at most only one at the time is running

Well, enough for now. Let us know if this was indeed the problem, and whether you got it working. Success!

Piet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing Animation....