Monday, March 5, 2012

JavaFX 2.0: PathTransition - transition along path



package javafx_path;

import javafx.animation.PathTransition;
import javafx.animation.PathTransitionBuilder;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
*
* @web java-buddy.blogspot.com
*/
public class JavaFX_Path extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

final Image image1 = new Image(getClass().getResourceAsStream("duke_44x80.png"));
final ImageView imageView = new ImageView();
imageView.setImage(image1);

Path path = new Path();
path.getElements().add(new MoveTo(50, 50));
path.getElements().add(new LineTo(50, 200));
path.getElements().add(new LineTo(100, 200));
path.getElements().add(new LineTo(100, 100));
path.getElements().add(new LineTo(200, 100));
path.setStrokeWidth(1);
path.setStroke(Color.BLACK);

final PathTransition pathTransition = PathTransitionBuilder.create()
.node(imageView)
.path(path)
.duration(Duration.millis(5000))
.orientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT)
.cycleCount(1)
.build();

Button btnStart = new Button("playFromStart");
btnStart.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
pathTransition.playFromStart();
}
});

root.getChildren().add(btnStart);
root.getChildren().add(imageView);
root.getChildren().add(path);
primaryStage.setScene(scene);
primaryStage.show();

}
}


No comments:

Post a Comment