如何在 JavaFX 上调整 canvas 的大小以适应大小

How to resize a canvas on JavaFX to fit the size

我是 JavaFX 的新手,在调整 canvas 的大小时遇到​​了问题。 我正在尝试使 canvas 大小完全适合我放入其中的内容。 输出应该是唯一的片段 5/7,但我在我绘制的数字旁边得到了很多白色 space。 我尝试使用 setWidth 调整 canvas 的大小,甚至从一开始就将 canvas 设置为小尺寸,但似乎没有任何帮助。

这是我的代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class Example extends Application {

    Font fontLarge = Font.font("Droid Sans", FontWeight.BOLD, 15);

    @Override
    public void start(Stage stage) {

        HBox root = new HBox();
        Scene scene = new Scene(root);

        root.getChildren().add(getCanvasOfRationalNumber("5", "7"));
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }

    public Canvas getCanvasOfRationalNumber(String num, String denom) {
        final Canvas rnCanvas = new Canvas(300, 55);
        GraphicsContext gc = rnCanvas.getGraphicsContext2D();
        gc.setFont(fontLarge);

        gc.fillText(num, 0, 15);
        gc.fillText("___", 0, 20);
        gc.fillText(denom, 0, 40);

        rnCanvas.setWidth(15);
        return rnCanvas;
    }

    public static void main(String[] args) {
        launch(args);
    }

}

编辑:我之前的回答是错误的,这里是新的。

我们需要澄清您真正想要的是什么。如果我现在理解正确的话,你想要的是 window 尺寸本身不大于 Canvas 尺寸。

正如 jewelsea 在评论中指出的那样,您可以更改 Canvas 宽度和高度。事实上,您已经在代码中这样做了。

我建议您为 Canvas 和场景选择不同的颜色。如果你用 e 这样做。 g.:

public class Example extends Application {

    Font fontLarge = Font.font("Droid Sans", FontWeight.BOLD, 15);

    @Override
    public void start(Stage stage) {

        HBox root = new HBox();
        Scene scene = new Scene(root, Color.YELLOW);

        root.getChildren().add(getCanvasOfRationalNumber("5", "7"));
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }

    public Canvas getCanvasOfRationalNumber(String num, String denom) {
        final Canvas rnCanvas = new Canvas(300, 55);
        GraphicsContext gc = rnCanvas.getGraphicsContext2D();
        gc.setFont(fontLarge);

        gc.setFill(Color.LIGHTBLUE);
        gc.fillRect(0, 0, 300, 55);

        gc.setFill(Color.BLUE);

        gc.fillText(num, 0, 15);
        gc.fillText("___", 0, 20);
        gc.fillText(denom, 0, 40);

        rnCanvas.setWidth(15);
        return rnCanvas;
    }

    public static void main(String[] args) {
        launch(args);
    }

}

你会得到这个

您清楚地看到 Canvas 已调整大小。您面临的问题是 window 不能更小。那是场景中的白色(或此处为黄色)space,而不是 Canvas。您可以尝试将宽度设置为 1,将高度设置为 1,但您仍然会得到这样的结果:

我的猜测是 window 大小不能因为 min/max/close 按钮而变小。您可以使用 initStyle 方法更改样式以摆脱按钮。

一切都取决于您的要求。


旧(错误)答案:

您无法修改 Canvas 的 width/height。

Canvas is an image that can be drawn on using a set of graphics commands provided by a GraphicsContext.

A Canvas node is constructed with a width and height that specifies the size of the image into which the canvas drawing commands are rendered. All drawing operations are clipped to the bounds of that image.

您的解决方案是将 canvas 区域复制到另一个尺寸的新 canvas 或根本不使用 canvas,而是使用 e。 G。一个 Text 节点。但这完全取决于您的要求。