×

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

3 minutes read.

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

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

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Fill Transition

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

4 minutes read.

JavaFX Scale Transition

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

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 ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

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 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 Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

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 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 Inner Shadow

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

4 minutes read.