×

JavaFX Rotate Transition

In the JavaFX application, in order to generate the rotate transition, we have to apply rotation transition on any particular shape. It has the ability to rotate the shape along all three axes. All methods needed for this purpose are present in the javafx.animation.RotateTransition class.

Properties and corresponding methods to create Rotate Transition:

1. axis

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

2. byAngle

This is double type property that is used to represent the angle at which the object is rotated. setByAngle() method is used to set it.

3. fromAngle

This property is used to represent rotate transition start Angle. setFromAngle() method is used to set it.

4. toAngle

This property is used to represent rotate transition stop Angle and the setToAngle() method helps to set it.

5. duration

It is used to represent the duration of the rotate transition. setDuration() method is used to set it.

6. node

This object type property is used to demonstrate the node for which rotate transition is to be applied.

JavaFX Animation -Rotate Transition

Example:

import javafx.animation.RotateTransition;
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.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;


public class AnimationUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    { 
    Rectangle newrect = new Rectangle(150,70,150,150);  
    newrect.setFill(Color.BLUE);  
    newrect.setStroke(Color.RED);  
    newrect.setStrokeWidth(3);  
      
    RotateTransition rotation = new RotateTransition();  
      
    rotation.setAxis(Rotate.Z_AXIS);  
    rotation.setByAngle(360);  
      
    rotation.setCycleCount(500);  
      
    rotation.setDuration(Duration.millis(700));  
      
    rotation.setAutoReverse(true);  
          
    rotation.setNode(newrect);  
      
    rotation.play();  
      
    Group root = new Group();  
    root.getChildren().add(newrect);  
    Scene scene = new Scene(root,600,400);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("JavaFX Rotation Transition example");  
    primaryStage.show();  
      
}  
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Rotate Transition in JavaFX, we have to import all the required libraries such as javafx.animation.RotateTransition, javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.paint.Color, javafx.scene.shape.Rectangle, javafx.scene.transform.Rotate, javafx.stage.Stage, javafx.util.Duration.

Then we have created one class named AnimationUI 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 roating Rectangle, a Group object is created which is then passed to the Scene class object.

The rectangle is created with the help of the constructor and X, Y, Width, and Height are passed to it. Then the object of RotateTransition is created and the axis is set to z and start angle to 360 and the duration millis to 700. This will show a rotating rectangle.

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 Transition example”. Also, it displays a rectangle filled with blue color rotating in a given plane.

JavaFX Animation -Rotate Transition

JavaFX Animation -Rotate Transition on Hexagon

Example:

import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
import javafx.util.Duration;


public class AnimationUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    { 
    Polygon newhexagon = new Polygon();        
        
    newhexagon.getPoints().addAll(new Double[]{        
           200.0, 50.0, 
           300.0, 50.0, 
           450.0, 150.0,          
           300.0, 250.0, 
           200.0, 250.0,                   
           150.0, 150.0, 
        }); 
    newhexagon.setFill(Color.RED); 
         
        RotateTransition rotation = new RotateTransition(); 
        
        rotation.setDuration(Duration.millis(1000)); 
        
        rotation.setNode(newhexagon);       
        
        rotation.setByAngle(360); 
        
        rotation.setCycleCount(50); 
        
        rotation.setAutoReverse(false); 
        
        rotation.play(); 
           
        Group root = new Group(newhexagon); 
           
        Scene scene = new Scene(root, 600, 400);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("JavaFX Rotation Transition example");  
    primaryStage.show();  
      
}  
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Rotate Transition on polygon in JavaFX, we have to import all the required libraries such as javafx.animation.RotateTransition, javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.paint.Color, javafx.scene.shape.Rectangle, javafx.scene.transform.Rotate, javafx.stage.Stage, javafx.util.Duration.

Then we have created one class named AnimationUI 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 roating hexagon, a Group object is created which is then passed to the Scene class object.

The Hexagon is created with the help of the polygon constructor and array of points passed with its object. Then the object of RotateTransition is created and the axis is set to z and start angle to 360 and the duration millis to 1000. This will show a rotating hexagon.

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 Transition example”. Also, it displays a hexagon filled with red color rotating in a given plane.

JavaFX Animation -Rotate Transition

Related Topics

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed...

4 minutes read.

JavaFX Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for...

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 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 Image Input

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

4 minutes read.

JavaFX Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 minutes read.

JavaFX Drop Shadow

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

4 minutes read.

JavaFX Light Point

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

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 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 Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX Parallel Transition

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

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 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 Light Spot

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

4 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

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 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.