JavaFX PathTransition动画没有播放

时间:2015-05-01 18:43:26

标签: java animation javafx

我有一些代码可以沿着弧形路径为圆圈设置动画:

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();

 }

}

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

enter image description here

请帮助我理解原因。

1 个答案:

答案 0 :(得分:3)

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

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

此值确定一个动画周期的持续时间。

Duration.INDEFINITE定义为Duration(Double.POSITIVE_INFINITY)。使用它会使动画播放的持续时间无限,导致插值步骤变得太小而无法对动画节点产生影响。