Table of contents
1.
Introduction
2.
JavaFX Fade Transition
2.1.
Properties
2.2.
Constructors
2.3.
Example
3.
Frequently Asked Questions
3.1.
What is JavaFX?
3.2.
What is setDuration Property in JavaFX Fade Transition?
3.3.
What is parallel transition in Java?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

JavaFX Fade Transition

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

Introduction

JavaFX Fade Transition creates a fade effect animation that spans its duration. This can be achieved by continuously reducing the fill color's opacity over time to reach the target opacity value. We are going to learn about it in more detail with the help of an example.

JavaFX Fade Transition

In JavaFX applications, we need to apply fade transitions to specific shapes to create fade transitions, it actually animates the opacity of the node to matte the fill color of the node. This is done by updating the opacity variable of the node at regular intervals. It starts from the fromValue, if provided, else, uses the node's opacity value, similarly, it stops at the toValue value, if provided, else, it will use start value plus byValue. The toValue takes precedence if both toValue and byValue are specified.

In JavaFX, the javafx.animation.FadeTransition class represents FadeTransition. We need to instantiate this class to create the proper fade transitions.

Properties

The properties of the class are described in the following table.

Properties of the transition class

 

Constructors

The class contains three Constructors.

  1. public TranslateTransition() : It creates a new instance of TranslateTransition with the default parameters.
     
  2. public TranslateTransition(Duration duration): It creates a new instance of TranslateTransition with the specified duration.
     
  3. public TranslateTransition(Duration duration, Node node): It creates a new instance of Translate Transition with the specified duration and node.

Example

In the following example, the opacity of the color of the circle keeps decreasing from 10 to 0.1.

 

 package application;  
import javafx.animation.FadeTransition;  
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 Fade_Transition extends Application{  
    public void start(Stage primaryStage) throws Exception {  
        //Creating the circle   
        Circle crcle = new Circle(260,110,70);  
        //set the color of the circle
        crcle.setFill(Color.RED);  
//set the stroke of the crcle
        crcle.setStroke(Color.BLACK);  
       
        //Instantiating FadeTransition class   
        FadeTransition fd = new FadeTransition();  
          
          
        //set the duration for the Fade transition   
       fd.setDuration(Duration.millis(5000));   
        fd.setFromValue(10);  
        fd.setToValue(0.1);  
          
        //set the cycle count for the Fade transition   
        fd.setCycleCount(1000);  
          
        //the transition will set to be auto reversed by setting this to true   
        fd.setAutoReverse(true);  
          
        //set Circle as the node onto which the transition will be applied  
        fd.setNode(crcle);  
          
          
        //playing the transition   
        fd.play();  
          
        //Configuring Group and Scene   
        Group root = new Group();  
        root.getChildren().addAll(crcle);  
        Scene scene = new Scene(root,500,250,Color.WHEAT);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("Translate Transition example");  
        primaryStage.show();  
          
    }  
    public static void main(String[] args) {  
        launch(args);  
    }  
}  

 

Output: 

output-image 1

 

output-image2

 

Frequently Asked Questions

What is JavaFX?

JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms.

What is setDuration Property in JavaFX Fade Transition?

It sets the value of the property duration. This is actually the duration of the fade transition.

What is parallel transition in Java?

The transition that applies multiple animations on a node in parallel.

Conclusion

This article discusses what JavaFX Fade Transition is, What are its properties. We have also seen few constructors and understood it using a simple example.

To learn more, see Basics of C++ with Data StructureDBMSOperating System by Coding Ninjas, and keep practicing on our platform Coding Ninjas Studio.

If you think you are ready for the tech giants company, check out the mock test series on code studio.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more!. You can also prepare for tech giants companies like Amazon, Microsoft, Uber, etc., by looking for the questions asked by them in recent interviews. If you want to prepare for placements, refer to the interview bundle. If you are nervous about your interviews, you can see interview experiences to get ideas about questions that have been asked by these companies.

Do upvote if you find this blog helpful!

Be a Ninja

Happy Coding!

Thankyou Image

 

Live masterclass