JavaFX How to - Create Centered Circle binded to Scene








Question

We would like to know how to create Centered Circle binded to Scene.

Answer

//ww  w . j  a v a2 s . com
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage stage) {
    Circle c = new Circle();
    Group root = new Group(c);
    Scene scene = new Scene(root, 100, 100);

    c.centerXProperty().bind(scene.widthProperty().divide(2));
    c.centerYProperty().bind(scene.heightProperty().divide(2));
    c.radiusProperty().bind(Bindings.min(scene.widthProperty(),
                                         scene.heightProperty())
                                    .divide(2));

    stage.setTitle("A Centered Circle");
    stage.setScene(scene);
    stage.sizeToScene();
    stage.show();
  }
}