简体   繁体   English

JavaFX PathTransition动画无法播放

[英]JavaFX PathTransition animation not playing

I have some code which is supposed to animate a circle along the path of an arc: 我有一些代码应该沿弧线的路径动画一个圆:

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

 }

}

However, when I run the program, no animation takes place. 但是,当我运行该程序时,没有动画发生。 The circle appears at the beginning of the arc, and nothing happens: 该圆出现在圆弧的起点,但没有任何反应:

在此处输入图片说明

Please help me understand why. 请帮助我理解原因。

You have to set a definite duration for your animation, eg 您必须为动画设置一定的持续时间,例如

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

This value determines the duration of one animation cycle. 此值确定一个动画周期的持续时间。

Duration.INDEFINITE is defined as Duration(Double.POSITIVE_INFINITY) . Duration.INDEFINITE定义为Duration(Double.POSITIVE_INFINITY) Using it will make the animation play with infinite duration, causing the interpolation steps to become too small to have an effect on the animated node. 使用它会使动画无限持续播放,从而导致插值步骤变得太小而无法对动画节点产生影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM