2017-07-19 5 views
0

Salut j'essaie de cliquer sur une position (de 0 à 7) dans mon GridPane. Je voudrais définir une image à l'intérieur. Je tryed tout, mais je ne vois aucune amélioration ...Java-FX Comment définir l'image dans un GridPane sur clic - jeu Othello

Ceci est mon conseil enter image description here

Voici mon code sur un clic sur la grille

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

    } 

Voici mon code lorsque je clique redémarrer

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

noir est ma pièce colorée de noir, car blanc est blanc.

Chemin source

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) 

Répondre

1

Ne pas créer une nouvelle GridPane sur chaque clic:

myGrid = new GridPane(); // delete this 

supprimer cette ligne, et ajouter une image à la GridPane que vous avez préparé à FXML

2

Je pense que vous ne voyez pas de nouvelles images parce que vous ajoutez à une nouvelle grille, pas à l'existant:

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