如何将 JavaFX 中的矩形列表或 GridPane 缩放到窗口宽度?

时间:2021-08-01 08:23:39

标签: java javafx

我试图以类似矩阵的方式制作矩形列表,我希望它们根据行中矩形的数量进行缩放,以始终适合窗口的固定大小。根据矩形的数量,行尾被小切或其中几个在边界之外。

public class Main extends Application {

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

    @Override
    public void start(Stage stage) {
        final int size = 80;
        stage.setWidth(1040);
        stage.setHeight(920);
        final double cellDimension = (stage.getWidth() / size) - 1;
        stage.setScene(new Scene(render(20, size, cellDimension), Color.WHITE));
        stage.show();
    }

    public static Region render(int generations, int size, double cellDimension) {
        VBox results = new VBox(0);
        Random random = new Random();
        for (int y = 0; y < generations; y++) {
            HBox hBox = new HBox(0);
            hBox.getChildren().addAll(IntStream.range(0, size).
                    mapToObj(idx -> random.nextInt(2)).map(item -> createAppropriateRectangle(item == 1, cellDimension)).
                    collect(Collectors.toList()));
            results.getChildren().add(hBox);
        }
        results.setSnapToPixel(true);
        return results;
    }

    private static Rectangle createAppropriateRectangle(boolean state, double cellDimension) {
        Rectangle rectangle = new Rectangle(cellDimension, cellDimension);
        rectangle.setStroke(Color.GRAY);
        if (state) {
            rectangle.setFill(Color.WHITE);
        }
        return rectangle;
    }
}

我做错了什么?如果有什么不清楚的请告诉我,谢谢:) for list of 90 values, up is original, bottom shows how much was outside of border

编辑: 使用@DaveB 剥离示例制作 mwe

2 个答案:

答案 0 :(得分:0)

构建 MRE 可能会很快向您展示答案。看起来使用 GridPane 只是尝试使矩形适合,但 GridPane 可能会增加可能会掩盖答案的复杂性。所以我使用了一个 VBox,里面有一堆 HBox。这可以毫不费力地处理元素的相对定位。

您没有包含 Generator 类,这意味着您的代码无法运行,因此我将其简化为在屏幕上放置一些随机黑白方块的机制,并转储其他所有内容:

public class Squares extends Application {

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

    @Override
    public void start(Stage stage) {
        final int size = 80;
        stage.setWidth(1040);
        stage.setHeight(920);
        final double cellDimension = (stage.getWidth() / size) - 1;
        stage.setScene(new Scene(render(20, size, cellDimension), Color.WHITE));
        stage.show();
    }

    public static Region render(int generations, int size, double cellDimension) {
        VBox results = new VBox(0);
        Random random = new Random();
        for (int y = 0; y < generations; y++) {
            HBox hBox = new HBox(0);
            hBox.getChildren().addAll(IntStream.range(0, size).mapToObj(idx -> random.nextInt(2)).map(item -> createAppropriateRectangle(item == 1, cellDimension)).collect(Collectors.toList()));
            results.getChildren().add(hBox);
        }
        return results;
    }

    private static Rectangle createAppropriateRectangle(boolean state, double cellDimension) {
        Rectangle rectangle = new Rectangle(cellDimension, cellDimension);
        rectangle.setStroke(Color.GRAY);
        if (state) {
            rectangle.setFill(Color.WHITE);
        }
        return rectangle;
    }
}

看起来您需要为每个矩形允许 1 个额外像素才能使数学正常工作。此外,它似乎只有当舞台宽度很好地除以每行的矩形数时才起作用 - 这里可能有一些部分像素的机制起作用。当行大小不是阶段宽度的偶数除数时,您可能会找到一种四舍五入策略。

答案 1 :(得分:-1)

您可以将其绑定到舞台或父级宽度、高度属性。 绑定是 JavaFX 最强大的概念之一。