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 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.