Table of contents
1.
Introduction
2.
JavaFX Animation
2.1.
Basic Transitions
2.2.
How Does JavaFX Animation Work in Java?
2.2.1.
Rotate Transition
2.2.2.
Scale Transition
2.2.3.
Translate Transition
2.2.4.
Fade Transition
2.2.5.
Fill Transition
2.3.
Steps for applying Animations
3.
Example
3.1.
Rotate Transition
4.
Frequently Asked Questions
4.1.
What is JavaFX?
4.2.
How do you animate an object in JavaFX?
4.3.
What is parallel transition?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Introduction to JavaFX Animation

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, animation consists of two fundamental steps: constructing an animation frame and then enabling Java to color the frame. Applets, AWT, Swing, and JavaFX may all be used to create animation in Java. Applet animation is used in browser-based applications, whereas AWT, Swing, and JavaFX are standalone apps. The majority of real-time apps are stand-alone. So we'll use JavaFX to manage our animation.

JavaFX Animation

In general, Animation is described as the transition that produces the illusion of motion for an object. It is the sequence of modifications made to an item over a specific time period to portray the thing in motion.

This may be accomplished by the fast presentation of frames. The package javafx.animation in JavaFX comprises all the classes used to apply animations to nodes. Every class in this package extends the class javafx.animation.Animation.

JavaFX has transition classes such as RotateTransition, ScaleTransition, TranslateTransition, FadeTransition, FillTransition, StrokeTransition, and others.

Basic Transitions

The javafx.animation package includes classes for performing the following transitions.

table containing different transitions in JavaFX animation.

How Does JavaFX Animation Work in Java?

A JavaFX animation package comprises all of the animation classes. As a result, we must import animations while applying them. We must extend the Animation class to add animations to our class. This Animation class contains all of the necessary animation packages.

Rotate Transition

This animation has a rotating characteristic. The package is animation.RotateTransition.

RotateTransition rotate = new RotateTransition(); //creating object for rotate transition
rotate.play();//using the play method to apply rotation
You can also try this code with Online Java Compiler
Run Code

Scale Transition

The object in this animation moves in all three directions (X, Y, and Z). The package is animation.ScaleTransition.

ScaleTransition scale = new ScaleTransition();  //creating object for scale transition
scale.play();  //using the play method to apply scale transition
You can also try this code with Online Java Compiler
Run Code

Translate Transition

This animation shifts the object from one position to another at regular intervals of time. The package is animation.TranslateTransition.

TranslateTransition translate = new TranslateTransition();  //creating object for Translate transition
translate.play();  //using the play method to apply translate transition
You can also try this code with Online Java Compiler
Run Code

Fade Transition

By specifying the opacity value, this animation dulls the object. The package is animation. FadeTransition.

FadeTransition fade = new FadeTransition();  //creating object for fade transition
fade.play();  //using the play method to apply fade transition
You can also try this code with Online Java Compiler
Run Code

Fill Transition

By choosing the time interval, this animation causes the object to fill with two colors, one after the other. The package is animation. FillTransition.

FillTransition fill = new FillTransition();  //creating object for fill transition
fill.play();  //using the play method to apply fill transition
You can also try this code with Online Java Compiler
Run Code

Steps for applying Animations

Create the destination node and set its properties.

Polygon traingle = new Polygon();
Double[] doubleValues=new Double[] { 60.0, 60.0, 200.0, 100.0, 100.0, 200.0 };
traingle.getPoints().addAll(doubleValues);
traingle.setFill(Color.LIGHTGREEN);
You can also try this code with Online Java Compiler
Run Code

Instantiate the corresponding transition class.

RotateTransition rotateTransition = new RotateTransition();
You can also try this code with Online Java Compiler
Run Code

Set the transition's desired attributes such as duration, cycle-count, etc.

rotateTransition.setAxis(Rotate.X_AXIS);
rotateTransition.setByAngle(360);
rotateTransition.setCycleCount(500);
rotateTransition.setDuration(Duration.millis(1000));
You can also try this code with Online Java Compiler
Run Code

Set the target node for the transition to be applied to. For this purpose, use the following method.

rotateTransition.setNode(traingle);
You can also try this code with Online Java Compiler
Run Code

Finally, use the play() method to play the transition.

rotateTransition.play(); 
You can also try this code with Online Java Compiler
Run Code

Example

Rotate Transition

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.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class NewFXMain extends Application {
	@Override
	public void start(Stage outStage) throws Exception {
		Polygon traingle = new Polygon();
		Double[] doubleValues=new Double[] { 60.0, 60.0, 200.0, 100.0, 100.0, 200.0 };
		traingle.getPoints().addAll(doubleValues);
		traingle.setFill(Color.LIGHTGREEN);
		traingle.setStroke(Color.RED);
		traingle.setStrokeWidth(5);
		RotateTransition rotateTransition = new RotateTransition();
		rotateTransition.setAxis(Rotate.Z_AXIS);
		rotateTransition.setByAngle(360);
		rotateTransition.setCycleCount(500);
		rotateTransition.setDuration(Duration.millis(1000));
		rotateTransition.setAutoReverse(true);
		rotateTransition.setNode(traingle);
		rotateTransition.play();
		Group root = new Group(); 
		root.getChildren().add(traingle); 
		Scene scene = new Scene(root, 400, 300, Color.WHEAT);
		outStage.setScene(scene);
		outStage.setTitle("Triangle Rotate Transition Example");
		outStage.show();
	}
	public static void main(String[] args) {
		launch(args);
	}
}
You can also try this code with Online Java Compiler
Run Code

Output:

The triangle in the below image rotates along the z-axis

output of rotate transition on a triangle.

output of rotate transition on a triangle.

Frequently Asked Questions

What is JavaFX?

JavaFX is a collection of graphics and media packages that allow developers to create, test, debug, and deploy rich client applications that work consistently across several platforms.

How do you animate an object in JavaFX?

The Animation class is responsible for the core functionality of all animations used in the JavaFX runtime. By setting cycleCount, an animation can be made to loop. Set the autoReverse -flag to make an animation loop back and forth. To play an Animation, use play() or playFromStart().

What is parallel transition?

Parallel transitions are transitions that occur simultaneously; from a particular activity or depending on the outcome of a specific activity, more than one activity might occur in parallel.

Conclusion

In this article, we have extensively discussed JavaFX Animation, basic transitions, their working, and how to apply them. We learned to create a rotate transition with the help of an example. 

We hope that this blog has helped you enhance your knowledge of java and JavaFX and if you would like to learn more about java, check out our articles, java archivesJavaFX - Coding Ninjas Coding Ninjas Studio

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

An image that displays a thankyou message from coding ninjas.

Live masterclass