Table of contents
1.
Introduction
2.
JavaFX Parallel Transition
3.
Properties
4.
Constructors
5.
Example
6.
Frequently Asked Questions
6.1.
What is a sequential transition?
6.2.
What are the three top level fundamental components of JavaFX?
6.3.
What is parallel transition?
7.
Conclusion
Last Updated: Mar 27, 2024

JavaFX Parallel Transition

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

Introduction

In this article, we shall be learning one of the applications of JavaFX, which is JavaFX Parallel Transition. In parallel transition, multiple animations are applied on a node in parallel. In this article, we shall be learning more about this.

JavaFX Parallel Transition

The class javafx.animation.ParallelTransition in JavaFX is used to apply parallel transitions. When creating a new instance of this class, we only need to supply the list of transitions to the function Object(). This Transition simultaneously applies several animations to a node. It is similar to Sequential Transition, with the exception that it uses many changes to a node at once as opposed to applying them to a node in the sequential order in which the animations are supplied to the function Object() { [native code] }. 

Properties

Node property image

Constructors

  • public parallelTransition(): creates a ParallelTransition instance using the default settings.
     
  • public ParallelTransition(Animation? children): generates a parallelTransition instance with the list of animations.
     
  • public ParallelTransition(Node node): generates a ParallelTransition object using the provided node as the target for the parallel Transition.
     
  • public ParallelTransition(Node node, Animation? children): generates a ParallelTransition instance with the provided node and the specified collection of animations.

Example

The following example illustrates how to make two circles and move them along the window.

import javafx.animation.Interpolator;
import javafx.animation.ParallelTransition;
import javafx.animation.Transition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;


public class Main extends Application {
    @Override
    public void start(Stage stage) throws Exception
    {

        Circle newball1 = new Circle(0,0,40);
        newball1.setFill(Color.GREENYELLOW);
        Circle newball2 = new Circle(0,0,40);
        newball2.setFill(Color.LIGHTBLUE);

        Group root = new Group();
        root.getChildren().addAll(newball1, newball2);
        Scene scene = new Scene(root, 600, 600);
        stage.setScene(scene);
        stage.setTitle("Javafx Parallel transition example");
        stage.show();

        TranslateTransition newt1 = new TranslateTransition(Duration.millis(2000), newball1);
        newt1.setFromX(newball1.getRadius());
        newt1.setToX(scene.getWidth() - newball1.getRadius());
        newt1.setFromY(scene.getHeight() / 3);
        newt1.setToY(scene.getHeight() / 3);
        newt1.setCycleCount(Transition.INDEFINITE);
        newt1.setAutoReverse(true);
        newt1.setInterpolator(Interpolator.LINEAR);



        TranslateTransition newt2 = new TranslateTransition(Duration.millis(2000), newball2);
        newt2.setFromX(scene.getWidth() - newball2.getRadius());
        newt2.setToX(newball2.getRadius());
        newt2.setFromY(scene.getHeight() / 3 * 2);
        newt2.setToY(scene.getHeight() / 3 * 2);
        newt2.setCycleCount(Transition.INDEFINITE);
        newt2.setAutoReverse(true);
        newt2.setInterpolator(Interpolator.LINEAR);



        ParallelTransition pt = new ParallelTransition(newt1, newt2);
        pt.play();

    }

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

Output

Output image 1

We must import all necessary libraries, including javafx.animation, in order to develop the parallel Transition in JavaFX. Javafx.animation.TranslateTransition , Javafx.animation.ParallelTransition and Javafx.application.PauseTransition in imported the JavaFX program.

Then we extended the Application class by creating a 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 must be constructed and then supplied to the Scene class object for the container to store a parallel transition on balls.

Frequently Asked Questions

What is a sequential transition?

This Transition is used to sequentially apply the list of animations to a node (one by one). Sequential transition is crucial when creating a game that animates its entities sequentially.

What are the three top level fundamental components of JavaFX?

A JavaFX application has three key components: Stage, Scene, and Nodes.

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 discussed the JavaFX Parallel 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