user1383359
user1383359

Reputation: 2753

The "Java" way to do Animation

I want to do animation in Java. I have looked at: http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

What I'm surprised by is that in the timer action handler:

This seems somewhat odd to me because:

Question: am I mis reading the sample code, or is this the "correct" way to do animation in Java?

Thanks!

Upvotes: 2

Views: 481

Answers (3)

Jasper Siepkes
Jasper Siepkes

Reputation: 1405

You might want to try Trident. Its a clean and simple animation library without a lot of bells and whistles and makes your life a LOT easier if your trying to do animations in Swing.

Upvotes: 0

PeakGen
PeakGen

Reputation: 23035

In case of "Animation", JavaFX is way better than Java, because it is built mainly for this purpose

Upvotes: 2

mikera
mikera

Reputation: 106401

It's fairly normal to do the state updates and rendering separately.

Java/Swing is pretty normal in this regard.

Reasons:

  • Lots of things can update the state (timer events, user input etc.) so it's useful to make changes very lightweight and decouple them from screen refreshes so that you can do them at any time
  • You often want screen refreshes to run at a different rate from the model state updates. Typically you want a fixed interval for animation / state updates but want the screen refreshes to happen as fast as they can. This is especially true if you are doing interpolation in your animations.
  • Rendering often needs to take place in a specific context (in this case, the Swing UI thread). You don't necessaily want to limit state changes to also only happen in this context
  • It's good practice to separate view (screen presentation) from the data model. Apart from being a good conceptual design distinction, this allows you do things like test the data model independently from the display, run a version of the model on a server without a GUI etc.

Upvotes: 5

Related Questions