Skip to content

Commit c6cd777

Browse files
committed
Improve tutorial
1 parent 1787f24 commit c6cd777

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed
596 KB
Loading

app/data/javafxdoc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
"AnimationPackageSummary": "javafx.graphics/javafx/animation/package-summary",
5959
"WritableValue": "javafx.base/javafx/beans/value/WritableValue",
6060
"Animation": "javafx.graphics/javafx/animation/Animation",
61-
"Node": "javafx.graphics/javafx/scene/Node.html",
61+
"Duration": "javafx.base/javafx/util/Duration",
62+
"Node": "javafx.graphics/javafx/scene/Node",
6263
"FadeTransition": "javafx.graphics/javafx/animation/FadeTransition",
6364
"FillTransition": "javafx.graphics/javafx/animation/FillTransition",
6465
"PathTransition": "javafx.graphics/javafx/animation/PathTransition",

app/pages/learn/01_tutorial/07_rich_client_apps/02_introduction-javafx-animation.md

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@ description: "Learn to create advanced JavaFX animations"
1313
last_update: 2024-05-17
1414
---
1515

16-
The [javafx.animation](javafxdoc:AnimationPackageSummary) package in JavaFX offers a simple framework for creating animations and transitions in a JavaFX application.
16+
The [javafx.animation](javafxdoc:AnimationPackageSummary) package offers a simple framework for creating animations and transitions in a JavaFX application.
1717
It operates on the principle of [WritableValue\<T\>](javafxdoc:WritableValue) which are used across JavaFX.
1818
It additionally provides a variety of built-in transitions for common effects, support for parallel and sequential transitions, and the ability to handle events upon animation completion.
1919
I will go through all types of animations, starting with `Animation` and its subclasses `Transition` and `Timeline`, before representing a lower level animation with `AnimationTimer`.
2020
While `Transition` provides a simpler and more user-friendly way to create animations, `Timeline` offers greater flexibility and is suitable for more complex animations.
21-
In contrast, `AnimationTimer` is designed for frame-by-frame updates and does not take use of `WritableValue<T>`.
21+
In contrast, `AnimationTimer` is designed for frame-by-frame updates and does not make use of `WritableValue<T>`.
22+
23+
## WritableValue
24+
`WritableValue<T>` is an interface that wraps a value that can be read and set. It is commonly used for storing properties in JavaFX UI elements, like
25+
`width` or `height` in the [Rectangle](javafxdoc:Rectangle) shape.
2226

2327
## Animation
2428

2529
The [Animation](javafxdoc:Animation) abstract class provides the core functionality for `Transition` and `Timeline` animations.
2630

2731
An `Animation` consists of multiple properties:
28-
- The `targetFramerate` is the maximum framerate (frames per s) at which this `Animation` will run.
29-
- The `currentTime` is the current point in time as a `Duration` of the `Animation`.
32+
- The `targetFramerate` is the maximum framerate (frames per second) at which this `Animation` will run.
33+
- The `currentTime` is the current point in time in the `Animation` as a [Duration](javafx:Duration).
3034
- The `rate` defines the direction and speed at which the `Animation` is expected to be played. It supports both positive and negative numbers.
3135
- The `cycleCount` defines the number of cycles of this `Animation`. It can't be changed while running and must be positive.
3236
- The `cycleDuration` is the `Duration` of one cycle of this `Animation`. It is the time it takes to play from start to end of the `Animation` **at the default rate of 1.0**.
@@ -43,10 +47,8 @@ A quick look into the JavaDocs provides a great overview of its functionalities.
4347
The [Transition](javafxdoc:Transition) abstract class serves as the foundational class for all transitions, presenting a common form of `Animation`.
4448
JavaFX provides a variety of built-in transitions for common [Node](javafxdoc:Node) and [Shape](javafxdoc:Shape) properties.
4549

46-
**Note:** By default, all transitions, excluding `ParallelTransition` and `SequentialTransition`, utilize the `Interpolator#EASE_BOTH`.
47-
4850
### Fade Transition
49-
The [FadeTransition](javafxdoc:FadeTransition) creates a fade effect over a `duration`.
51+
The [FadeTransition](javafxdoc:FadeTransition) creates a fade effect.
5052
This is done by updating the `opacity` property of the `Node` at regular intervals.
5153

5254
![FadeTransition](/assets/images/javafx/animation/transition/fade-example.gif)
@@ -62,7 +64,7 @@ transition.play();
6264
```
6365

6466
### Fill Transition
65-
The [FillTransition](javafxdoc:FillTransition) creates an animation, that changes the filling of a shape over a `duration`.
67+
The [FillTransition](javafxdoc:FillTransition) creates an animation, that changes the filling of a shape.
6668
This is done by updating the `fill` property of the `Shape` at regular intervals.
6769

6870
![FillTransition](/assets/images/javafx/animation/transition/fill-example.gif)
@@ -77,8 +79,24 @@ transition.setInterpolator(Interpolator.LINEAR);
7779
transition.play();
7880
```
7981

82+
### Translate Transition
83+
The [TranslateTransition](javafxdoc:TranslateTransition) creates a move/translate animation from one position to another in a straight line.
84+
This is done by updating the `translateX`, `translateY` and `translateZ` properties of the `Node` at regular interval.
85+
86+
![TranslateTransition](/assets/images/javafx/animation/transition/translate-example.gif)
87+
```java
88+
Circle circle = new Circle(50, 50, 10, Color.GREEN);
89+
90+
TranslateTransition transition = new TranslateTransition(Duration.seconds(5), circle);
91+
transition.setToX(200);
92+
transition.setToY(200);
93+
transition.setInterpolator(Interpolator.LINEAR);
94+
95+
transition.play();
96+
```
97+
8098
### Path Transition
81-
The [PathTransition](javafxdoc:PathTransition) creates a path animation that spans its `duration`.
99+
The [PathTransition](javafxdoc:PathTransition) creates a move animation using a complex predefined path specified by a sequence of shapes.
82100
The translation along the path is done by updating the `translateX` and `translateY` properties of the `Node`, and the `rotate` variable will get updated if `orientation` is set to `OrientationType.ORTHOGONAL_TO_TANGENT`, at regular interval.
83101

84102
![PathTransition](/assets/images/javafx/animation/transition/path-example.gif)
@@ -96,15 +114,15 @@ transition.play();
96114
```
97115

98116
### Rotate Transition
99-
The [RotateTransition](javafxdoc:RotateTransition) creates a rotation animation that spans its `duration`.
100-
This is done by updating the `rotate` property of the `Node` at regular interval. The angle value is specified in degrees.
117+
The [RotateTransition](javafxdoc:RotateTransition) creates a rotation animation.
118+
This is done by updating the `rotate` property of the `Node` at regular interval.
119+
The angle value is specified in degrees.
101120

102121
![RotateTransition](/assets/images/javafx/animation/transition/rotate-example.gif)
103122
```java
104123
Rectangle rectangle = new Rectangle(125, 125, 50, 50);
105124
rectangle.setFill(Color.GREEN);
106125

107-
108126
RotateTransition transition = new RotateTransition(Duration.seconds(5), rectangle);
109127
transition.setFromAngle(0);
110128
transition.setToAngle(360);
@@ -114,7 +132,7 @@ transition.play();
114132
```
115133

116134
### Scale Transition
117-
The [ScaleTransition](javafxdoc:ScaleTransition) creates a scale animation over a `duration`.
135+
The [ScaleTransition](javafxdoc:ScaleTransition) creates a scale animation, that changes the size of a node.
118136
This is done by updating the `scaleX`, `scaleY` and `scaleZ` properties of the `Node` at regular interval.
119137

120138
![ScaleTransition](/assets/images/javafx/animation/transition/scale-example.gif)
@@ -130,7 +148,7 @@ transition.play();
130148
```
131149

132150
### Stroke Transition
133-
The [StrokeTransition](javafxdoc:StrokeTransition) creates an animation, that changes the stroke color of a shape over a `duration`.
151+
The [StrokeTransition](javafxdoc:StrokeTransition) creates an animation, that changes the stroke color of a shape.
134152
This is done by updating the `stroke` property of the `Shape` at regular intervals.
135153

136154
![StrokeTransition](/assets/images/javafx/animation/transition/stroke-example.gif)
@@ -146,36 +164,38 @@ transition.setInterpolator(Interpolator.LINEAR);
146164
transition.play();
147165
```
148166

149-
### Translate Transition
150-
The [TranslateTransition](javafxdoc:TranslateTransition) creates a move/translate animation that spans its `duration`.
151-
This is done by updating the `translateX`, `translateY` and `translateZ` properties of the `Node` at regular interval.
167+
### Pause Transition
168+
The [PauseTransition](javafxdoc:PauseTransition) executes an `Animation.onFinished` at the end of its execution.
152169

153-
![TranslateTransition](/assets/images/javafx/animation/transition/translate-example.gif)
170+
### Parallel Transition
171+
The [ParallelTransition](javafxdoc:ParallelTransition) plays a list of animations in parallel.
172+
173+
![ParallelTransition](/assets/images/javafx/animation/transition/parallel-example.gif)
154174
```java
155-
Circle circle = new Circle(50, 50, 10, Color.GREEN);
175+
Rectangle rectangle = new Rectangle(50, 50, 10, 10);
176+
rectangle.setFill(Color.GREEN);
156177

157-
TranslateTransition transition = new TranslateTransition(Duration.seconds(5), circle);
158-
transition.setToX(200);
159-
transition.setToY(200);
160-
transition.setInterpolator(Interpolator.LINEAR);
178+
TranslateTransition translate = new TranslateTransition(Duration.seconds(5));
179+
translate.setToX(200);
180+
translate.setToY(200);
181+
translate.setInterpolator(Interpolator.LINEAR);
182+
183+
RotateTransition rotate = new RotateTransition(Duration.seconds(5));
184+
rotate.setFromAngle(0);
185+
rotate.setToAngle(360);
186+
rotate.setInterpolator(Interpolator.LINEAR);
161187

188+
ParallelTransition transition = new ParallelTransition(rectangle, translate, rotate);
162189
transition.play();
163190
```
164-
165-
### Pause Transition
166-
The [PauseTransition](javafxdoc:PauseTransition) executes an `Animation.onFinished` at the end of its `duration`.
167-
168-
### Parallel Transition
169-
The [ParallelTransition](javafxdoc:ParallelTransition) plays a list of animations in parallel.
170-
171191
### Sequential Transition
172192
The [SequentialTransition](javafxdoc:SequentialTransition) plays a list of animations in sequential order.
173193
It is not recommended to contain an `Animation`, which is not the last one, with `Duration.INDEFINITE` as this will block all later animations in the sequence.
174194

175195
## Timeline
176-
A [Timeline](javafxdoc:Timeline) is used to define a free form `Animation` of any `WritableValue<T>`. It is helpful if none of the built-in transitions operate on the required properties.
196+
A [Timeline](javafxdoc:Timeline) is used to define a free form `Animation` on any `WritableValue<T>`. It is helpful if none of the built-in transitions operate on the required properties.
177197

178-
It consists of a sequential series of `KeyFrame`.
198+
It consists of a sequential series of `KeyFrame`s.
179199
Each `KeyFrame` encapsulates a moment in time (**Cue Point**), and collectively specify how target properties evolve over the entire duration.
180200

181201
> **Warning:** A running Timeline is being referenced from the FX runtime. Infinite Timeline might result in a memory leak if not stopped properly. All the objects with animated properties would not be garbage collected.
@@ -232,10 +252,10 @@ Here is a visualization of the Interpolator using the example from [Timeline](#e
232252
## AnimationTimer
233253

234254
The [AnimationTimer](javafxdoc:AnimationTimer) abstract class provides the lowest level option to create an animation.
235-
The `handle(long now)` method gets called in each frame while it is active. The timestamp `long now` is the nanoseconds time of the current frame and will be the same for all `AnimationTimer` called during that frame.
236-
Additionally, the `AnimationTimer` adds the `start()` and `stop()` to handle the lifetime of the animation.
255+
The `handle(long now)` method gets called in each frame while it is active. The timestamp `now` is the time of the current frame in nanoseconds and will be the same for all `AnimationTimer`s called during that frame.
256+
Additionally, the `AnimationTimer` adds `start()` and `stop()` methods to handle the lifetime of the animation.
237257

238-
**Note:** The handle method will be called on the **JavaFX Application Thread** and thus shouldn't do heavy computations.
258+
**Note:** The `handle` method will be called in the **JavaFX Application Thread** and thus shouldn't do heavy computations.
239259

240260

241261
## Conclusion

0 commit comments

Comments
 (0)