1  import javafx.animation.PathTransition;
  2  import javafx.animation.Timeline;
  3  import javafx.application.Application;
  4  import javafx.scene.Scene;
  5  import javafx.scene.layout.Pane;
  6  import javafx.scene.paint.Color;
  7  import javafx.scene.shape.Rectangle;
  8  import javafx.scene.shape.Circle;
  9  import javafx.stage.Stage;
 10  import javafx.util.Duration;
 11  
 12  public class PathTransitionDemo extends Application {
 13    @Override // Override the start method in the Application class
 14    public void start(Stage primaryStage) {
 15      // Create a pane 
 16      Pane pane = new Pane();
 17      
 18      // Create a rectangle
 19      Rectangle rectangle = new Rectangle(0, 0, 25, 50);
 20      rectangle.setFill(Color.ORANGE);
 21      
 22      // Create a circle
 23      Circle circle = new Circle(125, 100, 50);
 24      circle.setFill(Color.WHITE);
 25      circle.setStroke(Color.BLACK);
 26      
 27      // Add circle and rectangle to the pane
 28      pane.getChildren().add(circle);
 29      pane.getChildren().add(rectangle);
 30      
 31      // Create a path transition 
 32      PathTransition pt = new PathTransition();
 33      pt.setDuration(Duration.millis(4000));
 34      pt.setPath(circle);
 35      pt.setNode(rectangle);
 36      pt.setOrientation(
 37        PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
 38      pt.setCycleCount(Timeline.INDEFINITE);
 39      pt.setAutoReverse(true);
 40      pt.play(); // Start animation 
 41      
 42      circle.setOnMousePressed(e -> pt.pause());
 43      circle.setOnMouseReleased(e -> pt.play());
 44      
 45      // Create a scene and place it in the stage
 46      Scene scene = new Scene(pane, 250, 200);
 47      primaryStage.setTitle("PathTransitionDemo"); // Set the stage title
 48      primaryStage.setScene(scene); // Place the scene in the stage
 49      primaryStage.show(); // Display the stage
 50    }
 51    
 52    /**
 53     * The main method is only needed for the IDE with limited
 54     * JavaFX support. Not needed for running from the command line.
 55     */
 56    public static void main(String[] args) {
 57      launch(args);
 58    }
 59  }