[英]Getting the center of a circle that using property binding Javafx
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
.但是,如果我想获得中心的 X 和 Y 坐标,它们的值为
0.00
。
I used circle.getCenterX();
我用
circle.getCenterX();
and circle.centerXProperty()
.和
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();
}
}
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:
您可以通过使用绑定(如使用 pane.heightProperty)或使用 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()));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.