×

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along all three axis. All methods needed for this purpose are present in the javafx.animation.ScaleTransition class.

Properties and corresponding methods to create Scale Transition:

1. byX

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

2. byY

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

3. byZ

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

4. fromX

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

5. fromY

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

6. fromZ

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

JavaFX Animation - Scale Transition

Example:

import javafx.animation.ScaleTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
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 
    { 
   
    Circle circle = new Circle(150, Color.RED);
   
         HBox root = new HBox(circle);


         root.setStyle("-fx-padding: 10;" +
                 "-fx-border-style: solid inside;" +
                 "-fx-border-width: 2;" +
                 "-fx-border-insets: 5;" +
                 "-fx-border-radius: 5;" +
                 "-fx-border-color: green;");
               Scene scene = new Scene(root);


         stage.setScene(scene);
  
         stage.setTitle(" JavaFX Animation - Scale Transition Example");
        
         stage.show();
                   ScaleTransition trans = new ScaleTransition(Duration.seconds(2), circle);
         trans.setFromX(1.0);
         trans.setToX(0.40);
         trans.setFromY(1.0);
         trans.setToY(0.20);
             
         trans.setCycleCount(ScaleTransition.INDEFINITE);
         
         trans.setAutoReverse(true);
       
         trans.play();       
     }
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Scale Transition in JavaFX, we have to import all the required libraries such as javafx.animation.ScaleTransition, 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 scaling 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 red color to be filled are passed in it. A scale Transition object is created and using setters various properties are set. Then it will show the scaling 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 - Scale Transition Example”. Also, it displays a circle filled with red color scaling in a given plane.

JavaFX Animation - Scale Transition JavaFX Animation - Scale Transition

JavaFX Animation -Scale Transition on Rectangle

Example:

import javafx.animation.ScaleTransition;
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 
    { 
    Group root = new Group();
         Scene scene = new Scene(root, 600, 400);
         stage.setScene(scene);


         Rectangle rect = new Rectangle ( 200, 140, 100, 100);
         rect.setArcHeight(50);
         rect.setArcWidth(50);
         rect.setFill(Color.RED);
     
         ScaleTransition st = new ScaleTransition(Duration.millis(2000), rect);
         st.setByX(1.5f);
         st.setByY(1.5f);
         st.setAutoReverse(true);
     
         st.play();
         
         root.getChildren().add(rect);


    stage.setScene(scene);  
   stage.setTitle("JavaFX Scale Transition example");  
    stage.show();  
      
}  
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Scale Transition in JavaFX, we have to import all the required libraries such as javafx.animation.ScaleTransition, 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 scaling 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 is passed in it. A scale Transition object is created and using setters various properties are set. Then it will show a scaling 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 Scale Transition example”. Also, it displays a rectangle filled with red color scaling in a given plane.

JavaFX Animation - Scale Transition JavaFX Animation - Scale Transition

Related Topics

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX HBox

In the JavaFX application, in order to set HBox as a layout, the HBox class is used. The HBox layout allows us to arrange the components in one row. All...

3 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.

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 Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Translate Transition

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

3 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 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 Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX Fill Transition

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

4 minutes read.