public abstract class Animation extends Object
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.
Timeline,
Transition| Modifier and Type | Class and Description |
|---|---|
static class |
Animation.Status
The possible states for
status. |
| Modifier and Type | Field and Description |
|---|---|
static int |
INDEFINITE
Used to specify an animation that repeats indefinitely, until the
stop() method is called. |
| Modifier | Constructor and Description |
|---|---|
protected |
Animation()
The constructor of
Animation. |
protected |
Animation(double targetFramerate)
The constructor of
Animation. |
| Modifier and Type | Method and Description |
|---|---|
BooleanProperty |
autoReverseProperty() |
ReadOnlyDoubleProperty |
currentRateProperty() |
ReadOnlyObjectProperty<Duration> |
currentTimeProperty() |
IntegerProperty |
cycleCountProperty() |
ReadOnlyObjectProperty<Duration> |
cycleDurationProperty() |
ObjectProperty<Duration> |
delayProperty() |
ObservableMap<String,Duration> |
getCuePoints()
The cue points can be
used to mark important positions of the
Animation. |
double |
getCurrentRate() |
Duration |
getCurrentTime() |
int |
getCycleCount() |
Duration |
getCycleDuration() |
Duration |
getDelay() |
EventHandler<ActionEvent> |
getOnFinished() |
double |
getRate() |
Animation.Status |
getStatus() |
double |
getTargetFramerate()
The target framerate is the maximum framerate at which this
Animation
will run, in frames per second. |
Duration |
getTotalDuration() |
boolean |
isAutoReverse() |
void |
jumpTo(Duration time)
Jumps to a given position in this
Animation. |
void |
jumpTo(String cuePoint)
Jumps to a predefined position in this
Animation. |
ObjectProperty<EventHandler<ActionEvent>> |
onFinishedProperty() |
void |
pause()
Pauses the animation.
|
void |
play()
Plays
Animation from current position in the direction indicated
by rate. |
void |
playFrom(Duration time)
A convenience method to play this
Animation from a specific
position. |
void |
playFrom(String cuePoint)
A convenience method to play this
Animation from a predefined
position. |
void |
playFromStart()
Plays an
Animation from initial position in forward direction. |
DoubleProperty |
rateProperty() |
void |
setAutoReverse(boolean value) |
void |
setCycleCount(int value) |
protected void |
setCycleDuration(Duration value) |
void |
setDelay(Duration value) |
void |
setOnFinished(EventHandler<ActionEvent> value) |
void |
setRate(double value) |
protected void |
setStatus(Animation.Status value) |
ReadOnlyObjectProperty<Animation.Status> |
statusProperty() |
void |
stop()
Stops the animation and resets the play head to its initial position.
|
ReadOnlyObjectProperty<Duration> |
totalDurationProperty() |
public static final int INDEFINITE
stop() method is called.protected Animation(double targetFramerate)
Animation.
This constructor allows to define a target framerate.targetFramerate - The custom target frame rate for this AnimationgetTargetFramerate()protected Animation()
Animation.public final void setRate(double value)
public final double getRate()
public final DoubleProperty rateProperty()
public final double getCurrentRate()
public final ReadOnlyDoubleProperty currentRateProperty()
protected final void setCycleDuration(Duration value)
public final Duration getCycleDuration()
public final ReadOnlyObjectProperty<Duration> cycleDurationProperty()
public final Duration getTotalDuration()
public final ReadOnlyObjectProperty<Duration> totalDurationProperty()
public final Duration getCurrentTime()
public final ReadOnlyObjectProperty<Duration> currentTimeProperty()
public final void setDelay(Duration value)
public final Duration getDelay()
public final ObjectProperty<Duration> delayProperty()
public final void setCycleCount(int value)
public final int getCycleCount()
public final IntegerProperty cycleCountProperty()
public final void setAutoReverse(boolean value)
public final boolean isAutoReverse()
public final BooleanProperty autoReverseProperty()
protected final void setStatus(Animation.Status value)
public final Animation.Status getStatus()
public final ReadOnlyObjectProperty<Animation.Status> statusProperty()
public final double getTargetFramerate()
Animation
will run, in frames per second. This can be used, for example, to keep
particularly complex Animations from over-consuming system resources.
By default, an Animation's framerate is not explicitly limited, meaning
the Animation will run at an optimal framerate for the underlying platform.public final void setOnFinished(EventHandler<ActionEvent> value)
public final EventHandler<ActionEvent> getOnFinished()
public final ObjectProperty<EventHandler<ActionEvent>> onFinishedProperty()
public final ObservableMap<String,Duration> getCuePoints()
Animation. Once a cue
point was defined, it can be used as an argument of
jumpTo() and playFrom()
to move to the associated position quickly.
Every Animation has two predefined cue points "start" and
"end", which are set at the start respectively the end of the
Animation. 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 Animation is to set the
KeyFrame.name property of a KeyFrame.
ObservableMap of cue pointspublic void jumpTo(Duration time)
Animation.
If the given time is less than Duration.ZERO, this method will
jump to the start of the animation. If the given time is larger than the
duration of this Animation, this method will jump to the end.time - the new positionNullPointerException - if time is nullIllegalArgumentException - if time is Duration.UNKNOWNIllegalStateException - if embedded in another animation,
such as SequentialTransition or ParallelTransitionpublic void jumpTo(String cuePoint)
Animation. 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, calling
jumpTo will result in a jump to the end. If the cue point has a
negative Duration it will result in a jump to the
beginning. If the cue point has a value of
Duration.UNKNOWN calling jumpTo will 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 this
Animation.
cuePoint - the name of the cue pointNullPointerException - if cuePoint is nullIllegalStateException - if embedded in another animation,
such as SequentialTransition or ParallelTransitiongetCuePoints()public void playFrom(String cuePoint)
Animation from a predefined
position. The position has to be predefined in cue points.
Calling this method is equivalent to
animation.jumpTo(cuePoint);
animation.play();
Note that unlike playFromStart() calling this method will not
change the playing direction of this Animation.cuePoint - name of the cue pointNullPointerException - if cuePoint is nullIllegalStateException - if embedded in another animation,
such as SequentialTransition or ParallelTransitiongetCuePoints()public void playFrom(Duration time)
Animation from a specific
position. Calling this method is equivalent to
animation.jumpTo(time);
animation.play();
Note that unlike playFromStart() calling this method will not
change the playing direction of this Animation.time - position where to play fromNullPointerException - if time is nullIllegalArgumentException - if time is Duration.UNKNOWNIllegalStateException - if embedded in another animation,
such as SequentialTransition or ParallelTransitionpublic void play()
Animation from current position in the direction indicated
by rate. If the Animation is running, it has no effect.
When rate > 0 (forward play), if an Animation is 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 an Animation is positioned at the beginning.
However, if the Animation has cycleCount > 1, following
cycle(s) will be played as usual.
When the Animation reaches the end, the Animation is stopped and
the play head remains at the end.
To play an Animation backwards from the end:
animation.setRate(negative rate);
animation.jumpTo(overall duration of animation);
animation.play();
Note:
play() is an asynchronous call, the Animation may not
start immediately. IllegalStateException - if embedded in another animation,
such as SequentialTransition or ParallelTransitionpublic void playFromStart()
Animation from 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:
playFromStart() is an asynchronous call, Animation may
not start immediately. IllegalStateException - if embedded in another animation,
such as SequentialTransition or ParallelTransitionpublic void stop()
Note:
stop() is an asynchronous call, the Animation may not stop
immediately. IllegalStateException - if embedded in another animation,
such as SequentialTransition or ParallelTransitionpublic void pause()
Note:
pause() is an asynchronous call, the Animation may not pause
immediately. IllegalStateException - if embedded in another animation,
such as SequentialTransition or ParallelTransitionCopyright © 2025. All rights reserved.