import javafx.animation.Interpolator;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * Animation: a dot moves around a circular path. Clicking the mouse causes the dot to 
 * reverse direction.
 * 
 * @author Drue Coles
 */
public class AroundTheWorld extends Application {

    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();
        final int size = 300;
        Scene scene = new Scene(root, size, size, Color.ALICEBLUE);

        // Create a circle that nearly fills the scene.
        Circle world = new Circle(size / 2 - 10);
        world.setStroke(Color.DARKGREEN);
        world.setStrokeWidth(3);
        world.setFill(Color.DARKSEAGREEN);

        // Create a dot that travels around the world.
        Circle dot = new Circle(6);
        dot.setFill(Color.DARKGREEN);
        root.getChildren().addAll(world, dot);

        // Path transition for dot traveling around the circle
        PathTransition pTrans = new PathTransition();
        pTrans.setDuration(Duration.millis(3000));
        pTrans.setPath(world);
        pTrans.setNode(dot);
        pTrans.setInterpolator(Interpolator.LINEAR);
        pTrans.setCycleCount(Timeline.INDEFINITE);
        pTrans.setAutoReverse(false);
        pTrans.play();        
      
        // Clicking the mouse changes the direction of the dot along the path
        root.setOnMouseClicked(e -> pTrans.setRate(-pTrans.getRate()));
        
        primaryStage.setTitle("Around the World");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
   
    public static void main(String[] args) {
        launch(args);
    }
}