AnimationTimer
JavaFX uses a separate thread to run animations (ex: rotateTransition) so the rest of your program doesn't freeze while the animation plays. You can tap into the built-in animation timer and execute a short piece of code during each animation frame. Here's how:
- Create your own AnimationTimer class that extends AnimationTimer.
(You can do this as a private inner class or in a separate class.)
- Override and implement the handle() method to do whatever you want. (See AnimationTimer API)
- The parameter of handle() is the current time given in nanoseconds. There are 10e9 nanoseconds in 1 second. You'll need this to convert your delay slider value (measured in seconds) into nanoseconds
- To control the delay timing of your animation, add the following to your custom AnimationTimer class:
- Create attributes for delay and previousTime, both of type long
- If (currentTime - previousTime) >= delay then call nextGeneration() and set previousTime to currentTime.
- Create an instance of your extended AnimationTimer class
- Start the timer by calling start(). Stop it by calling stop().
Here are two video demos: