- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
JavaFX animation
In this chapter, we work with animation in JavaFX. We create animation using AnimationTimer
, Transition
, and Timeline
.
Animation is a rapid succession of images which make an illusion of movement. Animation is not restricted to movement, however. Changing the background of a node over time is considered an animation too.
JavaFX provides three basic tools to create animation:
- AnimationTimer
- Transition
- Timeline
AnimationTimer
is the most simple tool to create animation. It is a basic timer; its handle()
method is called in every frame of the animation. Transition
is a basic high-level framework to define animation. The animation is controlled with the frac
value of the interpolate()
method. Timeline
is the most complex tool for doing high-level animations. Timeline
animation is defined with KeyFrames
, which outline target values of a node at a specified point in time for a set of variables that are interpolated along a Timeline
. The animated properties are defined with KeyValues
.
Animation class
Animation
is the basic class in JavaFX to define high-level animation. Both Transition
and Timeline
extend Animation
. An animation is started with play()
or playFromStart()
methods and ended with the stop()
method. An animation can be paused by calling the pause()
method, and the next play()
call resumes the animation from where it was paused. The rate
property defines the direction and speed at which the animation is expected to be played. The delay
property specifies the amount of initial delay of the animation. An animation can run in cycles; the number of cycles is defined in the cycleCount
property, and the cycleDuration
indicates the duration of a cycle. An animation can be reversed on alternating cycles with the autoReverseProperty
.
AnimationTimer
AnimationTimer
allows to create a timer that is called in each frame while it is active. It is an abstract class; therefore, we need to create a custom class which extends it. Its handle()
method, which is called in every frame, must be overridden. The AnimationTimer's
start()
method starts the timer and the stop()
method stops it.
AnimationTimerEx.java
package com.zetcode; import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.scene.text.Font; import javafx.stage.Stage; /** * ZetCode JavaFX tutorial * * This program uses AnimationTimer to * create an animation. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class AnimationTimerEx extends Application { private double opacity = 1; private Label lbl; @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { StackPane root = new StackPane(); lbl = new Label("JavaFX"); lbl.setFont(Font.font(48)); root.getChildren().add(lbl); AnimationTimer timer = new MyTimer(); timer.start(); Scene scene = new Scene(root, 300, 250); stage.setTitle("AnimationTimer"); stage.setScene(scene); stage.show(); } private class MyTimer extends AnimationTimer { @Override public void handle(long now) { doHandle(); } private void doHandle() { opacity -= 0.01; lbl.opacityProperty().set(opacity); if (opacity <= 0) { stop(); System.out.println("Animation stopped"); } } } public static void main(String[] args) { launch(args); } }
The example uses the AnimationTimer
to create a fade out effect on a node.
lbl = new Label("JavaFX"); lbl.setFont(Font.font(48)); root.getChildren().add(lbl);
Our animation changes the property of this Label
control.
AnimationTimer timer = new MyTimer(); timer.start();
An AnimationTimer
is created and its start()
method is called.
private class MyTimer extends AnimationTimer { @Override public void handle(long now) { doHandle(); } ... }
We create a concrete subclass of the AnimationTimer
and override its handle()
method.
private void doHandle() { opacity -= 0.01; lbl.opacityProperty().set(opacity); if (opacity <= 0) { stop(); System.out.println("Animation stopped"); } }
In the doHandle()
method, we decrease the opacity
variable and update the opacityProperty
. If the opacity
reaches its minimum value, the timer is stopped with its stop()
method.
FadeTransition
Transition
animation is best suited for a planned animation. Transition
has concrete classes that can be used to create various animations that can be executed in parallel or sequentially; for instance FadeTransition
, PathTransition
, RotateTransition
, or ScaleTransition
.
FadeTransition
creates a fade effect animation that spans its duration. This is done by updating the opacity
variable of the node at regular interval.
FadeTransitionEx.java
package com.zetcode; import javafx.animation.Animation; import javafx.animation.FadeTransition; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; /** * ZetCode JavaFX tutorial * * This program uses a FadeTransition. A rectangle * fades out after we click into its area. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class FadeTransitionEx extends Application { private FadeTransition ft; private Rectangle rect; @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { Group root = new Group(); rect = new Rectangle(20, 20, 150, 150); rect.setOnMouseClicked(new RectClickHandler()); ft = new FadeTransition(Duration.millis(5000), rect); ft.setFromValue(1.0); ft.setToValue(0.0); root.getChildren().add(rect); Scene scene = new Scene(root, 300, 250); stage.setTitle("Fading transition"); stage.setScene(scene); stage.show(); } private class RectClickHandler implements EventHandler<MouseEvent> { @Override public void handle(MouseEvent event) { doHandle(); } private void doHandle() { Double opa = rect.getOpacity(); if (opa.intValue() == 0) { return; } Animation.Status as = ft.getStatus(); if (as == Animation.Status.RUNNING) { return; } if (as == Animation.Status.STOPPED) { ft.play(); } } } public static void main(String[] args) { Application.launch(args); } }
This example uses FadeTransition
to create a fade out effect on a rectangle. The animation is started after a mouse click inside the area of the rectangle.
rect = new Rectangle(20, 20, 150, 150); rect.setOnMouseClicked(new RectClickHandler());
A mouse clicked handler is set to the rectangle.
ft = new FadeTransition(Duration.millis(5000), rect);
A FadeTransition
is created. Its first parameter is the duration of the transition. The second parameter is the node whose opacity
parameter is updated.
ft.setFromValue(1.0); ft.setToValue(0.0);
The setFromValue()
sets the start value of the opacity, and the setToValue()
sets the ending opacity value.
Double opa = rect.getOpacity();
The current opacity value is determined with the getOpacity()
method.
if (opa.intValue() == 0) { return; }
After the rectangle fades out, we dismiss the mouse click.
Animation.Status as = ft.getStatus(); if (as == Animation.Status.RUNNING) { return; } if (as == Animation.Status.STOPPED) { ft.play(); }
The getStatus()
method determines the transition's status. If the status is Animation.Status.STOPPED
, we start the transition with the play()
method.
PathTransition
PathTransition
creates an animation along a path. The translation along the path is done by updating the translateX
and translateY
variables of the node. Note that we must use a node that supports absolute positioning of elements.
PathTransitionEx.java
package com.zetcode; import javafx.animation.PathTransition; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.CubicCurveTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage; import javafx.util.Duration; /** * ZetCode JavaFX tutorial * * This program uses a PathTransition to move * a circle along a path. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class PathTransitionEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { Pane root = new Pane(); Path path = new Path(); path.getElements().add(new MoveTo(20, 120)); path.getElements().add(new CubicCurveTo(180, 60, 250, 340, 420, 240)); Circle circle = new Circle(20, 120, 10); circle.setFill(Color.CADETBLUE); PathTransition ptr = new PathTransition(); ptr.setDuration(Duration.seconds(6)); ptr.setDelay(Duration.seconds(2)); ptr.setPath(path); ptr.setNode(circle); ptr.setCycleCount(2); ptr.setAutoReverse(true); ptr.play(); root.getChildren().addAll(path, circle); Scene scene = new Scene(root, 450, 300); stage.setTitle("PathTransition"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The example uses a PathTransition
to move a circle along a path. The animation starts after an initial delay of 2 seconds. It consists of two cycles. The animation is reveresed; that is, the circle goes from the starting point to the ending point and then it returns back.
Pane root = new Pane();
We use the Pane
as our root node. It supports absolute positioning that is needed for animation.
Path path = new Path(); path.getElements().add(new MoveTo(20, 120)); path.getElements().add(new CubicCurveTo(180, 60, 250, 340, 420, 240));
Here we define the Path
along which the animated object will be moving.
Circle circle = new Circle(20, 120, 10); circle.setFill(Color.CADETBLUE);
This circle is the moving object in our animation.
PathTransition ptr = new PathTransition();
A PathTransition
object is created.
ptr.setDuration(Duration.seconds(6));
The setDuration()
method sets the duration of the animation.
ptr.setDelay(Duration.seconds(2));
The setDelay()
method sets the initial delay of the animation.
ptr.setPath(path); ptr.setNode(circle);
The setPath()
method sets the path, and the setNode()
sets the target node of the animation.
ptr.setCycleCount(2);
Our animation has two cycles. The number of cycles is set with the setCycleCount()
method.
ptr.setAutoReverse(true);
With the setAutoReverse()
method, we reverse the direction of the animation. The circle moves back to the starting position.
ptr.play();
Finally, the play()
method starts the plays the animation.

ParallelTransition
ParallelTransition
plays a list of Animations
in parallel.
ParallelTransitionEx.java
package com.zetcode; import javafx.animation.FillTransition; import javafx.animation.ParallelTransition; import javafx.animation.RotateTransition; import javafx.animation.ScaleTransition; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; /** * ZetCode JavaFX tutorial * * This program creates a parallel * transition animation. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class ParallelTransitionEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { Pane root = new Pane(); Rectangle rect = new Rectangle(50, 50, 30, 30); rect.setArcHeight(10); rect.setArcWidth(10); rect.setFill(Color.CADETBLUE); RotateTransition rottr = new RotateTransition(Duration.millis(2000), rect); rottr.setByAngle(180); rottr.setCycleCount(2); rottr.setAutoReverse(true); ScaleTransition sctr = new ScaleTransition(Duration.millis(2000), rect); sctr.setByX(2); sctr.setByY(2); sctr.setCycleCount(2); sctr.setAutoReverse(true); FillTransition fltr = new FillTransition(Duration.millis(2000), rect, Color.CADETBLUE, Color.STEELBLUE); fltr.setCycleCount(2); fltr.setAutoReverse(true); root.getChildren().add(rect); ParallelTransition ptr = new ParallelTransition(); ptr.getChildren().addAll(rottr, sctr, fltr); ptr.play(); Scene scene = new Scene(root, 300, 250); stage.setTitle("ParallelTransition"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The example plays three transitions in parallel. There is a rectangle object that is rotated, scaled, and its background colour changes.
RotateTransition rottr = new RotateTransition(Duration.millis(2000), rect); rottr.setByAngle(180); rottr.setCycleCount(2); rottr.setAutoReverse(true);
The RotateTransition
rotates the rectangle by the specified angle. The rotation happens in two cycles and is reversed.
ScaleTransition sctr = new ScaleTransition(Duration.millis(2000), rect); sctr.setByX(2); sctr.setByY(2);
The ScaleTransition
scales the rectangle up and down by the factor of 2.
FillTransition fltr = new FillTransition(Duration.millis(2000), rect, Color.CADETBLUE, Color.STEELBLUE);
The FillTransition
changes the filling colour of the rectangle from one colour value to another.
ParallelTransition ptr = new ParallelTransition(); ptr.getChildren().addAll(rottr, sctr, fltr); ptr.play();
The three types of transitions are placed in the ParallelTransition
, which plays them in parallel, that is, at the same time.
SequentialTransition
SequentialTransition
plays a list of Animations
in a sequential order.
SequentialTransitionEx.java
package com.zetcode; import javafx.animation.FillTransition; import javafx.animation.RotateTransition; import javafx.animation.ScaleTransition; import javafx.animation.SequentialTransition; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; /** * ZetCode JavaFX tutorial * * This program plays three transitions in * a sequential order. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class SequentialTransitionEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { Pane root = new Pane(); Rectangle rect = new Rectangle(50, 50, 30, 30); rect.setArcHeight(10); rect.setArcWidth(10); rect.setFill(Color.CADETBLUE); RotateTransition rottr = new RotateTransition(Duration.millis(2000), rect); rottr.setByAngle(180); rottr.setCycleCount(2); rottr.setAutoReverse(true); ScaleTransition sctr = new ScaleTransition(Duration.millis(2000), rect); sctr.setByX(2); sctr.setByY(2); sctr.setCycleCount(2); sctr.setAutoReverse(true); FillTransition fltr = new FillTransition(Duration.millis(2000), rect, Color.CADETBLUE, Color.STEELBLUE); fltr.setCycleCount(2); fltr.setAutoReverse(true); root.getChildren().add(rect); SequentialTransition str = new SequentialTransition(); str.getChildren().addAll(rottr, sctr, fltr); str.play(); Scene scene = new Scene(root, 300, 250); stage.setTitle("SequentialTransition"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The example plays three transitions in a sequential order—one after another.
SequentialTransition str = new SequentialTransition(); str.getChildren().addAll(rottr, sctr, fltr); str.play();
The three transitions are added to the SequentialTransition
.
Timeline
Timeline
is the most involved tool to create animation in JavaFX. The animation is defined with KeyFrames
which contain the properties of nodes that change. These properties are encapsulated in KeyValues
. The Timeline
interpolates the changes of the properties.
TimelineEx.java
package com.zetcode; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.effect.Lighting; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; /** * ZetCode JavaFX tutorial * * This program uses a Timeline to * move a rectangle. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class TimelineEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { Pane root = new Pane(); Rectangle rect = new Rectangle(20, 20, 60, 60); rect.setEffect(new Lighting()); rect.setFill(Color.CADETBLUE); Timeline tl = new Timeline(); tl.setCycleCount(2); tl.setAutoReverse(true); KeyValue kv = new KeyValue(rect.translateXProperty(), 200); KeyFrame kf = new KeyFrame(Duration.millis(2000), kv); tl.getKeyFrames().addAll(kf); tl.play(); root.getChildren().addAll(rect); Scene scene = new Scene(root, 350, 250); stage.setTitle("Timeline"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The example animates a rectangle with a Timeline
.
Rectangle rect = new Rectangle(20, 20, 60, 60); rect.setEffect(new Lighting()); rect.setFill(Color.CADETBLUE);
This rectangle is the moving object in the animation.
Timeline tl = new Timeline();
A Timeline
object is created.
tl.setCycleCount(2); tl.setAutoReverse(true);
The animation consists of two cycles and is reversed. The rectangle moves forth and back.
KeyValue kv = new KeyValue(rect.translateXProperty(), 200);
The KeyValue
contains the translateX
property that changes over time to 200.
KeyFrame kf = new KeyFrame(Duration.millis(2000), kv);
A KeyFrame
is instantiated. The first parameter is its duration, the second is the KeyValue
. The animation lasts 2 seconds, during which its translateX
property changes to 200.
tl.getKeyFrames().addAll(kf);
The key frame is added to the list of frames.
Sequential Timeline animation
We do not define all the key frames in a timeline. We define some frames and the remaining ones are interpolated. Key frames provide target values at a specified point in time for a set of variables interpolated in a timeline. In order to execute key frames sequentially, we utilize the SequentialTransition
class.
SequentialTimelineEx.java
package com.zetcode; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.SequentialTransition; import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; /** * ZetCode JavaFX tutorial * * This program creates a sequential Timeline * animation. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class SequentialTimelineEx extends Application { @Override public void start(Stage stage) { initUI(stage); } private void initUI(Stage stage) { Pane root = new Pane(); Circle c = new Circle(50, 100, 10); c.setFill(Color.CADETBLUE); KeyValue kv1 = new KeyValue(c.scaleXProperty(), 4); KeyValue kv2 = new KeyValue(c.scaleYProperty(), 4); KeyFrame kf1 = new KeyFrame(Duration.millis(3000), kv1, kv2); Timeline scale = new Timeline(); scale.getKeyFrames().add(kf1); KeyValue kv3 = new KeyValue(c.centerXProperty(), 250); KeyFrame kf2 = new KeyFrame(Duration.millis(5000), kv3); Timeline move = new Timeline(); move.getKeyFrames().add(kf2); KeyValue kv4 = new KeyValue(c.scaleXProperty(), 1); KeyValue kv5 = new KeyValue(c.scaleYProperty(), 1); KeyFrame kf3 = new KeyFrame(Duration.millis(3000), kv4, kv5); Timeline scale2 = new Timeline(); scale2.getKeyFrames().add(kf3); SequentialTransition seqtr = new SequentialTransition(scale, move, scale2); seqtr.play(); root.getChildren().add(c); Scene scene = new Scene(root, 300, 250); stage.setTitle("Sequential Timeline animation"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The example creates a sequential timeline animation. The animation consists of three Timelines
that are executed sequentially with a SequentialTransition
.
Circle c = new Circle(50, 100, 10); c.setFill(Color.CADETBLUE);
The moving object in this animation is a Circle
.
KeyValue kv1 = new KeyValue(c.scaleXProperty(), 4); KeyValue kv2 = new KeyValue(c.scaleYProperty(), 4); KeyFrame kf1 = new KeyFrame(Duration.millis(3000), kv1, kv2); Timeline scale = new Timeline(); scale.getKeyFrames().add(kf1);
This is the first Timeline
. It scales up the circle over the time of three seconds.
KeyValue kv3 = new KeyValue(c.centerXProperty(), 250); KeyFrame kf2 = new KeyFrame(Duration.millis(5000), kv3); Timeline move = new Timeline(); move.getKeyFrames().add(kf2);
The second Timeline
moves the circle forward. This part of the animation lasts five seconds.
KeyValue kv4 = new KeyValue(c.scaleXProperty(), 1); KeyValue kv5 = new KeyValue(c.scaleYProperty(), 1); KeyFrame kf3 = new KeyFrame(Duration.millis(3000), kv4, kv5); Timeline scale2 = new Timeline(); scale2.getKeyFrames().add(kf3);
The third Timeline
scales down the circle.
SequentialTransition seqtr = new SequentialTransition(scale, move, scale2); seqtr.play();
The three timelines are placed in a SequentialTransition
. The timelines are played sequentially, one after another.
In this chapter, we have covered JavaFX animation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论