1 回答

TA貢獻1770條經驗 獲得超3個贊
我建議使用Rotate轉換。這樣您只需要設置初始位置和樞軸點,并且可以限制對Rotate.angle屬性的更新。
以下示例使用 aTimeline為屬性設置動畫,但這可以通過moveCircle使用 方法輕松完成rotate.setAngle(angleDegrees);:
@Override
public void start(Stage primaryStage) {
Pane root = new Pane();
root.setMinSize(500, 500);
final double radius = 150;
final double centerX = 250;
final double centerY = 250;
final double height = 40;
Circle circle = new Circle(centerX, centerY, radius, null);
circle.setStroke(Color.BLACK);
// rect starts at the rightmost point of the circle touching it with the left midpoint
Rectangle rect = new Rectangle(centerX + radius, centerY - height / 2, 10, height);
rect.setFill(Color.RED);
Rotate rotate = new Rotate(0, centerX, centerY); // pivot point matches center of circle
rect.getTransforms().add(rotate);
// animate one rotation per 5 sec
Timeline animation = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(rotate.angleProperty(), 0d)),
new KeyFrame(Duration.seconds(5), new KeyValue(rotate.angleProperty(), 360d)));
animation.setCycleCount(Animation.INDEFINITE);
animation.play();
root.getChildren().addAll(circle, rect);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
順便說一句:您的代碼的以下部分看起來很奇怪
double newX = getX() + (radius * Math.cos(Math.toDegrees(angle)));
double newY = getY() + (radius * Math.sin(Math.toDegrees(angle)));
Math.sin并Math.cos期望弧度,而不是度數。您要么需要使用toRadians,要么不需要轉換...
添加回答
舉報