2017-07-19 25 views
0

嗨,我試圖在GridPane中點擊一個位置(從0到7)。 我會在裏面設置一個圖像。我tryed一切,但我看不到任何改善...Java-FX如何在點擊遊戲中設置GridPane中的圖像Othello

這是我的板 enter image description here

這裏我就點擊代碼網格

@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) 

回答

1

不要創建的每次點擊一個新的GridPane:

myGrid = new GridPane(); // delete this 

刪除這條線,以及圖像添加到您在FXML

準備GridPane
2

我認爲你沒有看到新的圖像,因爲你添加到一個新的網格,而不是現有的:

myGrid = new GridPane(); // !!! here a problem 
myGrid.add(new ImageView(white), colIndex, rowIndex); 
相關問題