Wednesday, May 28, 2014

JavaFX 8 PauseTransition example

Example of using PauseTransition to insert a pause between transitions in SequentialTransition.


package javafx8_animation;

import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.animation.PauseTransition;
import javafx.animation.RotateTransition;
import javafx.animation.SequentialTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 350, Color.BLACK);
        scene.setFill(Color.WHITE);

        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect = new Rectangle(150, 150, 50, 50);
        rect.setFill(linearGradientTransparence);
        rect.setStroke(Color.BLUE);
        rect.setStrokeWidth(3);
        
        MoveTo moveTo = new MoveTo();
        moveTo.setX(175f);
        moveTo.setY(175f);
        
        ArcTo arcTo = new ArcTo();
        arcTo.setX(375f);
        arcTo.setY(175f);
        arcTo.setRadiusX(100f);
        arcTo.setRadiusY(50f);
        
        Path path = new Path();
        path.getElements().add (moveTo);
        path.getElements().add (arcTo);

        PathTransition pathTransition = new PathTransition();
        pathTransition.setDuration(Duration.millis(2000));
        pathTransition.setPath(path);
        pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
        pathTransition.setCycleCount(2);
        pathTransition.setAutoReverse(true);
        
        //pause for 2 second
        PauseTransition pauseTransition = 
            new PauseTransition(Duration.millis(2000)); 
 
        RotateTransition rotateTransition = 
            new RotateTransition(Duration.millis(2000));
        rotateTransition.setByAngle(360);
        rotateTransition.setCycleCount(2);
        rotateTransition.setAutoReverse(true);
        
        SequentialTransition sequentialTransition2 = 
            new SequentialTransition (
                    rect, 
                    pathTransition, 
                    pauseTransition,
                    rotateTransition);

        rect.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            sequentialTransition2.play();
        });
        
        root.getChildren().add(rect);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

No comments:

Post a Comment