简体   繁体   English

如何在JavaFX上调整画布的大小以适合其大小

[英]How to resize a canvas on JavaFX to fit the size

I am new to JavaFX and I encountered a problem resizing a canvas. 我是JavaFX的新手,在调整画布大小时遇到​​问题。 I am trying to get the canvas size to fit exactly the contents I place into it. 我正在尝试使画布尺寸完全适合放入其中的内容。 The output should be the only the fragment 5/7, but I get a lot of white space right next to the numbers I drew. 输出应该是唯一的片段5/7,但是在我绘制的数字旁边有很多空白。 I tried resizing the canvas using setWidth and even setting the canvas on small size from the begining but nothing seems to help. 我尝试使用setWidth调整画布的大小,甚至从一开始就将画布设置为小尺寸,但似乎无济于事。

Here is my code: 这是我的代码:

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

}

Edit: My previous answer was wrong, here's the new one. 编辑:我以前的答案是错误的,这是新的答案。

We need to clarify on what you really want. 我们需要澄清您的真正需求。 If I understand correctly now what you want is that the window size itself isn't larger than the Canvas size. 如果我现在正确理解,您想要的是窗口大小本身不大于Canvas大小。

As jewelsea pointed out in the comments you can change the Canvas width and height. 如jewelsea在评论中指出的,您可以更改“画布”的宽度和高度。 In fact you are doing it already in your code. 实际上,您已经在代码中这样做了。

I suggest you choose different colors for the Canvas and the Scene. 我建议您为“画布”和“场景”选择不同的颜色。 If you do that with eg: 如果您使用例如:

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

}

you'll get this 你会得到这个

在此处输入图片说明

You clearly see that the Canvas has been resized. 您清楚地看到了画布的大小。 The problem you are facing is that the window can't be smaller. 您面临的问题是窗口不能更小。 That's the white (or here yellow) space from the Scene, not from the Canvas. 那是场景中的白色(或此处为黄色)空间,而不是“画布”中的空间。 You could try setting the width to 1 and height to 1, but you'll still end up with this: 您可以尝试将width设置为1,height设置为1,但最终仍然会这样:

在此处输入图片说明

My guess is that the window size can't become smaller because of the min/max/close buttons. 我的猜测是,由于最小/最大/关闭按钮,窗口大小无法变小。 You could change the style using the initStyle method to get rid of the buttons. 您可以使用initStyle方法更改样式以摆脱按钮。

It all comes down to what your requirements are. 这一切都取决于您的要求。


Old (wrong) answer: 旧的(错误的)答案:

You can't modify the width/height of the Canvas . 您无法修改Canvas的宽度/高度。

Canvas is an image that can be drawn on using a set of graphics commands provided by a GraphicsContext. 画布是可以使用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. Canvas节点的宽度和高度可以指定画布绘制命令在其中渲染的图像的大小。 All drawing operations are clipped to the bounds of that image. 所有绘图操作都被裁剪到该图像的边界。

Your solution would be either to copy the canvas area to a new canvas with another size or don't use a canvas at all, instead use eg a Text node. 您的解决方案是将画布区域复制到其他大小的新画布,或者根本不使用画布,而使用例如Text节点。 But that all depends on your requirements. 但这一切都取决于您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM