Java Reference
In-Depth Information
resumeButton.disableProperty().bind(anim.statusProperty()
.isNotEqualTo(Animation.Status.PAUSED));
stopButton.disableProperty().bind(anim.statusProperty()
.isEqualTo(Animation.Status.STOPPED));
stage.setScene(scene);
stage.setTitle("Metronome using TranslateTransition");
stage.show();
}
}
Using the TranslateTransition Class
As shown in the following snippet from Listing 2-6, to create a
TranslateTransition
we're supplying values that are
reminiscent of the values that we used when creating a timeline in the previous example. For example, we're setting
autoReverse
to true and
cycleCount
to
Animation.INDEFINITE
. Also, just as when creating a
KeyFrame
for a timeline,
we're supplying a duration and an interpolation type here as well.
In addition, we're supplying some values to properties that are specific to a
TranslateTransition
, namely
fromX
and
toX
. These values are interpolated over the requested duration and assigned to the
layoutX
property of the node
controlled by the transition (in this case, the circle). If we also wanted to cause vertical movement, assigning values to
fromY
and
toY
would cause interpolated values between them to be assigned to the
layoutY
property.
An alternative to supplying
toX
and
toY
values is to provide values to the
byX
and
byY
properties, which enables
you to specify the distance to travel in each direction rather than start and end points. Also, if you don't supply a value
for
fromX
, the interpolation will begin with the current value of the node's
layoutX
property. The same holds true for
fromY
(if not supplied, the interpolation will begin with the value of
layoutY
).
circle = new Circle(100, 50, 4, Color.BLUE);
TranslateTransition anim = new TranslateTransition(new Duration(1000.0), circle);
anim.setFromX(0);
anim.setToX(200);
anim.setAutoReverse(true);
anim.setCycleCount(Animation.INDEFINITE);
anim.setInterpolator(Interpolator.LINEAR);
Controlling and Monitoring the Transition
The
TranslateTransition
class, as do all of the classes in Table
2-1
earlier, extends the
javafx.animation.
Transition
class, which in turn extends the
Animation
class. Because the
Timeline
class extends the
Animation
class,
as you can see by comparing Listings 2-5 and 2-6, all of the code for the buttons in this example is identical to that in
the previous example. Indeed, the functionality required to start, pause, resume, and stop an animation is defined on
the
Animation
class itself, and inherited by both the
Translation
classes as well as the
Timeline
class.
The MetronomePathTransition Example
As shown in Table
2-1
earlier,
PathTransition
is a transition class that enables you to move a node along
a defined geometric path. Figure
2-10
shows a screenshot of a version of the metronome example, named
MetronomePathTransition, that demonstrates how to use the
PathTransition
class.
Search WWH ::
Custom Search