gpt4 book ai didi

java - 从 Gridpane 中删除 ImageView

转载 作者:行者123 更新时间:2023-12-02 12:37:25 30 4
gpt4 key购买 nike

我正在用 Java 编写游戏。这有点像迷宫。如果玩家单击网格 Pane 上的单元格,则玩家图像会移动到那里,而我正在努力从旧位置删除playerImage。

这是我创建 imageView 的方法:

private ImageView[][] initImages() {
int colcount = gridPane.getColumnConstraints().size();
int rowcount = gridPane.getRowConstraints().size();
imageViews = new ImageView[colcount][rowcount];
// bind each Imageview to a cell of the gridpane
int cellWidth = (int) gridPane.getWidth() / colcount;
int cellHeight = (int) gridPane.getHeight() / rowcount;
for (int x = 0; x < colcount; x++) {
for (int y = 0; y < rowcount; y++) {
//creates an empty imageview
imageViews[x][y] = new ImageView();
//image has to fit a cell and mustn't preserve ratio
imageViews[x][y].setFitWidth(cellWidth);
imageViews[x][y].setFitHeight(cellHeight);
imageViews[x][y].setPreserveRatio(false);
imageViews[x][y].setSmooth(true);
//add the imageview to the cell and
//assign the correct indicees for this imageview, so you later can use GridPane.getColumnIndex(Node)
gridPane.add(imageViews[x][y], x, y);
//the image shall resize when the cell resizes
imageViews[x][y].fitWidthProperty().bind(gridPane.widthProperty().divide(colcount));
imageViews[x][y].fitHeightProperty().bind(gridPane.heightProperty().divide(rowcount));
}
}

这是我在 OnClickEvent 上绘制玩家的方法

public RealGUI drawPlayer(int playerNumber, CellCoordinates coords) {
Image img = null;
switch (playerNumber) {
case 1:
img = new Image("/gui/img/Bob_trans.png");
break;
case 2:
// trans Bild Spieler 2
img = new Image("/gui/img/Bob_trans.png");
break;
default:
System.out.print("Problem mit Spielernummer");
}

ImageView newImgView = new ImageView();
newImgView.fitHeightProperty().bind(this.gridPane.heightProperty()
.divide(this.gridPane.getRowConstraints().size()));
newImgView.setImage(img);
this.gridPane.add(newImgView, coords.getX(), coords.getY());
return this;
}

我的基本问题是:如何删除 GridPane 上的 Player Imageview?

非常感谢,丹尼尔

最佳答案

您可以尝试以下方法。每当您移动玩家时,删除目的地的当前图像并将玩家图像添加到目的地。还要从之前的位置删除玩家图像,并在其中添加一个图像。这是通用语法:

gridPane.getChildren().remove(currImg); 
gridPane.add(newImg, 1,0);

请注意,您需要引用要添加和删除的图像(玩家、空物体)。

关于java - 从 Gridpane 中删除 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45082036/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com