我使用网格 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;
}
}
}
最佳答案
您可以将每个图像添加到相对于底行的一行,即 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/