Reputation: 435
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.
Upvotes: 2
Views: 891
Reputation: 5123
You have to set a definite duration for your animation, e.g.
pt.setDuration(Duration.seconds(4));
This value determines the duration of one animation cycle.
Duration.INDEFINITE
is defined as 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.
Upvotes: 3