Friday, March 2, 2012

JavaFX 2.0: Implement setOnFinished() to handle end of Transition


package JavaFX_UI;

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;
    TranslateTransition transitionForward, transitionBackward;
    
    @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);
        
        transitionForward = createTransitionForward(imageView);
        transitionBackward = createTransitionBackward(imageView);
        
        Button btnStart = new Button();
        btnStart.setText("Start");
        btnStart.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                transitionForward.play();
            }
        });
        
        Button btnStop = new Button();
        btnStop.setText("Stop");
        btnStop.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                transitionForward.stop();
                transitionBackward.stop();
            }
        });
        
        vBox.getChildren().add(imageView);
        vBox.getChildren().add(btnStart);
        vBox.getChildren().add(btnStop);
        
        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();
        
        transition.setOnFinished(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent arg0) {
                transitionBackward.play();
            }
        });

        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();
        
        transition.setOnFinished(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent arg0) {
                transitionForward.play();
            }
        });

        return transition;
    }
}


1 comment:

  1. hi
    in this program stop button willnot work
    if u can try as i said maximimize the stage and and first press "start" button and
    after that try to press "stop" button it will not work and stage also stage also wont close
    if u can try

    because as my exp in transitions if u give timeline i.e. transition.setCycleCount(Timeline.INDEFINITE)
    and if u maximize the stage stage wont close
    and other controls also wont work

    this program is also like that only

    if u have solution for that i.e.if u can close stage properly after maximize stage in
    transition.setCycleCount(Timeline.INDEFINITE)
    or
    in ur prog first maximize stage and press start after that try to stop or close if immediately those two functionalists are working properly

    please message me
    my mail id is srihari1203@gmail.com
    and iam waiting for ur replay

    ReplyDelete