×

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors of the given shape. All methods needed for this purpose are present in the javafx.animation.FadeTransition class.

Properties and corresponding methods to create Fade Transition:

1. byValue

This property is of double type. It is used to represent the stop opacity factor and the setByValue() method is used to set it.

2. fromValue

This property is of double type. It is used to represent the start opacity factor and the setFromValue() method is used to set it.

3. toValue

This property is of double type. It is used to represent the stop value for opacity factor and the setToValue() method is used to set it.

4. duration

This property is of the object type. It is used to represent the duration of the transition and the setDuration() method is used to set it.

5. node

This property is of the object type. It is used to represent the shape on which transition is to be applied and the setNode() method is used to set it.

JavaFX Animation - Fade Transition

Example:

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 AnimationUI extends Application {  


    @Override  
    public void start(Stage stage) throws Exception 
    { 
   
     Circle cir = new Circle(250,120,80);  
         
         cir.setFill(Color.BLUE);  
           
         FadeTransition fade = new FadeTransition();  
           
           
         fade.setDuration(Duration.millis(3000));  
           
         fade.setFromValue(10);  
         fade.setToValue(0.1);  
           
         fade.setCycleCount(1000);  
           
         fade.setAutoReverse(true);  
           
         fade.setNode(cir);  
           
           
         fade.play();  
           
         Group root = new Group();  
         root.getChildren().addAll(cir);  
         Scene scene = new Scene(root,500,250);  
         stage.setScene(scene);  
        stage.setTitle("JavaFx Fade Transition example");  
         stage.show();  
     }
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Fade Transition in JavaFX, we have to import all the required libraries such as javafx.animation.FadeTransition, javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.paint.Color, javafx.scene.shape.Circle, javafx.stage.Stage, javafx.util.Duration.

Then we have created one class named AnimationUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a fading circle, a Group object is created which is then passed to the Scene class object.

The Circle is created with the help of the constructor and the size parameter and blue color to be filled are passed in it. A fade Transition object is created and using setters various properties are set. Then it will show the fading circle.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX Animation - Fade Transition Example”. Also, it displays a circle filled with blue color showing the faded shade after some time.

JavaFX Animation - Fade Transition JavaFX Animation - Fade Transition

JavaFX Animation -Fade Transition on Rectangle

Example:

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.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;


public class AnimationUI extends Application {  


    @Override  
    public void start(Stage stage) throws Exception 
    { 
   
    Rectangle rect = new Rectangle(20,20,200,200);  
         
    rect.setFill(Color.RED);  
           
         FadeTransition fade = new FadeTransition();  
           
           
         fade.setDuration(Duration.millis(3000));  
           
         fade.setFromValue(10);  
         fade.setToValue(0.1);  
           
         fade.setCycleCount(1000);  
           
         fade.setAutoReverse(true);  
           
         fade.setNode(rect);  
           
           
         fade.play();  
           
         Group root = new Group();  
         root.getChildren().addAll(rect);  
         Scene scene = new Scene(root,500,450);  
         stage.setScene(scene);  
        stage.setTitle("JavaFx Fade Transition example");  
         stage.show();  
     }
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Fade Transition on Rectangle in JavaFX, we have to import all the required libraries such as javafx.animation.FadeTransition, javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.paint.Color, javafx.scene.shape.Rectangle, javafx.stage.Stage, javafx.util.Duration.

Then we have created one class named AnimationUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a fading Rectangle, a Group object is created which is then passed to the Scene class object.

The Rectangle is created with the help of the constructor and the size parameter and red color to be filled are passed in it. A fade Transition object is created and using setters various properties are set. Then it will show the fading rectangle.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX Animation - Fade Transition Example”. Also, it displays a rectangle filled with red color showing the faded shade after some time.

JavaFX Animation - Fade Transition JavaFX Animation - Fade Transition

Related Topics

JavaFX Light Spot

In the JavaFX application, in order to apply various Light Spot effect from a single source, we use this class. It has the ability to apply light from all the...

4 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Glow

In the JavaFX application, in order to apply various glow effect, we use Glow class. It has the ability to glow the colors of the shape. All methods needed for...

4 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Circle

In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for...

4 minutes read.

JavaFX Area Chart

In the JavaFX application, in order to create Area Chart, the AreaChart class is used. The Area Chart allows us to plot data along the XY plane. All methods needed...

3 minutes read.

JavaFX Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 minutes read.