Java Reference
In-Depth Information
Controlling and Monitoring the Timeline
As you observed when using the Metronome1 program, clicking the buttons causes the animation to start, pause,
resume, and stop. This in turn has an effect on the states of the animation (running, paused, or stopped). Those states
are reflected in the buttons in the form of being enabled or disabled. The following snippet from Listing 2-5 shows
how to start, pause, resume, and stop the timeline, as well as how to tell whether the timeline is running or paused.
startButton = new Button("start");
startButton.setOnAction(e -> anim.playFromStart());
pauseButton = new Button("pause");
pauseButton.setOnAction(e -> anim.pause());
resumeButton = new Button("resume");
resumeButton.setOnAction(e -> anim.play());
stopButton = new Button("stop");
stopButton.setOnAction(e -> anim.stop());
...code omitted...
startButton.disableProperty().bind(anim.statusProperty()
.isNotEqualTo(Animation.Status.STOPPED));
pauseButton.disableProperty().bind(anim.statusProperty()
.isNotEqualTo(Animation.Status.RUNNING));
resumeButton.disableProperty().bind(anim.statusProperty()
.isNotEqualTo(Animation.Status.PAUSED));
stopButton.disableProperty().bind(anim.statusProperty()
.isEqualTo(Animation.Status.STOPPED));
As shown here in the action event handler of the Start button, the playFromStart() method of the Timeline
instance is called, which begins playing the timeline from the beginning. In addition, the disable property of that
Button is bound to an expression that evaluates whether the status property of the timeline is not equal to
Animation.Status.STOPPED . This causes the button to be disabled when the timeline is not stopped (in which case
it must be either running or paused).
When the user clicks the Pause button, the action event handler calls the timeline's pause() method, which
pauses the animation. The disable property of that Button is bound to an expression that evaluates whether the
timeline is not running.
The Resume button is only disabled when the timeline is not paused. To resume the timeline from where it was
paused, the action event handler calls the play() method of the timeline.
Finally, the Stop button is disabled when the timeline is stopped. To stop the timeline, the action event handler
calls the stop() method of the timeline.
Now that you know how to animate nodes by creating a Timeline class and creating KeyFrame instances, it's time
to learn how to use the transition classes to animate nodes.
Using the Transition Classes for Animation
Using a TimeLine allows for very flexible animations. There are a number of common animations facilitating the
translation from one state to another that are out-of-the box supported by JavaFX. The javafx.animation package
contains several classes whose purpose is to provide convenient ways to do these commonly used animation tasks.
Both TimeLine and Transition (the abstract root class for all concrete transitions) extend the Animation class.
Table 2-1 contains a list of transition classes in that package.
 
Search WWH ::




Custom Search