Thursday, September 17, 2015

JavaFX Transition Animation with Reflection effect

JavaFX example to combine Transition Animation and Reflection effect.



package javafx_transitionexample;

import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.RotateTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_TransitionExample extends Application {

    @Override
    public void start(Stage primaryStage) {

        //Transition Example
        //ref: http://docs.oracle.com/javafx/2/animations/basics.htm
        ImageView imageView = new ImageView(new Image("http://goo.gl/kYEQl"));
        
        Reflection reflection = new Reflection();
        imageView.setEffect(reflection);

        FadeTransition fadeTransition
                = new FadeTransition(Duration.millis(3000), imageView);
        fadeTransition.setFromValue(1.0f);
        fadeTransition.setToValue(0.3f);
        fadeTransition.setCycleCount(2);
        fadeTransition.setAutoReverse(true);
         
        TranslateTransition translateTransition
                = new TranslateTransition(Duration.millis(2000), imageView);
        translateTransition.setFromX(0);
        translateTransition.setToX(350);
        translateTransition.setCycleCount(2);
        translateTransition.setAutoReverse(true);
 
        RotateTransition rotateTransition
                = new RotateTransition(Duration.millis(3000), imageView);
        rotateTransition.setByAngle(180f);
        rotateTransition.setCycleCount(4);
        rotateTransition.setAutoReverse(true);
 
        ScaleTransition scaleTransition
                = new ScaleTransition(Duration.millis(2000), imageView);
        scaleTransition.setToX(2f);
        scaleTransition.setToY(2f);
        scaleTransition.setCycleCount(2);
        scaleTransition.setAutoReverse(true);
 
        ParallelTransition parallelTransition = 
                 new ParallelTransition();
        parallelTransition.getChildren().addAll(
                fadeTransition,
                translateTransition,
                rotateTransition,
                scaleTransition);
         parallelTransition.setCycleCount(1);
         //parallelTransition.play();
         
        SequentialTransition sequentialTransition = 
                new SequentialTransition();
        sequentialTransition.getChildren().addAll(
                fadeTransition,
                translateTransition,
                rotateTransition,
                scaleTransition);
        sequentialTransition.setCycleCount(1);
        //sequentialTransition.play();
        //
         
        Button btnParallelTransitionPlay = 
                new Button("parallelTransition.play");
        btnParallelTransitionPlay.setOnAction((ActionEvent event) -> {
            parallelTransition.play();
        });
 
        Button btnSequentialTransitionPlay = 
                new Button("sequentialTransition.play");
        btnSequentialTransitionPlay.setOnAction((ActionEvent event) -> {
            sequentialTransition.play();
        });
 
        HBox hbox = new HBox();
        hbox.getChildren().addAll(
                btnParallelTransitionPlay,
                imageView,
                btnSequentialTransitionPlay);
         
        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version") + "\n"
                + "os.name: " + System.getProperty("os.name")
        );
         
        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, hbox);
         
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
 
        Scene scene = new Scene(root, 700, 250);
 
        primaryStage.setTitle("java-buddy: Transition Example "
                + "- ParallelTransition/SequentialTransition");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}


No comments:

Post a Comment