如何在JavaFX中画布'父级的中间画一个圆圈?

时间:2017-01-30 15:14:22

标签: java javafx

根据我的理解,除非另有说明,否则画布的大小由其内容决定。我的画布位于BorderPane的中心窗格内。左窗格是窗口宽度的15%,是用户屏幕宽度的75%。因此,中心窗格的剩余宽度是窗口宽度的85%。我想在窗口宽度的剩余85%的中间绘制一个圆,或者换句话说,在中心窗格的中心绘制一个圆圈。

目前,就宽度而言,圆圈偏离中心,但高度正确。我做错了什么?

这是我的代码:

public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    public double windowWidth = screenSize.getWidth() * .75;
    public double windowHeight = screenSize.getHeight() * .75;

    @Override
    public void start(Stage primaryStage) {

        BorderPane root = new BorderPane();

        Button regenerate = new Button("Regenerate Board");
        regenerate.setPrefWidth(windowWidth * .15 * .80);
        regenerate.getStyleClass().add("spButton");

        Button settings = new Button("Game Settings");
        settings.setPrefWidth(windowWidth * .15 * .80);
        settings.getStyleClass().add("spButton");

        Button start = new Button("Start Game");
        start.setPrefWidth(windowWidth * .15 * .80);
        start.getStyleClass().add("spButton");

        VBox bpLeft = new VBox(20);
        bpLeft.setPrefWidth(windowWidth * .15);
        //bpLeft.resize(((screenSize.getWidth() * .75) *.20), screenSize.getHeight() * .75);
        bpLeft.getStyleClass().add("bpLeft");
        bpLeft.getChildren().addAll(regenerate, settings, start);

        root.setLeft(bpLeft);

        //Circle cir = new Circle();
        double originX = (windowWidth * .15) + ((windowWidth * .85) / 2);
        double originY = (windowHeight / 2);
        double radius = windowHeight * .90;
        /*cir.setCenterX(originX);
        cir.setCenterY(originY);
        cir.setRadius(windowHeight * (.90 / 2));        
        cir.setFill(Color.rgb(87, 175, 255));*/

        //Hexagon hex = new Hexagon(new Point((int)originX, (int)originY), 10);

        Pane wrapperPane = new Pane();
        root.setCenter(wrapperPane);
        // Put canvas in the center of the window
        Canvas canvas = new Canvas((int)Math.round(windowWidth * .85), (int)Math.round(windowHeight));
        root.setAlignment(canvas, Pos.CENTER_LEFT);

        GraphicsContext gc = canvas.getGraphicsContext2D();
        drawCircle(gc, new Point((int)originX, (int)originY), (int)radius, true, true, 0x57AFFF, 3);
        wrapperPane.getChildren().add(canvas);

        //root.getChildren().add(btn);
        Scene scene = new Scene(root, windowWidth, windowHeight);

        scene.getStylesheets().add(HelloWorld.class.getResource("Catan.css").toExternalForm());
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

}

public void drawCircle(GraphicsContext g, Point origin, int radius,
            boolean centered, boolean filled, int colorValue, int lineThickness) {
        g.setFill(Color.BLUE);
        g.fillOval(origin.x - (radius / 2), origin.y - (radius / 2), radius, radius);
    }

这是我目前的输出: enter image description here

我做错了什么?

0 个答案:

没有答案