Table of contents
1.
Introduction
2.
JavaFX Rotate Transition
3.
JavaFX Properties
4.
Constructors
5.
Example
5.1.
Code 
6.
Frequently Asked Questions
6.1.
What is the JAVAFX framework?
6.2.
How to deploy the JavaFX application?
6.3.
Why is JavaFX the best choice for building GUI applications?
7.
Conclusion
Last Updated: Mar 27, 2024

JavaFX Rotate Transition

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This blog will mainly focus on the JavaFX Rotate Transition, but before moving on to the topic, let's recall what JavaFX is. A Java library called JavaFX contains classes and interfaces created using the native Java programming language. The Java Virtual Machine (Java VM) languages, such as JRuby and Scala, are not as friendly as the APIs designed to provide an alternative.

JavaFX Rotate Transition

We must apply rotation transition to a specific shape in the JavaFX application to generate the rotation transition. It can turn the shape around on each of the three axes. The class javafx.animation.Rotate Transition is used to represent the Rotate Transition. All required to produce an adequate RotateTransition is to instantiate this class.

JavaFX Properties

Constructors

  • public RotateTransition(): This will create a new instance of Rotate Transition with the default parameters.
     
  • public RotateTransition(Duration duration): This will create a new instance of Rotate Transition with the specified duration value.
     
  • public RotateTransition(Duration, duration, Node node): This will create a new instance of Rotate Transition with the mentioned duration value and the Node on which it is applied.

Example

Code 

In the following example, we will make a rectangle rotating along the Z-axis by 180 degrees. 

package application;

import javafx.application.Application;
import javafx.animation.RotateTransition;  
import javafx.application.Application;  
import javafx.geometry.Point3D;  
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;  
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
	
		Rectangle rect = new Rectangle(200,100,200,200);  
        rect.setFill(Color.YELLOW);  
        rect.setStroke(Color.ORANGE);  
        rect.setStrokeWidth(5);  
          
        //Instantiating RotateTransition class   
        RotateTransition rotate = new RotateTransition();  
          
        //Setting Axis of rotation   
        rotate.setAxis(Rotate.Z_AXIS);  
          
        // setting the angle of rotation   
        rotate.setByAngle(180);  
          
        //setting cycle count of the rotation   
        rotate.setCycleCount(5000);  
          
        //Setting duration of the transition   
        rotate.setDuration(Duration.millis(10000));  
          
        //the transition will be auto reversed by setting this to true   
        rotate.setAutoReverse(true);  
              
        //setting Rectangle as the node onto which the transition will be applied  
        rotate.setNode(rect);  
          
        //playing the transition   
        rotate.play();  
          
        //Configuring Group and Scene   
        Group root = new Group();  
        root.getChildren().add(rect);  
        Scene scene = new Scene(root,600,400,Color.WHITE);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("Rotate Transition example");  
        primaryStage.show();  
           	
	}

	public static void main(String[] args) {
		launch(args);
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

In this example, we have used Rotate Transition in JavaFX. We extended the Application class and generated one new class called Main. To provide implementation details, we also need to override the start method. This function generates a primaryStage object from a Stage object. A Group object is generated and then supplied to the Scene class object for the container to house a rotating Rectangle. 

Frequently Asked Questions

What is the JAVAFX framework?

We extended the Application class and generated one new class called Main. To provide implementation details, we also need to override the start method. This function generates a primaryStage object from a Stage object. A Group object is generated and then supplied to the Scene class object for the container to house a rotating Rectangle.

How to deploy the JavaFX application?

The first JavaFx application needs to be deployed. Taking up the procedure: By selecting the JavaFX FXML application. There are three sectors in the IDE. The Java file This requires some Java code for the Main Application. The fxml file A user interface portion is defined in this FXML source file.

Why is JavaFX the best choice for building GUI applications?

Java developers can now create richly contented GUI apps efficiently. Adding features like Media, UI controls, Web, 2D, and 3D, etc., required the programmers to rely on various libraries to create Client-Side Applications with rich capabilities. All of these functionalities are part of the JavaFX library.

Conclusion

In this article, we have discussed the JavaFX Rotate Transition. We debated its properties, constructors, and a code example showing its Transition.

If you want to explore more about JavaFX, visit here.

You can improve your skills in  Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you're just getting started to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

We hope that this blog has helped you in enhancing your knowledge. 

"Happy Coding!".

Live masterclass