Java-FX如何在点击游戏中设置GridPane中的图像Othello
问题描述:
嗨,我试图在GridPane中点击一个位置(从0到7)。 我会在里面设置一个图像。我tryed一切,但我看不到任何改善...Java-FX如何在点击游戏中设置GridPane中的图像Othello
这里我就点击代码网格
@FXML
private void clickGrid(MouseEvent event) {
myGrid = new GridPane();
black = new Image("othello/images/black.png");
white = new Image("othello/images/white.png");
empty = new Image("othello/images/empty.png");
Node source = (Node)event.getSource() ;
Integer colIndex = GridPane.getColumnIndex(source);
Integer rowIndex = GridPane.getRowIndex(source);
System.out.printf("Mouse clicked cell [%d, %d]%n", colIndex.intValue(), rowIndex.intValue());
myGrid.add(new ImageView(white), colIndex, rowIndex);
}
这里我的代码,当我点击重新启动
@FXML
private void restartGame(ActionEvent event)throws Exception{
myGrid = new GridPane();
black = new Image("othello/images/black.png");
white = new Image("othello/images/white.png");
empty = new Image("othello/images/empty.png");
for (int i = 0; i < 8; i++){ //Per righe
for (int j = 0; j < 8; j++){ // Per colonne
myGrid.add(new ImageView(empty), i, j);
}
}
myGrid.add(new ImageView(black), 3, 3);
myGrid.add(new ImageView(black), 4, 3);
myGrid.add(new ImageView(white), 4, 4);
myGrid.add(new ImageView(white), 4, 3);
}
黑色是我的一块黑色,白色是白色的。
源路径
I have main project in src of netbeans.
Inside it, i have:
- othello (it contains my main)
- othello.images (it cointains all my image also backgrounds)
- othello.view (it contains my FXML files)
- othello.model (now nothing)
- othello.controller (it contains the controllers about the fxml files)
答
不要创建的每次点击一个新的GridPane:
myGrid = new GridPane(); // delete this
删除这条线,以及图像添加到您在FXML
准备GridPane
答
我认为你没有看到新的图像,因为你添加到一个新的网格,而不是现有的:
myGrid = new GridPane(); // !!! here a problem
myGrid.add(new ImageView(white), colIndex, rowIndex);