0

So I'm trying to develop a chess game in javafx. I successed in showing the board but it doesnt show the pieces. The weird thing is when I tested on the main to find the problem, it shows me the piece.I can't post images.

Here is the code of the main classes needed : chessboard, square, piece and the main

public class chessBoard extends GridPane{
    
    private final int nbSquare = 8;
    
    private square[][] squares;
    
    public chessBoard() {
    
        this.squares = new square[this.nbSquare][this.nbSquare];

        //initiate color of squares
        for (int row=0; row<this.nbSquare;row++) {
            for (int col=0; col<this.nbSquare;col++) {
                squares[row][col]= new square(null, null, new position(row, col), this.nbSquare - row, intToChar(col));
                squares[row][col].setPrefHeight(100);
                squares[row][col].setPrefWidth(100);
                squares[row][col].setBorder(new Border(new BorderStroke(Color.BLACK,
                            BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
            
                if ((row+col) % 2 == 0) {
                    squares[row][col].setColor(color.WHITE);
                    squares[row][col].setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
                }
                else {
                    squares[row][col].setColor(color.BLACK);
                    squares[row][col].setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
                }
            }
        }
        
        //put black pieces
        squares[0][0].getChildren().add(new castle(color.BLACK, 5.1, new position (0,0), "file:./sdrc/tourNoireS.jpg"));
        squares[0][1].getChildren().add(new knight (color.BLACK, 3.2, new position (0, 1), "file:./src/images/cavalierNoirS.jpg"));
        squares[0][2].getChildren().add(new bishop(color.BLACK, 3.3, new position (0,2), "file:./src/images/fouNoirS.jpg"));
        squares[0][3].getChildren().add(new queen(color.BLACK, 8.8, new position (0,3), "file:./srcimages/reineNoireS.jpg"));
        squares[0][4].getChildren().add(new king(color.BLACK, 0, new position (0, 4), "file:./src/images/roiNoirS.jpg"));
        squares[0][5].getChildren().add(new bishop(color.BLACK, 3.3, new position (0,5),"file:./src/images/fouNoirS.jpg"));
        squares[0][6].getChildren().add(new knight(color.BLACK, 3.2,  new position (0,6),"file:./src/images/cavalierNoirS.jpg"));
        squares[0][7].getChildren().add(new castle(color.BLACK, 5.1, new position (0,7), "file:./src/images/tourNoireS.jpg"));
        
        for (int i=0; i<this.nbSquare;i++)  
        {
            squares[1][i].getChildren().add(new pawn(color.BLACK, 1, new position(1, i), "file:./src/images/pionBlancS.jpg"));
        }
        
        //put the white pieces
        System.out.println("put white");
        squares[7][0].getChildren().add(new castle(color.WHITE, 5.1, new position (7,0), "file:./src/images/tourBlancS.jpg"));
        squares[7][1].getChildren().add(new knight (color.WHITE, 3.2, new position (7, 1), "file:./src/images/cavalierBlancS.jpg"));
        squares[7][2].getChildren().add(new bishop(color.WHITE, 3.3, new position (7,2), "file:./src/images/fouBlancS.jpg"));
        squares[7][3].getChildren().add(new queen(color.WHITE, 8.8, new position (7,3), "file:./src/images/reineBlancS.jpg"));
        squares[7][4].getChildren().add(new king(color.WHITE, 0, new position (7, 4), "file:./src/images/roiBlancS.jpg"));
        squares[7][5].getChildren().add(new bishop(color.WHITE, 3.3, new position (7,5),"file:./src/images/fouBlancS.jpg"));
        squares[7][6].getChildren().add(new knight(color.WHITE, 3.2,  new position (7,6), "file:./src/images/cavalierBlancS.jpg"));
        squares[7][7].getChildren().add(new castle(color.WHITE, 5.1, new position (7,7), "file:./src/images/tourBlancS.jpg"));
        
        
        for (int i=0; i<this.nbSquare;i++) 
        {
            squares[1][i].getChildren().add(new pawn(color.WHITE, 1, new position(6, i), "file:./src/images/pionBlancS.jpg"));
        }
        
        
        //add squares to gridpane chessboard
        for (int row=0; row<8;row++) {
            for (int col=0; col<8;col++) {
        
                add(squares[row][col], row, col);
            }
        }
        
        System.out.println("finish");
    }   
    
        
    public char intToChar(int number)
    {
        char letter = ' ';
        
        switch(number)
        {
            case 0 :
                letter = 'a';
                break;
            case 1 :
                letter = 'b';
                break;
            case 2 :
                letter = 'c';
                break;
            case 3 :
                letter = 'd';
                break;
            case 4 :
                letter = 'e';
                break;
            case 5 : 
                letter = 'f';
                break;
            case 6 : 
                letter = 'g';
                break;
            case 7 : 
                letter = 'h';
                break;
        }
        
        return letter;
    }

    public square[][] getSquares() {
        return squares;
    }


    public void setSquares(square[][] squares) {
        this.squares = squares;
    }


    public int getNbSquare() {
        return nbSquare;
    }
}
public class square extends StackPane{
    color color;
    position position;
    piece piece;
    int number;
    char letter ;
    
    public square(color color, piece piece, position position, int number, char letter) {
        this.color = color;
        this.piece = piece;
        this.position = position;
        this.number = number;
        this.letter = letter;
    }

    public color getColor() {
        return color;
    }
    
    public position getPosition() {
        return position;
    }

    public piece getPiece() {
        return piece;
    }

    public void setPiece(piece piece) {
        this.getChildren().addAll(piece);
    }

    public void setColor(color color) {
        this.color = color;
    }
}
ublic abstract class piece extends ImageView implements Ipiece {
    color side;
    double weight;
    position position;
     
    public piece(color side, double weight, position position, String pathImage) {
        this.side = side;
        this.weight = weight;
        this.position = position;
        
        if (pathImage != null)
        {
            try
            {
                System.out.println(pathImage);
                this.setImage(new Image(pathImage));
            }
            catch(Exception e)
            {
                System.out.println("file doesnt exist" + pathImage);
            }
        }
    }

    public color getSide() {
        return side;
    }

    public void setSide(color side) {
        this.side = side;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public position getPosition() {
        return position;
    }

    public void setPosition(position position) {
        this.position = position;
    }   
}
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("java2s.com");
        
        chessBoard chess = new chessBoard();
        
        
        //put the black pieces
        //piece Castle = new castle(color.WHITE, 5.1, new position (7,0), "file:./src/images/cavalierBlancS.png");
        
        //GridPane grid = new GridPane();
        
        
        
        /*square sq = new square(color.BLACK, null, new position(3,4), 3, 'h');
        
        sq.setPrefHeight(100);
        sq.setPrefWidth(100);
        sq.setBorder(new Border(new BorderStroke(Color.BLACK,
                    BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
        sq.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
        sq.getChildren().add(Castle);
        
        
        for (int row=0; row<8;row++) {
            for (int col=0; col<8;col++) {
        
                grid.add(chess.getSquares()[row][col], row, col);
            }
        }
    */
        BorderPane pane= new BorderPane();
        
        
        pane.setCenter(chess);
        System.out.println("scene");
        Scene scene = new Scene(pane, 500, 500);
        
        System.out.println("stage");
            
            primaryStage.setScene(scene);
            primaryStage.show();

        
         /*Create a GridPane
        GridPane pane = new GridPane();

        // Create 64 rectangles and add to pane
        int count = 0;
        double s = 100; // side of rectangle
        for (int i = 0; i < 8; i++) {
          count++;
          for (int j = 0; j < 8; j++) {
            Rectangle r = new Rectangle(s, s, s, s);
            if (count % 2 == 0)
              r.setFill(Color.WHITE);
            pane.add(r, j, i);
            count++;
          }
        }

        // Create a scene and place it in the stage
        Scene scene = new Scene(pane);
        primaryStage.setTitle("java2s.com");
        primaryStage.setScene(scene); // Place in scene in the stage
        primaryStage.show();
        ;*/
    }
    public static void main(String[] args) {
        launch(args);
    }
}
  • 1
    Also off-topic: your `intToChar()` method can be a one-liner: `return (char)(‘a’+number);`. – James_D Aug 26 '22 at 10:43
  • 1
    Probably your image paths are wrong. Read https://stackoverflow.com/q/61531317/2189127 – James_D Aug 26 '22 at 10:44
  • Actually, looking at the commented code, the image paths are clearly wrong. The `src` folder is for *source code* (and resources), so obviously it is not available at runtime (unless you are running from within your development environment, and only then for specific configurations). – James_D Aug 26 '22 at 14:46
  • 1
    No need to extend `GridPane`, see examples [here](https://stackoverflow.com/a/69340852/230513) and [here](https://stackoverflow.com/a/2563350/230513). – trashgod Aug 26 '22 at 14:49
  • hello everyone, I finally find a solution for that . Thank you all for your help – Lyna DJELMOUDI Aug 26 '22 at 19:11

0 Answers0