
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rotate a Node in JavaFX
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.
Example
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); rect.setFill(Color.BLUEVIOLET); rect.setStrokeWidth(5.0); rect.setStroke(Color.BROWN); //Setting the slider Slider slider = new Slider(0, 360, 0); slider.setShowTickLabels(true); slider.setShowTickMarks(true); slider.setMajorTickUnit(90); slider.setBlockIncrement(10); slider.setOrientation(Orientation.VERTICAL); slider.setLayoutX(2); slider.setLayoutY(195); //creating the rotation transformation Rotate rotate = new Rotate(); //Setting pivot points for the rotation rotate.setPivotX(300); rotate.setPivotY(100); //Adding the transformation to rectangle rect.getTransforms().addAll(rotate); //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 rect.getTransforms().add(rotate); //Creating the pane BorderPane pane = new BorderPane(); pane.setRight(new VBox(new Label("Rotate"), slider)); pane.setCenter(rect); //Preparing the scene Scene scene = new Scene(pane, 600, 300); stage.setTitle("Rotation Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }