- Direct Known Subclasses:
Timeline,Transition
Animation provides the core functionality of all animations
used in the JavaFX runtime.
An animation can run in a loop by setting cycleCount.
To make an animation run back and forth while looping, set the
autoReverse -flag.
Call play() or playFromStart() to play an Animation
. The Animation progresses in the direction and speed specified by
rate, and stops when its duration is elapsed. An Animation
with indefinite duration (a cycleCount of INDEFINITE) runs
repeatedly until the stop() method is explicitly called, which will
stop the running Animation and reset its play head to the initial
position.
An Animation can be paused by calling pause(), and the next
play() call will resume the Animation from where it was
paused.
An Animation's play head can be randomly positioned, whether it is
running or not. If the Animation is running, the play head jumps to
the specified position immediately and continues playing from new position.
If the Animation is not running, the next play() will start
the Animation from the specified position.
Inverting the value of rate toggles the play direction.
The animation runs on the JavaFX Application Thread.
- Since:
- JavaFX 2.0
- See Also:
-
Property Summary
PropertiesTypePropertyDescriptionfinal BooleanPropertyDefines whether thisAnimationreverses direction on alternating cycles.final ReadOnlyDoublePropertyRead-only variable to indicate current direction/speed at which theAnimationis being played.final ReadOnlyObjectProperty<Duration> Defines theAnimation's play head position.final IntegerPropertyDefines the number of cycles in this animation.final ReadOnlyObjectProperty<Duration> Read-only variable to indicate the duration of one cycle of thisAnimation: the time it takes to play from time 0 to the end of the Animation (at the defaultrateof 1.0).final ObjectProperty<Duration> Delays the start of an animation.final ObjectProperty<EventHandler<ActionEvent>> The action to be executed at the conclusion of thisAnimation.final DoublePropertyDefines the direction/speed at which theAnimationis expected to be played.The status of theAnimation.final ReadOnlyObjectProperty<Duration> Read-only variable to indicate the total duration of thisAnimation, including repeats. -
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intUsed as a value forcycleCountto specify an animation that repeats indefinitely, until thestop()method is called. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal BooleanPropertyDefines whether thisAnimationreverses direction on alternating cycles.final ReadOnlyDoublePropertyRead-only variable to indicate current direction/speed at which theAnimationis being played.final ReadOnlyObjectProperty<Duration> Defines theAnimation's play head position.final IntegerPropertyDefines the number of cycles in this animation.final ReadOnlyObjectProperty<Duration> Read-only variable to indicate the duration of one cycle of thisAnimation: the time it takes to play from time 0 to the end of the Animation (at the defaultrateof 1.0).final ObjectProperty<Duration> Delays the start of an animation.final ObservableMap<String, Duration> The cue points can be used to mark important positions of theAnimation.final doubleGets the value of thecurrentRateproperty.final DurationGets the value of thecurrentTimeproperty.final intGets the value of thecycleCountproperty.final DurationGets the value of thecycleDurationproperty.final DurationgetDelay()Gets the value of thedelayproperty.final EventHandler<ActionEvent> Gets the value of theonFinishedproperty.final doublegetRate()Gets the value of therateproperty.final Animation.StatusGets the value of thestatusproperty.final doubleThe target framerate is the maximum framerate at which thisAnimationwill run, in frames per second.final DurationGets the value of thetotalDurationproperty.final booleanGets the value of theautoReverseproperty.voidJumps to a predefined position in thisAnimation.voidJumps to a given position in thisAnimation.final ObjectProperty<EventHandler<ActionEvent>> The action to be executed at the conclusion of thisAnimation.voidpause()Pauses the animation.voidplay()PlaysAnimationfrom current position in the direction indicated byrate.voidA convenience method to play thisAnimationfrom a predefined position.voidA convenience method to play thisAnimationfrom a specific position.voidPlays anAnimationfrom initial position in forward direction.final DoublePropertyDefines the direction/speed at which theAnimationis expected to be played.final voidsetAutoReverse(boolean value) Sets the value of theautoReverseproperty.final voidsetCycleCount(int value) Sets the value of thecycleCountproperty.protected final voidsetCycleDuration(Duration value) Sets the value of thecycleDurationproperty.final voidSets the value of thedelayproperty.final voidsetOnFinished(EventHandler<ActionEvent> value) Sets the value of theonFinishedproperty.final voidsetRate(double value) Sets the value of therateproperty.protected final voidsetStatus(Animation.Status value) Sets the value of thestatusproperty.The status of theAnimation.voidstop()Stops the animation and resets the play head to its initial position.final ReadOnlyObjectProperty<Duration> Read-only variable to indicate the total duration of thisAnimation, including repeats.
-
Property Details
-
rate
Defines the direction/speed at which theAnimationis expected to be played.The absolute value of
rateindicates the speed at which theAnimationis to be played, while the sign ofrateindicates the direction. A positive value ofrateindicates forward play, a negative value indicates backward play and0.0to stop a runningAnimation.Rate
1.0is normal play,2.0is 2 time normal,-1.0is backwards, etc.Inverting the rate of a running
Animationwill cause theAnimationto reverse direction in place and play back over the portion of theAnimationthat has already elapsed.- Default value:
- 1.0
- See Also:
-
currentRate
Read-only variable to indicate current direction/speed at which theAnimationis being played.currentRateis not necessarily equal torate.currentRateis set to0.0when animation is paused or stopped.currentRatemay also point in the opposite direction ofrateduring reverse cycles whenautoReverseistrue.- Default value:
- 0.0
- See Also:
-
cycleDuration
Read-only variable to indicate the duration of one cycle of thisAnimation: the time it takes to play from time 0 to the end of the Animation (at the defaultrateof 1.0).- Default value:
- 0ms
- See Also:
-
totalDuration
Read-only variable to indicate the total duration of thisAnimation, including repeats. AnAnimationwith acycleCountofAnimation.INDEFINITEwill have atotalDurationofDuration.INDEFINITE.This is set to cycleDuration * cycleCount.
- Default value:
- 0ms
- See Also:
-
currentTime
Defines theAnimation's play head position.- Default value:
- 0ms
- See Also:
-
delay
Delays the start of an animation. Cannot be negative. Setting to a negative number will result inIllegalArgumentException.- Default value:
- 0ms
- See Also:
-
cycleCount
Defines the number of cycles in this animation. ThecycleCountmay beINDEFINITEfor animations that repeat indefinitely, but must otherwise be > 0.It is not possible to change the
cycleCountof a runningAnimation. If the value ofcycleCountis changed for a runningAnimation, the animation has to be stopped and started again to pick up the new value.- Default value:
- 1
- See Also:
-
autoReverse
Defines whether thisAnimationreverses direction on alternating cycles. Iftrue, theAnimationwill proceed forward on the first cycle, then reverses on the second cycle, and so on. Otherwise, animation will loop such that each cycle proceeds forward from the start. It is not possible to change theautoReverseflag of a runningAnimation. If the value ofautoReverseis changed for a runningAnimation, the animation has to be stopped and started again to pick up the new value.- Default value:
- false
- See Also:
-
status
The status of theAnimation. AnAnimationcan be in one of three states:Animation.Status.STOPPED,Animation.Status.PAUSEDorAnimation.Status.RUNNING.- See Also:
-
onFinished
The action to be executed at the conclusion of thisAnimation.- Default value:
- null
- See Also:
-
-
Field Details
-
INDEFINITE
public static final int INDEFINITEUsed as a value forcycleCountto specify an animation that repeats indefinitely, until thestop()method is called.- See Also:
-
-
Constructor Details
-
Animation
protected Animation(double targetFramerate) The constructor ofAnimation. This constructor allows to define a target framerate.- Parameters:
targetFramerate- The custom target frame rate for thisAnimation- See Also:
-
Animation
protected Animation()The constructor ofAnimation.
-
-
Method Details
-
setRate
public final void setRate(double value) Sets the value of therateproperty.- Property description:
- Defines the direction/speed at which the
Animationis expected to be played.The absolute value of
rateindicates the speed at which theAnimationis to be played, while the sign ofrateindicates the direction. A positive value ofrateindicates forward play, a negative value indicates backward play and0.0to stop a runningAnimation.Rate
1.0is normal play,2.0is 2 time normal,-1.0is backwards, etc.Inverting the rate of a running
Animationwill cause theAnimationto reverse direction in place and play back over the portion of theAnimationthat has already elapsed. - Default value:
- 1.0
- Parameters:
value- the value for therateproperty- See Also:
-
getRate
public final double getRate()Gets the value of therateproperty.- Property description:
- Defines the direction/speed at which the
Animationis expected to be played.The absolute value of
rateindicates the speed at which theAnimationis to be played, while the sign ofrateindicates the direction. A positive value ofrateindicates forward play, a negative value indicates backward play and0.0to stop a runningAnimation.Rate
1.0is normal play,2.0is 2 time normal,-1.0is backwards, etc.Inverting the rate of a running
Animationwill cause theAnimationto reverse direction in place and play back over the portion of theAnimationthat has already elapsed. - Default value:
- 1.0
- Returns:
- the value of the
rateproperty - See Also:
-
rateProperty
Defines the direction/speed at which theAnimationis expected to be played.The absolute value of
rateindicates the speed at which theAnimationis to be played, while the sign ofrateindicates the direction. A positive value ofrateindicates forward play, a negative value indicates backward play and0.0to stop a runningAnimation.Rate
1.0is normal play,2.0is 2 time normal,-1.0is backwards, etc.Inverting the rate of a running
Animationwill cause theAnimationto reverse direction in place and play back over the portion of theAnimationthat has already elapsed.- Default value:
- 1.0
- Returns:
- the
rateproperty - See Also:
-
getCurrentRate
public final double getCurrentRate()Gets the value of thecurrentRateproperty.- Property description:
- Read-only variable to indicate current direction/speed at which the
Animationis being played.currentRateis not necessarily equal torate.currentRateis set to0.0when animation is paused or stopped.currentRatemay also point in the opposite direction ofrateduring reverse cycles whenautoReverseistrue. - Default value:
- 0.0
- Returns:
- the value of the
currentRateproperty - See Also:
-
currentRateProperty
Read-only variable to indicate current direction/speed at which theAnimationis being played.currentRateis not necessarily equal torate.currentRateis set to0.0when animation is paused or stopped.currentRatemay also point in the opposite direction ofrateduring reverse cycles whenautoReverseistrue.- Default value:
- 0.0
- Returns:
- the
currentRateproperty - See Also:
-
setCycleDuration
Sets the value of thecycleDurationproperty.- Property description:
- Read-only variable to indicate the duration of one cycle of this
Animation: the time it takes to play from time 0 to the end of the Animation (at the defaultrateof 1.0). - Default value:
- 0ms
- Parameters:
value- the value for thecycleDurationproperty- See Also:
-
getCycleDuration
Gets the value of thecycleDurationproperty.- Property description:
- Read-only variable to indicate the duration of one cycle of this
Animation: the time it takes to play from time 0 to the end of the Animation (at the defaultrateof 1.0). - Default value:
- 0ms
- Returns:
- the value of the
cycleDurationproperty - See Also:
-
cycleDurationProperty
Read-only variable to indicate the duration of one cycle of thisAnimation: the time it takes to play from time 0 to the end of the Animation (at the defaultrateof 1.0).- Default value:
- 0ms
- Returns:
- the
cycleDurationproperty - See Also:
-
getTotalDuration
Gets the value of thetotalDurationproperty.- Property description:
- Read-only variable to indicate the total duration of this
Animation, including repeats. AnAnimationwith acycleCountofAnimation.INDEFINITEwill have atotalDurationofDuration.INDEFINITE.This is set to cycleDuration * cycleCount.
- Default value:
- 0ms
- Returns:
- the value of the
totalDurationproperty - See Also:
-
totalDurationProperty
Read-only variable to indicate the total duration of thisAnimation, including repeats. AnAnimationwith acycleCountofAnimation.INDEFINITEwill have atotalDurationofDuration.INDEFINITE.This is set to cycleDuration * cycleCount.
- Default value:
- 0ms
- Returns:
- the
totalDurationproperty - See Also:
-
getCurrentTime
Gets the value of thecurrentTimeproperty.- Property description:
- Defines the
Animation's play head position. - Default value:
- 0ms
- Returns:
- the value of the
currentTimeproperty - See Also:
-
currentTimeProperty
Defines theAnimation's play head position.- Default value:
- 0ms
- Returns:
- the
currentTimeproperty - See Also:
-
setDelay
Sets the value of thedelayproperty.- Property description:
- Delays the start of an animation.
Cannot be negative. Setting to a negative number will result in
IllegalArgumentException. - Default value:
- 0ms
- Parameters:
value- the value for thedelayproperty- See Also:
-
getDelay
Gets the value of thedelayproperty.- Property description:
- Delays the start of an animation.
Cannot be negative. Setting to a negative number will result in
IllegalArgumentException. - Default value:
- 0ms
- Returns:
- the value of the
delayproperty - See Also:
-
delayProperty
Delays the start of an animation. Cannot be negative. Setting to a negative number will result inIllegalArgumentException.- Default value:
- 0ms
- Returns:
- the
delayproperty - See Also:
-
setCycleCount
public final void setCycleCount(int value) Sets the value of thecycleCountproperty.- Property description:
- Defines the number of cycles in this animation. The
cycleCountmay beINDEFINITEfor animations that repeat indefinitely, but must otherwise be > 0.It is not possible to change the
cycleCountof a runningAnimation. If the value ofcycleCountis changed for a runningAnimation, the animation has to be stopped and started again to pick up the new value. - Default value:
- 1
- Parameters:
value- the value for thecycleCountproperty- See Also:
-
getCycleCount
public final int getCycleCount()Gets the value of thecycleCountproperty.- Property description:
- Defines the number of cycles in this animation. The
cycleCountmay beINDEFINITEfor animations that repeat indefinitely, but must otherwise be > 0.It is not possible to change the
cycleCountof a runningAnimation. If the value ofcycleCountis changed for a runningAnimation, the animation has to be stopped and started again to pick up the new value. - Default value:
- 1
- Returns:
- the value of the
cycleCountproperty - See Also:
-
cycleCountProperty
Defines the number of cycles in this animation. ThecycleCountmay beINDEFINITEfor animations that repeat indefinitely, but must otherwise be > 0.It is not possible to change the
cycleCountof a runningAnimation. If the value ofcycleCountis changed for a runningAnimation, the animation has to be stopped and started again to pick up the new value.- Default value:
- 1
- Returns:
- the
cycleCountproperty - See Also:
-
setAutoReverse
public final void setAutoReverse(boolean value) Sets the value of theautoReverseproperty.- Property description:
- Defines whether this
Animationreverses direction on alternating cycles. Iftrue, theAnimationwill proceed forward on the first cycle, then reverses on the second cycle, and so on. Otherwise, animation will loop such that each cycle proceeds forward from the start. It is not possible to change theautoReverseflag of a runningAnimation. If the value ofautoReverseis changed for a runningAnimation, the animation has to be stopped and started again to pick up the new value. - Default value:
- false
- Parameters:
value- the value for theautoReverseproperty- See Also:
-
isAutoReverse
public final boolean isAutoReverse()Gets the value of theautoReverseproperty.- Property description:
- Defines whether this
Animationreverses direction on alternating cycles. Iftrue, theAnimationwill proceed forward on the first cycle, then reverses on the second cycle, and so on. Otherwise, animation will loop such that each cycle proceeds forward from the start. It is not possible to change theautoReverseflag of a runningAnimation. If the value ofautoReverseis changed for a runningAnimation, the animation has to be stopped and started again to pick up the new value. - Default value:
- false
- Returns:
- the value of the
autoReverseproperty - See Also:
-
autoReverseProperty
Defines whether thisAnimationreverses direction on alternating cycles. Iftrue, theAnimationwill proceed forward on the first cycle, then reverses on the second cycle, and so on. Otherwise, animation will loop such that each cycle proceeds forward from the start. It is not possible to change theautoReverseflag of a runningAnimation. If the value ofautoReverseis changed for a runningAnimation, the animation has to be stopped and started again to pick up the new value.- Default value:
- false
- Returns:
- the
autoReverseproperty - See Also:
-
setStatus
Sets the value of thestatusproperty.- Property description:
- The status of the
Animation. AnAnimationcan be in one of three states:Animation.Status.STOPPED,Animation.Status.PAUSEDorAnimation.Status.RUNNING. - Parameters:
value- the value for thestatusproperty- See Also:
-
getStatus
Gets the value of thestatusproperty.- Property description:
- The status of the
Animation. AnAnimationcan be in one of three states:Animation.Status.STOPPED,Animation.Status.PAUSEDorAnimation.Status.RUNNING. - Returns:
- the value of the
statusproperty - See Also:
-
statusProperty
The status of theAnimation. AnAnimationcan be in one of three states:Animation.Status.STOPPED,Animation.Status.PAUSEDorAnimation.Status.RUNNING.- Returns:
- the
statusproperty - See Also:
-
setOnFinished
Sets the value of theonFinishedproperty.- Property description:
- The action to be executed at the conclusion of this
Animation. - Default value:
- null
- Parameters:
value- the value for theonFinishedproperty- See Also:
-
getOnFinished
Gets the value of theonFinishedproperty.- Property description:
- The action to be executed at the conclusion of this
Animation. - Default value:
- null
- Returns:
- the value of the
onFinishedproperty - See Also:
-
onFinishedProperty
The action to be executed at the conclusion of thisAnimation.- Default value:
- null
- Returns:
- the
onFinishedproperty - See Also:
-
getCuePoints
The cue points can be used to mark important positions of theAnimation. Once a cue point was defined, it can be used as an argument ofjumpTo()andplayFrom()to move to the associated position quickly.Every
Animationhas two predefined cue points"start"and"end", which are set at the start respectively the end of theAnimation. The predefined cuepoints do not appear in the map, attempts to override them have no effect.Another option to define a cue point in a
Animationis to set thenameproperty of aKeyFrame.- Returns:
ObservableMapof cue points
-
jumpTo
Jumps to a given position in thisAnimation. If the given time is less thanDuration.ZERO, this method will jump to the start of the animation. If the given time is larger than the duration of thisAnimation, this method will jump to the end.- Parameters:
time- the new position- Throws:
NullPointerException- iftimeisnullIllegalArgumentException- iftimeisDuration.UNKNOWNIllegalStateException- if embedded in another animation, such asSequentialTransitionorParallelTransition
-
jumpTo
Jumps to a predefined position in thisAnimation. This method looks for an entry in cue points and jumps to the associated position, if it finds one.If the cue point is behind the end of this
Animation, callingjumpTowill result in a jump to the end. If the cue point has a negativeDurationit will result in a jump to the beginning. If the cue point has a value ofDuration.UNKNOWNcallingjumpTowill have no effect for this cue point.There are two predefined cue points
"start"and"end"which are defined to be at the start respectively the end of thisAnimation.- Parameters:
cuePoint- the name of the cue point- Throws:
NullPointerException- ifcuePointisnullIllegalStateException- if embedded in another animation, such asSequentialTransitionorParallelTransition- See Also:
-
playFrom
A convenience method to play thisAnimationfrom a predefined position. The position has to be predefined in cue points. Calling this method is equivalent to
Note that unlikeanimation.jumpTo(cuePoint); animation.play();playFromStart()calling this method will not change the playing direction of thisAnimation.Note: if this method is not called on the JavaFX Application Thread, it is delegated to it automatically. In this case, the call is asynchronous and may not happen immediately.
- Parameters:
cuePoint- name of the cue point- Throws:
NullPointerException- ifcuePointisnullIllegalStateException- if embedded in another animation, such asSequentialTransitionorParallelTransition- See Also:
-
playFrom
A convenience method to play thisAnimationfrom a specific position. Calling this method is equivalent to
Note that unlikeanimation.jumpTo(time); animation.play();playFromStart()calling this method will not change the playing direction of thisAnimation.Note: if this method is not called on the JavaFX Application Thread, it is delegated to it automatically. In this case, the call is asynchronous and may not happen immediately.
- Parameters:
time- position where to play from- Throws:
NullPointerException- iftimeisnullIllegalArgumentException- iftimeisDuration.UNKNOWNIllegalStateException- if embedded in another animation, such asSequentialTransitionorParallelTransition
-
playFromStart
public void playFromStart()Plays anAnimationfrom initial position in forward direction.It is equivalent to
animation.stop();
animation.setRate = setRate(Math.abs(animation.getRate()));
animation.jumpTo(Duration.ZERO);
animation.play();
Note: if this method is not called on the JavaFX Application Thread, it is delegated to it automatically. In this case, the call is asynchronous and may not happen immediately.
- Throws:
IllegalStateException- if embedded in another animation, such asSequentialTransitionorParallelTransition
-
play
public void play()PlaysAnimationfrom current position in the direction indicated byrate. If theAnimationis running, it has no effect.When
rate> 0 (forward play), if anAnimationis already positioned at the end, the first cycle will not be played, it is considered to have already finished. This also applies to a backward (rate< 0) cycle if anAnimationis positioned at the beginning. However, if theAnimationhascycleCount> 1, following cycle(s) will be played as usual.When the
Animationreaches the end, theAnimationis stopped and the play head remains at the end.To play an
Animationbackwards from the end:
animation.setRate(negative rate);
animation.jumpTo(overall duration of animation);
animation.play();
Note: if this method is not called on the JavaFX Application Thread, it is delegated to it automatically. In this case, the call is asynchronous and may not happen immediately.
- Throws:
IllegalStateException- if embedded in another animation, such asSequentialTransitionorParallelTransition
-
stop
public void stop()Stops the animation and resets the play head to its initial position. If the animation is already stopped, this method has no effect.Note: if this method is not called on the JavaFX Application Thread, it is delegated to it automatically. In this case, the call is asynchronous and may not happen immediately.
- Throws:
IllegalStateException- if embedded in another animation, such asSequentialTransitionorParallelTransition
-
pause
public void pause()Pauses the animation. If the animation is not currently running, this method has no effect.Note: if this method is not called on the JavaFX Application Thread, it is delegated to it automatically. In this case, the call is asynchronous and may not happen immediately.
- Throws:
IllegalStateException- if embedded in another animation, such asSequentialTransitionorParallelTransition
-
getTargetFramerate
public final double getTargetFramerate()The target framerate is the maximum framerate at which thisAnimationwill run, in frames per second. This can be used, for example, to keep particularly complexAnimationsfrom over-consuming system resources. By default, anAnimation's framerate is not explicitly limited, meaning theAnimationwill run at an optimal framerate for the underlying platform.- Returns:
- the target framerate
-