从 Gridpane 中移除 ImageView
Remove ImageView from Gridpane
我正在 Java 编写游戏。这有点像迷宫。如果玩家点击网格面板上的一个单元格,玩家图像会移动到那里,我正在努力从旧位置移除 playerImage。
这是我创建图像视图的方式:
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?
非常感谢,
丹尼尔
您可以尝试以下方法。每当您移动播放器时,删除目的地的当前图像并将播放器图像添加到目的地。同时从之前的位置移除播放器图像,并在那里添加一个 empty 图像。这是通用语法:
gridPane.getChildren().remove(currImg);
gridPane.add(newImg, 1,0);
请注意,您需要参考您要添加和删除的图像(播放器、空箱)。
我正在 Java 编写游戏。这有点像迷宫。如果玩家点击网格面板上的一个单元格,玩家图像会移动到那里,我正在努力从旧位置移除 playerImage。
这是我创建图像视图的方式:
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?
非常感谢, 丹尼尔
您可以尝试以下方法。每当您移动播放器时,删除目的地的当前图像并将播放器图像添加到目的地。同时从之前的位置移除播放器图像,并在那里添加一个 empty 图像。这是通用语法:
gridPane.getChildren().remove(currImg);
gridPane.add(newImg, 1,0);
请注意,您需要参考您要添加和删除的图像(播放器、空箱)。