Java Reference
In-Depth Information
based on where the mouse is located, you use a fade transition
(
javafx.animation.FadeTransition
).
Before I begin to discuss the steps to fulfill the requirements, I want to mention the
basics of JavaFX animation. The JavaFX animation API allows you to assemble timed
events that can interpolate over a node's attribute values to produce animated effects.
Each timed event is called a keyframe (
KeyFrame
), and it's responsible for interpolat-
ing over a node's property over a period of time (
javafx.util.Duration
).
Knowing that a keyframe's job is to operate on a node's property value, you have to
create an instance of a
KeyValue
class that will reference the desired node property.
The idea of interpolation is simply the distributing of values between a start and end
value. An example is to move a rectangle by its current x position (zero) to 100 pixels
in 1,000 milliseconds; in other words, move the rectangle 100 pixels to the right during
one second. Shown here is a keyframe and key value to interpolate a rectangle's x
property for 1,000 milliseconds:
final Rectangle rectangle = new Rectangle(0, 0, 50, 50);
KeyValue keyValue = new KeyValue(rectangle.xProperty(),
100);
KeyFrame keyFrame = new KeyFrame(Duration.millis(1000),
keyValue);
When creating many keyframes that are assembled consecutively, you need to cre-
ate a timeline. Because timeline is a subclass of
javafx.animation.Animation
, there are standard attributes—such as its cycle
count and auto-reverse—that you can set. The
cycle count
is the number of times you
want the timeline to play the animation. If you want the cycle count to play the anima-
tion indefinitely, use the value
Timeline.INDEFINITE
. The auto-reverse is the
capability for the animation to play the timeline backward. By default, the cycle count
is set to
1
, and the auto-reverse is set to
false
. When adding keyframes you simply
add them using the
getKeyFrames().add()
method on the
TimeLine
object.
The following code snippet demonstrates a timeline playing indefinitely with auto-re-
verse set to
true
:
Timeline timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
timeline.getKeyFrames().add(keyFrame);
timeline.play();