File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Animation in Swing 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 "Animation in Swing" Watch "Animation in Swing" New topic
Author

Animation in Swing

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Hello.

I am making an applet, using swing. I want animations in it. Like trains moving from a point 2 another point. So, what package can i use?


kunal
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i have an example(not an applet) that moves a horse across the frame


SCJP
Visit my download page
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Use a Swing Timer for animtion.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

Rob makes a good point. using a timer is generally preferred. the class i posted runs in its own thread so it can be an exception. even so, if i wrote it today instead of 8 years ago i would use a Timer instead of Thread.sleep().
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Frankly speaking, i didn't understood Randall's code. May be, it looks very difficult. Or, may be, i am running from difficult codes.

Can you(or anyone) provide me a link , or package details, or any thing which would be of great help?

Between Thanks Randall & Rob for your concern
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

sorry if it seems i am hijacking this thread but this is on topic anyway. i got to thinking about changing my old code to use a Timer and realized there is a fundamental difference. in the original app i created 8 Horse objects, each running in it's own thread, and they all wanted to run the synchronized method parent.postResults(name);

if i remember correctly, when the lock on the synchronized code becomes available, a waiting thread at random gets chosen. i think that using a Timer they will be "competing" for the EDT instead. i also think the EDT queues the waiting events(so it is not random).

this brings up a question. would i still need to synchronize the postResults() method? or will the EDT take care of that?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i refactored the program to use a Timer, but this is the rare case where using Thread.sleep() works better. i have 8 Horse threads that all call the synchronized method postResults(). that method lists the names of all the Horses and how they finished in the race. with the original code postResults() completed almost instantly. using a Timer it takes a second or so. not all that surprising if you think about it. here is the code anyway, for those who want to see a Timer version.
Jj Hill
Ranch Hand

Joined: Jul 25, 2011
Posts: 41

Randall's way is correct, except for you're usually supposed to use a timer. See this on timers: http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
A timer triggers an event every <delay> milliseconds. So if you have a timer that triggers an event, inside the corresponding actionPerformed() method you should put code to increase the coordinates of the Horse.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Animation in Swing