import javafx.stage.Stage;
import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.Scene;
import javafx.scene.text.*;
public class PieChart extends Application{
public void start(Stage primaryStage) {
final double WIDTH = 600;
final double HEIGHT = 600;
Pane pane = new Pane();
pane.setStyle("-fx-background-color: lightBlue");
Arc arc1 = new Arc(WIDTH / 2, HEIGHT / 2, 200, 200, 0, 72);
arc1.centerXProperty().bind(pane.widthProperty().divide(2));
arc1.centerYProperty().bind(pane.heightProperty().divide(2));
arc1.setType(ArcType.ROUND);
arc1.setFill(Color.RED);
Text text1 = new Text(375, 250, "Project -- 20%");
Arc arc2 = new Arc(WIDTH / 2, HEIGHT / 2, 200, 200, 72, 36);
arc2.centerXProperty().bind(pane.widthProperty().divide(2));
arc2.centerYProperty().bind(pane.heightProperty().divide(2));
arc2.setType(ArcType.ROUND);
arc2.setFill(Color.BLUE);
Text text2 = new Text(270, 95, "Quiz -- 10%");
Arc arc3 = new Arc(WIDTH / 2, HEIGHT / 2, 200, 200, 108, 108);
Text text3 = new Text(150, 250, "Midterm -- 30%");
arc3.centerXProperty().bind(pane.widthProperty().divide(2));
arc3.centerYProperty().bind(pane.heightProperty().divide(2));
arc3.setType(ArcType.ROUND);
arc3.setFill(Color.GREEN);
Arc arc4 = new Arc(WIDTH / 2, HEIGHT / 2, 200, 200, 216, 144);
Text text4 = new Text(300, 400, "Final -- 40%");
arc4.centerXProperty().bind(pane.widthProperty().divide(2));
arc4.centerYProperty().bind(pane.heightProperty().divide(2));
arc4.setType(ArcType.ROUND);
arc4.setFill(Color.ORANGE);
pane.getChildren().addAll(arc1, arc2, arc3, arc4, text1, text2, text3, text4);
Scene scene = new Scene(pane, WIDTH, HEIGHT);
primaryStage.setTitle("Pie Chart");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Link to what my program looks like
大家好,我有一个使用多个弧和文本创建pi图表的程序。我将centerX和centerY属性值绑定到我窗格的宽度和高度的一半,这样即使重新调整窗口,pi图也会保持在中心位置。但是我尝试以同样的方式将我的文本绑定到窗格,没有任何显示。到目前为止,我只是手动输入x和y值来将我的文本放在正确的位置,但是如果我重新调整窗口,则piChart会保留在中心但文本不会。如何编辑我的文本,使其在链接中的图片看起来更加动态,但即使我改变窗口的大小,也会保持这种状态?