×

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along with the pivot point with a specified angle. All methods needed for this purpose are present in the javafx.scene.transform.Rotate class.

Properties and corresponding methods to create rotate Transformation :

1. axis 

This property is of the object type. It is used to represent the axis of the rotation of the object and the setAxis( Point3D value ) method is used to set it.

2. angle

This property is of double type. It is used to represent the angle by which the angle is rotated and the setAngle( Double value ) method is used to set it.

3. pivotX      

This property is of double type. It is used to represent the x coordinate of the pivot point of the rotation and the setPivotX( Double value ) method is used to set it.

4. pivotY

This property is of double type. It is used to represent the y coordinate of the pivot point of the rotation and the setPivotY( Double value ) method is used to set it.

5. pivotZ

This property is of double type. It is used to represent the z coordinate of the pivot point of the rotation and the setPivotZ( Double value ) method is used to set it.

JavaFX Transformation – Rotation Transformation on Rectangle:

Example:

import javafx.application.Application;  
import javafx.scene.Group;  
import javafx.scene.Scene;  
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;  




public class SliderUI extends Application
{  
@Override  
public void start ( Stage primaryStage ) throws Exception 
{  
    Rectangle rect1 = new Rectangle (40, 120, 220, 180);  
    Rectangle rect2 = new Rectangle (540, 4, 220, 180);  
      
    rect1.setFill ( Color.RED );  
    rect1.setStroke ( Color.BLACK );  
    rect1.setStrokeWidth (5);  
    rect2.setFill ( Color.BLUE );  
    rect2.setStroke ( Color.BLACK );  
    rect2.setStrokeWidth (5);  
      
     Text text = new Text();  
     text.setText(" Rectangle Without Rotate Effect ");  
     text.setX(40);  
     text.setY(390);  
     text.setFont(Font.font(" Times New Roman ",  FontWeight.BLACK, FontPosture.ITALIC, 30));  
     text.setFill ( Color.RED );  
     text.setStroke ( Color.BROWN);  
     text.setUnderline(true);  
     
     
     Text text2 = new Text();  
     text2.setText(" Rectangle With Rotate Effect ");  
     text2.setX(460);  
     text2.setY(440);  
     text2.setFont(Font.font("Times New Roman", FontWeight.BLACK, FontPosture.ITALIC, 30));  
     text2.setFill( Color.RED );  
     text2.setStroke( Color.BROWN );  
     text2.setUnderline(true);  


     Rotate rotate = new Rotate();  
     
     rotate.setAngle(20);  
     rotate.setPivotX(150);  
     rotate.setPivotY(70);  
       
     rect2.getTransforms().add(rotate);  
      
    Group root = new Group();  
    root.getChildren().addAll ( rect1, rect2, text, text2 );  
    Scene scene = new Scene( root, 900, 500 ); 
 
    primaryStage.setScene(scene);  
    primaryStage.setTitle(" JavaFX Rotation Example ");  
    primaryStage.show();  
}  


public static void main( String[] args ) 
{  
    launch( args );  
}  
} 

Output:

JavaFX Effect – Transformation

Explanation:

In order to create rotate transformation on rectangle to increase shape in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.paint.Color, javafx.scene.text.Font, javafx.scene.text.FontPosture, javafx.scene.text.FontWeight, javafx.scene.text.Text, javafx.scene.shape.Rectangle, javafx.scene.transform.Rotate, javafx.stage.Stage. 

Then we have created one class named TransformUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a rectangle with and without the rotate transformation, a Group object is created which is then passed to the Scene class object.

The rectangle is created using the constructor and various properties are set using setters. Also, the text is created using the constructor, and various properties are set. The rotate transformation constructor is created and then using the setters various properties are set.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX Rotation example”. Also, it displays the rectangle with and without the rotate transformation.


Related Topics

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Color Input

In the JavaFX application, in order to apply various color input effect, we use the ColorInput class. It has the ability to fill the specified shape with a given color...

4 minutes read.

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.