如何从下往上在网格中添加对象?

问题描述 投票:0回答:1

我使用网格窗格视图来使用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

java javafx gridpane
1个回答
0
投票

您可以在相对于底行data[x].length - 1的一行中添加每个图像。

替换这些:

gridPane.add(imageView, x, y);

带有此:

gridPane.add(imageView, x, data[x].length - 1 - y)
© www.soinside.com 2019 - 2024. All rights reserved.