Java 从Gridpane中删除ImageView
我正在用Java编写一个游戏。有点像迷宫。如果玩家点击网格窗格上的一个单元格,玩家图像就会移动到那里,我正在努力将玩家图像从原来的位置移除 以下是我创建ImageView的方式:Java 从Gridpane中删除ImageView,java,user-interface,gridpane,Java,User Interface,Gridpane,我正在用Java编写一个游戏。有点像迷宫。如果玩家点击网格窗格上的一个单元格,玩家图像就会移动到那里,我正在努力将玩家图像从原来的位置移除 以下是我创建ImageView的方式: private ImageView[][] initImages() { int colcount = gridPane.getColumnConstraints().size(); int rowcount = gridPane.getRowConstraints().size(); imag
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));
}
}
我的基本问题是:如何删除GridPane上的Player Imageview
多谢各位,
丹尼尔你可以试试看。无论何时移动播放机,请删除目标位置的当前图像,并将播放机图像添加到目标位置。同时从上一个位置移除玩家图像,并在那里添加一个空图像。以下是通用语法:
gridPane.getChildren().remove(currImg);
gridPane.add(newImg, 1,0);
请注意,您需要对正在添加和删除的图像(播放器、清空)进行引用
gridPane.getChildren().remove(currImg);
gridPane.add(newImg, 1,0);