How to rotate a node in JavaFX?

JavaFXObject Oriented ProgrammingProgramming

If you move an object in the XY plane around a fixed point with an angle it is known as rotation.

In JavaFX using the object of the javafx.scene.transform.Rotate class, you can rotate a node. This class internally rotates the coordinate space of the node around a given fixed point, this makes the node to appear rotated.

This class contains five properties −

  • The angle property (double) specifying the angle of rotation. You can set the value to this property using the setAngle() method.

  • The axis property (Point3D) specifying the axis of rotation. You can set the value to this property using the setAxis() method.

  • The pivotX property (double) specifying the x coordinates of the pivot point. You can set the value to this property using the setPivotX() method.

  • The pivotY property (double) specifying the y coordinates of the pivot point. You can set the value to this property using the setPivotY() method.

  • The pivotZ property (double) specifying the z coordinates of the pivot point. You can set the value to this property using the setPivotZ() method.

Every node in JavaFX contains an observable list to hold all the transforms to be applied on a node. You can get this list using the getTransforms() method.

To rotate a Node in JavaFX −

  • Instantiate the Rotate class.

  • Set the angle and the pivot point using the setter methods.

  • Get the list of transforms from the node (which you want to rotate) using the getTransforms() method.

  • Add the above created rotate object to it.

  • Add the node to the scene.


Following the JavaFX example demonstrates the rotation transform. It contains a 2D geometric shape and a slider, representing the angle value. If you move the slider the object will be rotated.

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
public class RotationExample extends Application {
   public void start(Stage stage) {
      //Creating a rectangle
      Rectangle rect = new Rectangle(300, 100, 75, 75);
      //Setting the slider
      Slider slider = new Slider(0, 360, 0);
      //creating the rotation transformation
      Rotate rotate = new Rotate();
      //Setting pivot points for the rotation
      //Adding the transformation to rectangle
      //Linking the transformation to the slider
      slider.valueProperty().addListener(new ChangeListener<Number>() {
         public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){
            //Setting the angle for the rotation
            rotate.setAngle((double) newValue);
      //Adding the transformation to the circle
      //Creating the pane
      BorderPane pane = new BorderPane();
      pane.setRight(new VBox(new Label("Rotate"), slider));
      //Preparing the scene
      Scene scene = new Scene(pane, 600, 300);
      stage.setTitle("Rotation Example");
   public static void main(String args[]){


Updated on 19-May-2020 07:39:12