java - 如何从下往上添加网格中的对象?

标签 java javafx gridpane

我使用网格 Pane View 通过 add() 方法填充网格。通过 add() 的设计,网格从上到下填充每一行,其中位置 (0,0) 位于网格的最左上角。添加更多行然后附加在其下方,依此类推。是否可以填充我的网格,使第一行位于底部并向上添加行,以便位置 (0,0) 位于左下角?要实现这一目标需要什么?我查看了 GridPane 中的不同方法,但找不到这是如何完成的。我的预感是我需要重写 add() 方法,但我不确定如何实现此行为。

我不想镜像这个,因为我正在处理图像。

为了简单起见,以下是从类和辅助方法中提取的代码要点:

public enum LawnType
{
    GRASS,
    CRATER
}

lawnData = new LawnType[][] {
        {CRATER,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
        {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,},
    };

GridPane lawnViewer = new GridPane();

for (int x = 0 ; x < data.length ; x++) {
    for (int y = 0 ; y < data[x].length ; y++) {
        ImageView imageView;

        switch(data[x][y]){
            case GRASS:
                imageView = new ImageView(new Image("mower/resources/longgrass1.png"));
                imageView.setFitWidth(gridPixelSize);
                imageView.setFitHeight(gridPixelSize);
                gridPane.add(imageView,x,y);
                break;
            case CRATER:
                imageView = new ImageView(new Image("mower/resources/crater.png"));
                imageView.setFitWidth(gridPixelSize);
                imageView.setFitHeight(gridPixelSize);
                gridPane.add(imageView, x, y);
                break;
            default:
                break;
            }
        }
    }

输出: enter image description here

最佳答案

您可以将每个图像添加到相对于底行的一行,即 data[x].length - 1

替换其中的每一个:

gridPane.add(imageView, x, y);

这样:

gridPane.add(imageView, x, data[x].length - 1 - y)

关于java - 如何从下往上添加网格中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55602778/

相关文章:

java - 在 GraphView 上设置轴边界时出现 NullPointerException

java - 比较 JPA 查询中的当前日期时间

JavaFx ImageView 不会在绑定(bind)到 VBox 的 fitWidthProperty 上调整大小

JavaFX Scene Builder - 使用变量值

java - 如何在javaFX中实现扫描仪

java - 带有 TextField 的 GridPane 9x9 用于数独

java - Jsch session 配置

java - CloudEndpoints : what is autogenerated "patch" API method and how to use it?

java - 使用 JavaFX 创建列和行约束的循环