Monday, July 11, 2016

98. Scale Transition

We can scale a node using ScaleTransition.


The Rectangle rect is scaled up 20% in x and y, in 5 seconds after the button is clicked.


package ex98;

import javafx.animation.ScaleTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Ex98 extends Application {
  
    @Override
    public void start(Stage stage) {
        
        DropShadow dropShadow = new DropShadow();
        dropShadow.setOffsetX(5);
        dropShadow.setOffsetY(5);
        dropShadow.setColor(Color.GOLD);
        
        Rectangle rect = new Rectangle(300, 100, Color.RED);
        rect.setStrokeWidth(5);
        rect.setArcWidth(30);
        rect.setArcHeight(30);
        rect.setStroke(Color.BLUE);
        rect.setEffect(dropShadow);

        Text text = new Text("ScaleTransition");
        text.setFont(Font.font("Geogia", 32));
        
        StackPane stackPane = new StackPane(rect, text);
        
        ScaleTransition scaleTransition = 
                new ScaleTransition(Duration.seconds(5), rect);
        
        scaleTransition.setToX(1.2);
        scaleTransition.setToY(1.2);
        
        Button button = new Button("Start");
        button.setOnAction(e -> { 
            scaleTransition.play();
            button.setVisible(false);
        });
        
        VBox vbox = new VBox(50, stackPane, button); 
        vbox.setAlignment(Pos.CENTER);
        vbox.setStyle("-fx-background-color: gray");

        Scene scene = new Scene(vbox, 400, 300);
        stage.setTitle("Example 98. ScaleTransition");
        
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

This is the output during the animation:


No comments:

Post a Comment