Tuesday, February 28, 2012

JavaFX 2.0: TranslateTransition



package javafx_imagetransition;

import javafx.animation.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
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_ImageTransition extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    Scene scene;
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("http://java-buddy.blogspot.com/");
        StackPane root = new StackPane();
        scene = new Scene(root, 300, 250);
        
        VBox vBox = new VBox();
        vBox.setSpacing(3);
        
        final Image image1 = new Image(getClass().getResourceAsStream("duke_44x80.png"));
        final ImageView imageView = new ImageView();
        imageView.setImage(image1);
        
        final TranslateTransition transitionForward 
                = createTransitionForward(imageView);
        final TranslateTransition transitionBackward 
                = createTransitionBackward(imageView);
 
        Button btnForward = new Button();
        btnForward.setText("Forward");
        btnForward.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                transitionForward.play();
            }
        });
        
        Button btnBackward = new Button();
        btnBackward.setText("Backward");
        btnBackward.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                transitionBackward.play();
            }
        });
        
        vBox.getChildren().add(imageView);
        vBox.getChildren().add(btnForward);
        vBox.getChildren().add(btnBackward);
        
        root.getChildren().add(vBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    TranslateTransition createTransitionForward(final ImageView iv){
        TranslateTransition transition = TranslateTransitionBuilder.create()
                .node(iv)
                .fromX(0)
                .toX(scene.widthProperty().doubleValue() - iv.getImage().getWidth())
                .fromY(0)
                .toY(scene.heightProperty().doubleValue() - iv.getImage().getHeight())
                .duration(Duration.millis(1000))
                .interpolator(Interpolator.LINEAR)
                .cycleCount(1)
                .build();
        
        return transition;
    }
    
    TranslateTransition createTransitionBackward(final ImageView iv){
        TranslateTransition transition = TranslateTransitionBuilder.create()
                .node(iv)
                .fromX(scene.widthProperty().doubleValue() - iv.getImage().getWidth())
                .toX(0)
                .fromY(scene.heightProperty().doubleValue() - iv.getImage().getHeight())
                .toY(0)
                .duration(Duration.millis(1000))
                .interpolator(Interpolator.LINEAR)
                .cycleCount(1)
                .build();
        
        return transition;
    }
}



Read more: Implement setOnFinished() to handle end of Transition

No comments:

Post a Comment