I am trying a basic animation to move around a circle, but the stage is not showing the circle's new position.
package circler;
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.util.Duration;
public class Main extends Application {
Circle circle;
int distance = 100;
int angle = 0;
public void start(Stage primaryStage) {
circle = new Circle();
circle.setCenterX(400);
circle.setCenterY(400);
circle.setRadius(10);
circle.setFill(Color.RED);
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.1), e -> {
angle++;
angle %= 360;
setPosition();
primaryStage.show();
}));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
primaryStage.setScene(new Scene(new StackPane(circle), 800, 800));
primaryStage.show();
}
private int calc(boolean sin) {
return (int) (sin ? distance * Math.sin(Math.toRadians(angle)) : distance * Math.cos(Math.toRadians(angle)));
}
private void setPosition() {
circle.setCenterX(circle.getScene().getHeight() / 2 + calc(true));
circle.setCenterY(circle.getScene().getHeight() / 2 + calc(false));
System.out.println(circle.getCenterX());
}
}
I tried every page which tells about refreshing it. I want the circle to move 100ms and the stage should show it at the new position.