0

I am trying to make a chess game, to show chess pieces on board, I made a main JPanel which has a gridlayout containing every tiles which are all JPanels too. I then draw pieces on the board using this function:

    public void drawPiece(Graphics g) {
        try {
            g.drawImage(ImageIO.read(Queen.class.getResource(path)), 18, 10, 75, 90, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        g.dispose();
    }

This works fine, the problem I am having comes when I try to set a border on the JPanel in which there is already a piece. I have a color code to put borders on tiles to show illegal moves, kill moves and normal moves when a piece is selected. When I place a border where there is a chess piece, then the image dissapears and even if I draw it again right after adding the border, it doesnt show.

For a solution I tried placing a second JPanel on top of my tile panel, which I called graphicsPanel, this panel is where I now draw my chess piece, and then I change the border of the tile containing the panel where the image is and this didn't fix anything, the problem stays the same. I am out of ideas and don't know how to fix it, so if anyone can help me, that would be great, thank you!

public void mouseClicked(MouseEvent e) {
    Tile tile = (Tile) e.getSource();
    ArrayList<Point> possibleMoves = new ArrayList<Point>();
    if(tile.piece != null) {
        switch(currentTurn) {
            case white:{ 
                for(Piece current: this.white.pieceList) {
                    if(current.x + current.y * 8 == tile.number) {
                        if(current.isSelected == false) {
                            current.isSelected = true;
                            possibleMoves = current.possibleMoves();
                            for(Point currentPoint: possibleMoves) {
                                if(board.tileList.get(currentPoint.x + currentPoint.y * 8).piece == null) {
                                    board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(legalMoveBorder);
                                }else if(board.tileList.get(currentPoint.x + currentPoint.y * 8).piece.color == 'b') {
                                    board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(killBorder);
                                }else {
                                    board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(illegalMoveBorder);
                                }
                            }
                        }else {
                            current.isSelected = false;
                            board.removeAllBorders();
                        }
                        break;
                    }
                }
            }   
            case black:{
                for(Piece current: this.black.pieceList) {
                    if(current.x + current.y * 8 == tile.number) {
                        if(current.isSelected == false) {
                            current.isSelected = true;
                            possibleMoves = current.possibleMoves();
                            for(Point currentPoint: possibleMoves) {
                                if(board.tileList.get(currentPoint.x + currentPoint.y * 8).piece == null) {
                                    board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(legalMoveBorder);
                                }else if(board.tileList.get(currentPoint.x + currentPoint.y * 8).piece.color == 'w') {
                                    board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(killBorder);
                                }else {
                                    board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(illegalMoveBorder);
                                }
                            }
                        }else {
                            current.isSelected = false;
                            board.removeAllBorders();
                        }
                        break;
                    }
                }
            }   
        }   
    }

}

This the code where I add borders and removes borders when piece gets either selected or stop being selected. If anyone can help me, thanks a lot!

  • 1
    Not related to your problem but a painting method should NOT read an image. You should not be doing I/O every time the board is repainted. The image should be read in the constructor of your class. You have a problem with your Border logic. Using the `setBorder(...)` method of a panel should not affect the painting of the image on the panel. I suggest, you should just use a JLabel containing an Icon of the chess piece you want to paint. Then your panel will simply have a default EmptyBorder with a pixel size of 1 and a JLabel. You can then freely change the Border as you wish. – camickr May 18 '19 at 14:51
  • Simple example showing the above painting concept: https://stackoverflow.com/questions/6811247/drawing-in-jlayeredpane-over-exising-jpanels/6811800#6811800 – camickr May 18 '19 at 14:53
  • When you want others to work with your code, it is in your best interest to make the code easy to read. Instead of calling the rather long `board.tileList.get(currentPoint.x + currentPoint.y * 8)` so many times and creating visual clutter, put `JComponent tile = board.tileList.get(currentPoint.x + currentPoint.y * 8);` at the start of each loop. Then put `tile.piece` and `tile.setBorder` in the subsequent statements. – VGR May 18 '19 at 15:04
  • Thank you @camickr I will try that! – Benjamin Theriault May 18 '19 at 15:09
  • @VGR sorry for the mess with the code, first time posting here, I will be more careful next time. – Benjamin Theriault May 18 '19 at 15:09
  • *"I made a main JPanel which has a gridlayout containing every tiles which are all JPanels too."* t would be far better to use an undecorated `JButton` instead of the `JPanel` here. 1) The button supports displaying an icon without any changes. 2) We can add an `ActionListener` to the button that will react to either mouse clicks of keyboard entry. See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). General tip: For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 19 '19 at 01:19

0 Answers0