簡體   English   中英

獲取使用屬性綁定 Javafx 的圓心

[英]Getting the center of a circle that using property binding Javafx

將圓心綁定到窗格后,如何找到圓心

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

我用上面的代碼綁定了圓的屬性。 但是,如果我想獲得中心的 X 和 Y 坐標,它們的值為0.00

我用circle.getCenterX(); circle.centerXProperty()

誰能幫我弄清楚如何獲得正確的圓心?


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();
    }
}

您的問題是您沒有動態訪問圓圈值。 您可以通過使用綁定(如使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM