Do you think IIT Guwahati certified course can help you in your career?
No
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
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
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.