javafx animation
JavaFX provides several classes and APIs for creating animations, such as the AnimationTimer
, Timeline
, and Transition
classes.
AnimationTimer
is a class that provides a way to create custom animations. It is designed to be used with a Canvas
or a Group
of nodes. You create a subclass of AnimationTimer
, and then override the handle()
method to implement the animation logic. The handle()
method is called by the JavaFX runtime on each frame of the animation.
Here's an example of how to use AnimationTimer
to create a simple animation that moves a circle across the screen:
import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class MyApplication extends Application { @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 400, 400, Color.WHITE); Circle circle = new Circle(20, Color.BLUE); root.getChildren().add(circle); new AnimationTimer() { private long lastTime = 0; private double x = 0; @Override public void handle(long now) { if (lastTime == 0) { lastTime = now; } double elapsedSeconds = (now - lastTime) / 1_000_000_000.0; double deltaX = elapsedSeconds * 50.0; x += deltaX; circle.setTranslateX(x); lastTime = now; } }.start(); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
In this example, we create a Circle
object and add it to a Group
that is displayed in a Scene
. We then create an instance of AnimationTimer
and override its handle()
method to move the circle across the screen. The handle()
method calculates the elapsed time since the last frame and uses it to update the position of the circle. The start()
method is called to begin the animation.
Another way to create animations in JavaFX is to use the Timeline
class. A Timeline
is a series of keyframes that define the values of one or more properties of an object over time. You can use a KeyValue
object to specify the value of a property at a specific point in time, and then use a KeyFrame
object to specify the duration and timing of the animation.
Here's an example of how to use Timeline
to create a simple animation that changes the color of a rectangle:
import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; public class MyApplication extends Application { @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 400, 400, Color.WHITE); Rectangle rect = new Rectangle(100, 100, 200, 200); rect.setFill(Color.RED); root.getChildren().add(rect); KeyValue keyValue = new KeyValue(rect.fillProperty(), Color.BLUE); KeyFrame keyFrame = new KeyFrame(Duration.seconds(2), keyValue); Timeline timeline = new Timeline(keyFrame); timeline.setCycleCount(Timeline.INDEFINITE); timeline.setAutoReverse(true); timeline.play(); primaryStage.setScene(scene);