有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在画布的父画中画一个圆?

据我所知,画布的大小由其内容决定,除非另有规定。我的画布位于边框窗格的中心窗格内。左窗格为窗口宽度的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) 个答案