JavaFX PathTransition 动画不播放

JavaFX PathTransition animation not playing

我有一些代码应该沿着圆弧的路径为圆制作动画:

package event_handling;

import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class PalindromeSwing extends Application {

public static void main(String[] args) {
    Application.launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {

    Pane pane = new Pane();

    System.out.println(pane.getWidth());

    Arc a = new Arc(100, 100, 100, 100, -135, 90);
    a.setType(ArcType.OPEN);
    a.setStroke(Color.BLACK);
    a.setFill(Color.TRANSPARENT);

    Circle c = new Circle(5);

    pane.getChildren().addAll(a, c);

    PathTransition pt = new PathTransition();

    pt.setDuration(Duration.INDEFINITE);
    pt.setNode(c);
    pt.setPath(a);
    pt.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
    pt.setCycleCount(Timeline.INDEFINITE);
    pt.setAutoReverse(true);
    pt.play();

    Scene scene = new Scene(pane, 400, 400);

    primaryStage.setTitle("Animated circle");
    primaryStage.setScene(scene);
    primaryStage.show();

 }

}

但是,当我运行程序时,没有动画发生。圆圈出现在圆弧的起点,没有任何反应:

请帮我理解为什么。

您必须为动画设置明确的持续时间,例如

pt.setDuration(Duration.seconds(4));

这个值决定了一个动画周期的持续时间。

Duration.INDEFINITE 定义为 Duration(Double.POSITIVE_INFINITY)。使用它会使动画无限播放,导致插值步长过小而无法对动画节点产生影响。