aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Is it possible to make a smooth animation in Java? Animation Stutters... 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 "Is it possible to make a smooth animation in Java? Animation Stutters..." Watch "Is it possible to make a smooth animation in Java? Animation Stutters..." New topic
Author

Is it possible to make a smooth animation in Java? Animation Stutters...

Nathan deNoyelles
Greenhorn

Joined: Jul 02, 2012
Posts: 5
Hi everyone I'm new to the forums and I really hope I can get some help with this problem

I've been working really hard lately looking everywhere to try and figure out how to get my Java animations to flow smoothly, the main problem I have is the animation runs smooth with hiccups every 2 seconds where the animation appears to "pop" ahead of itself and then "pop" back making the animation look choppy.

Here's a short example that I created to show you guys the problem i'm having. Should be easy to compile and run if you wanna give it a try. When you run it you'll see a blue rectangle slide back and fourth on the screen, if you watch it for a bit you should notice that the rectangle seems to stop and start briefly, or "pop" ahead of itself and then jump back making the animation kinda choppy.

So my question is, what could be causing this? Any suggestions on the code or maybe some ideas on better ways to achieve a nice animation?

Thank you all for taking the time



I've also tried the following to account for the time it might take to draw a frame and compensate for the that time by sleeping less if the frame took time to draw, didn't seem to help :/

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
looks like you're updating Swing components, outside of Swing's EDT.

when in a separate thread, try wrapping things like this
repaint();

in a SwingUtilities.invokeLater, same as in your main()
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Isn't repaint(), being asynchronous, supposed to be thread safe?

But, why muck around with Threads when javax.swing.Timer exists?


luck, db
There are no new questions, but there may be new answers.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Isn't repaint(), being asynchronous, supposed to be thread safe?

I've seen a number of documented 'thread safe' proven not to be,
so I no longer trust it.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Nathan, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/java-2d/61103-intel-hd-integrated-graphics-3000-java-trace-hardware-accelerated.html

From that thread, it appears you have been digging deeper into possible causes for your problem. How about sharing your findings here?
Nathan deNoyelles
Greenhorn

Joined: Jul 02, 2012
Posts: 5
Darryl Burke wrote:Nathan, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/java-2d/61103-intel-hd-integrated-graphics-3000-java-trace-hardware-accelerated.html

From that thread, it appears you have been digging deeper into possible causes for your problem. How about sharing your findings here?


Thanks for your reply

I didn't mention the post I made on java-forums.org because I've tried animations on various computers, I get the same stuttering regardless of whether hardware acceleration is present therefore I've ruled out the lack of hardware acceleration being the culprit. My question on that forum was regarding why hardware acceleration is not present on a particular machine, my question here is why my animations in general on all platforms are not fluid and how I might go about improving their performance.

I've also tried the Timer approach with no luck I still get the same stuttering every 2 or so seconds where the animation appears to jump forward and then back again. Really stumped I'm not sure what could be causing this even after a lot of research. There's got to be a way to make it stutter free or at least very close to it.

Thanks again for your help!

Nathan deNoyelles
Greenhorn

Joined: Jul 02, 2012
Posts: 5
Also seems to have the stuttering using a BufferStrategy and full screen exclusive mode, could this have something to do with Vertical Sync? Or the lack of it? Though I would expect to see the frame cut in half as the screen updates not the whole animation stutter:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to make a smooth animation in Java? Animation Stutters...