A node can travel along a Path. Our path is 4 lines near the 4 sides of the window.
The node is a Rectangle and it moves over the path in 5 seconds.
package ex99;
import javafx.animation.Interpolator;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Ex99 extends Application {
@Override
public void start(Stage primaryStage) {
Rectangle rect = new Rectangle(50, 50, 20, 20);
rect.setFill(Color.RED);
Path path = new Path(new MoveTo(50, 50),
new LineTo(350, 50),
new LineTo(350, 350),
new LineTo(50, 350),
new ClosePath());
PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(Duration.seconds(5));
pathTransition.setNode(rect);
pathTransition.setPath(path);
pathTransition.setAutoReverse(true);
pathTransition.setCycleCount(Timeline.INDEFINITE);
pathTransition.setInterpolator(Interpolator.LINEAR);
Group root = new Group(rect);
Scene scene = new Scene(root, 400, 400);
pathTransition.play();
primaryStage.setTitle("Example 99. PathTransition");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the output during the animation (on the left side):
No comments:
Post a Comment