0

How can I find the center of a circle after binding it to the pane?

circle.centerXProperty().bind(pane.widthProperty().divide(2));
circle.centerYProperty().bind(pane.heightProperty().divide(2));

I used the code above to bind the properties of the circle. However, if I want to get the X and Y coordinates of the center, their values are 0.00.

I used circle.getCenterX(); and circle.centerXProperty().

Can anyone help me in figuring out how to get the right center of the circle?


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.text.*;
import java.lang.Math;

public class CharactersAroundCircle extends Application {


    public void start(Stage primaryStage){

        String str = "Welcome To Java";
        Circle circle = new Circle();
        System.out.println(str.length());
        Pane pane = new Pane();
        circle.setRadius(125);
        circle.centerXProperty().bind(pane.widthProperty().divide(2));
        circle.centerYProperty().bind(pane.heightProperty().divide(2));

        circle.setStroke(Color.BLACK);
        circle.setFill(new Color(1,1,1,0));


            Text text = new Text();
            text.setText(String.valueOf(str.charAt(0)));
            text.setStroke(Color.BLACK);
            text.setFont(Font.font("Times New Roman", 20));
            // text.setX(circle.getCenterX() + Math.toRadians(Math.sin(360 / str.length()) * circle.getRadius()));
            // text.setY(circle.getCenterY() + Math.toRadians(Math.cos(360 / str.length()) * circle.getRadius()));
            text.setX(circle.getCenterX() + 100);
            text.setY(circle.getCenterY() + 100);
            System.out.println(circle.centerXProperty());
            System.out.println(circle.getCenterY());
            pane.getChildren().add(text);


        pane.getChildren().add(circle);

        Scene scene = new Scene(pane, 300, 300);
        primaryStage.setTitle("CharactersAroundCircle");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Polygnome
  • 7,639
  • 2
  • 37
  • 57
  • 1
    What values do you expect and what values do you get? – Polygnome Jan 16 '20 at 11:48
  • The actual output seems to be `0.00`, but you are right, @Polygnome, we need the expected values, too. – deHaar Jan 16 '20 at 11:49
  • Also, you never add the pane that contains the circle to the scene. So the pane has no width or height, thus the center of the circle is 0/0. – Polygnome Jan 16 '20 at 11:58
  • `System.out.println(circle.centerXProperty()); System.out.println(circle.getCenterY());` should print the expected values after `primaryStage.show();` – c0der Jan 16 '20 at 13:02

1 Answers1

0

Your problem is that you don't access the circles values dynamically. You can solve this by using bindings (like with pane.heightProperty) or use a ChangeListener:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class CharactersAroundCircle extends Application implements ChangeListener<Number> {
    Circle circle;
    Text text;
    String str;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        str = "Welcome To Java";
        circle = new Circle();
        System.out.println(str.length());
        Pane pane = new Pane();
        circle.setRadius(125);
        circle.centerXProperty().bind(pane.widthProperty().divide(2));
        circle.centerYProperty().bind(pane.heightProperty().divide(2));
        pane.widthProperty().addListener(this);
        pane.heightProperty().addListener(this);

        circle.setStroke(Color.BLACK);
        circle.setFill(new Color(1, 1, 1, 0));

        text = new Text();
        text.setText(String.valueOf(str.charAt(0)));
        text.setStroke(Color.BLACK);
        text.setFont(Font.font("Times New Roman", 20));
        pane.getChildren().add(text);

        pane.getChildren().add(circle);

        Scene scene = new Scene(pane, 300, 300);
        primaryStage.setTitle("CharactersAroundCircle");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    @Override
    public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
        text.setX(circle.getCenterX() + Math.toRadians(Math.sin(360 / str.length()) * circle.getRadius()));
        text.setY(circle.getCenterY() + Math.toRadians(Math.cos(360 / str.length()) * circle.getRadius()));
    }
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Ralf Renz
  • 1,061
  • 5
  • 7