Tino APCS

RotateTransition

The RotateTransition class rotates a node. Here are important methods:

Method Description
setFromAngle(double) Sets the start angle
setToAngle(double) Sets the end angle
setByAngle(double) Instead of specifying start/end, this specifies how much to rotate by

Let's rotate a Node:

// Create a Node. Could be any kind.
Rectangle rect = new Rectangle(50, 200, 100, 50);

// Create the Transition
RotateTransition rt = new RotateTransition();
rt.setDuration(Duration.seconds(1));
rt.setByAngle(150);
rt.setNode(rect);

// Play it (after showing the Stage)
rt.play();

Here's the same code but over 1.5 seconds using a specific start/end angle:

// Create a Node. Could be any kind.
Rectangle rect = new Rectangle(50, 200, 100, 50);

// Create the Transition
RotateTransition rt = new RotateTransition();
rt.setDuration(Duration.millis(1500));
rt.setFromAngle(90);
rt.setToAngle(210);
rt.setNode(rect);

// Play it (after showing the Stage)
rt.play();

Here's the same code using 4 cycles. One cycle counts in either direction whether forward or backward. Try it with autoreverse turned on.

// Create a Node. Could be any kind.
Rectangle rect = new Rectangle(50, 200, 100, 50);

// Create the Transition
RotateTransition rt = new RotateTransition();
rt.setDuration(Duration.seconds(1));
rt.setByAngle(150);
rt.setCycleCount(4);
rt.setAutoReverse(false);  // Try true
rt.setNode(rect);

// Play it (after showing the Stage)
rt.play();

Here's the same code using an infinite cycle with autoreverse turned on. In a future lesson you will learn how to pause, restart, and stop the animation.

// Create a Node. Could be any kind.
Rectangle rect = new Rectangle(50, 200, 100, 50);

// Create the Transition
RotateTransition rt = new RotateTransition();
rt.setDuration(Duration.seconds(1));
rt.setByAngle(150);
rt.setCycleCount(Animation.INDEFINITE);
rt.setAutoReverse(true);
rt.setNode(rect);

// Play it (after showing the Stage)
rt.play();

Tips

Using a Transition is a matter of reading the API to see how to set up the Transition before applying it to a Node. Here are a few things to keep in mind:

  • You cannot assign a Transition to multiple Nodes but you can create a Group of Nodes and apply the Transition to the group as a whole. The group will move as a single unit.

  • You can add multiple Nodes at once to any other Node using
    parent.getChildren().addAll(node1, node2, node3, ...);

  • You can run multiple Transitions at once on a Node. For example, RotateTransition and TranslateTransition.

Dark Mode

Outline