Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nathan deNoyelles
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't repaint(), being asynchronous, supposed to be thread safe?

But, why muck around with Threads when javax.swing.Timer exists?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic