In the last two posts I showed you how to use the Timeline class to animate the node. Although it is good, there is a lot of coding that you need to do to move the nodes around. Instead, you can use the TranslateTransition class. This lets you move the nodes around very easily without having to write a lot of code.
TranslateTransition is one of the many Transition classes that you can use to apply animation to the Nodes without having to code too much. To ‘translate’ something means to move it from one place to another along an axis.
package blog; import javafx.animation.Interpolator; import javafx.animation.Timeline; import javafx.animation.TranslateTransition; import javafx.animation.TranslateTransitionBuilder; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.SceneBuilder; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.shape.RectangleBuilder; import javafx.stage.Stage; import javafx.util.Duration; public class TheTranslateTransition extends Application { Pane pane; Scene scene; Rectangle rect; TranslateTransition translate; @Override public void start(Stage stage) throws Exception { pane = new Pane(); scene = SceneBuilder .create() .root(pane) .width(600) .height(300) .build(); rect = RectangleBuilder .create() .width(100) .height(50) .stroke(Color.SLATEGREY) .fill(Color.WHITESMOKE) .build(); translate = TranslateTransitionBuilder .create() .duration(new Duration(5 * 1000)) .node(rect) .toX(400) .autoReverse(true) .cycleCount(Timeline.INDEFINITE) .interpolator(Interpolator.EASE_BOTH) .build(); pane.getChildren().addAll(rect); stage.setScene(scene); stage.sizeToScene(); stage.show(); translate.play(); } public static void main(String[] args) { Application.launch("blog.TheTranslateTransition"); } }
Instead of moving a Text node around, I am choosing to move a javafx.scene.shape.Rectanglenode around. It is created using *wait for it* RectangleBuilder. It has a width of 100 pixels and a height of 50 pixels, a slate-grey stroke and smoke-white fill.
Now comes the heart of this post – TranslateTransition. The TranslateTransitionBuilder class lets you create a TranslateTransition. The duration() lets you specify the complete duration of the animation. As you know from the previous post, autoReverse() lets you specify whether you want the animation to be played backwards or not. The cycleCount() lets you specify how many times you want the animation to play. Since my rectangle is going to be moved around the X-axis, I am using the toX() method which allows me to define the X coordinate to which the rectangle should be translated. You could very well use toY() and toZ() if you want to translate it along a different axis. The interpolator() allows you to pass an interpolator which will calculate the intermediate values of the animation. There are many built-in interpolators which you can pass in; I passed the EASE_BOTH interpolator. This causes the animation to be slow in the start, accelerate in-between and then slow down again towards the end. The built-in interpolators are available as static fields of the javafx.animation.Interpolator class.
The output below shows how the rectangle moves: