4. From the Open Project dialog box,
navigate to your chosen directory and
open the TransitionExample project,
as shown in Figure 4. If you receive
a message dialog stating that the
jfxrt.jar file can’t be found, click the
Resolve button and navigate to the
rt/lib folder subordinate to where you
installed the JavaFX 2.0 SDK.
Note: You can obtain the NetBeans
IDE from the NetBeans site.
5. To run the application, click the Run
Project icon on the toolbar, or press
the F6 key. The Run Project icon has
the appearance of the Play button on
a DVD player, as shown in Figure 5.
The TransitionExample application
should appear in a window, as shown in
Figure 6.
You’ll notice that clicking the Play
and Stop buttons has no effect. Your
mission will be to add code that imple-
ments the behavior de-
scribed previously. Here
are steps you can follow to
implement this behavior:
transition = Translate TransitionBuilder.create()
// TO DO: Insert code to set the duration to 1500 milliseconds
.node(ball)
// TO DO: Insert code to set the fromX property to 0
// TO DO: Insert code to set the toX property to 440
.interpolator( Interpolator.LINEAR)
// TO DO: Insert code to set the autoReverse property to true
JAVA IN ACTION
.cycleCount( Animation.INDEFINITE)
.build();
Step 1: Create a
TranslateTransition
Instance to Animate
the Node
To cause the ball to move
(also known as translate)
between two different
positions in the scene, we’ll
create an instance of the
Translate Transition class.
Take a look at the code in the
TransitionExampleMain.java
file in the TransitionExample
project, which shows
the starter code for this
example.
Using the
TranslateTransitionBuilder
class to build
TranslateTransition.
The starter code in
TransitionExampleMain.java
makes use of builder classes
in the JavaFX 2.0 API, in-
See all listings as text
cluding the Translate TransitionBuilder
class shown in Listing 1.
Go ahead and fill in the lines indicated
by the “TO DO” comments, so the code
in Listing 1 turns into the code shown in
Listing 2.
There are many builder classes in
the JavaFX API, and their purpose is to
enable a declarative style of programming to create and set the properties
of objects. For example, the code you
completed in Listing 2 creates an instance of the Translate Transition class,
and it populates that instance with
properties such as the duration of the
animation and the node in the scene
graph being animated. As you can see
in TransitionExampleMain.java, other
builder classes used in this application include ButtonBuilder, CircleBuilder,
RectangleBuilder, SceneBuilder, and
VBoxBuilder.
ABOUT US
Figure 4
Figure 5
blog
Figure 6
Step 2: Define an Event Handler in the
Play Button
To make the Translate Transition start,
pause, and stop, you’ll define event handlers in the buttons that call the appropriate methods on the Translate Transition
object. For example, the starter code in
Listing 3 from TransitionExampleMain
.java contains the builder that creates a
Button instance for the Play button.
As you did before, fill in the lines indicated by the “TO DO” comments, turning the code shown in Listing 3 into the
code shown in Listing 4.
Using methods of the Animation class to
control the animation. All the transition
classes in Figure 1 are subclasses of the