Java-FX 如何在单击时在 GridPane 中设置图像 - 黑白棋游戏
Java-FX How to set image in a GridPane on click - game Othello
您好,我正在尝试单击我的 GridPane 中的一个位置(从 0 到 7)。
我会在里面设置一个图像。我什么都试过了,但我看不到任何改进...
这是我的看板
这是我点击网格的代码
@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);
}
black 是我的作品是黑色的,white 是白色的。
来源路径
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);
您好,我正在尝试单击我的 GridPane 中的一个位置(从 0 到 7)。 我会在里面设置一个图像。我什么都试过了,但我看不到任何改进...
这是我的看板
这是我点击网格的代码
@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);
}
black 是我的作品是黑色的,white 是白色的。
来源路径
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);