0

I am making a chess game gui by setting up a grid of jpanels with jlabels on top (the piece images). I set up the board with each square (jpanel) having an empty jlabel w/ visibility set to false. I then go through each of the jpanels and set the jlabel picture to an image corresponding to one of my pieces. I know whether or not to draw a piece because I have a variable _board[8][8] that tells me if that spot is occupied and by whom. This is how I create the original board:

EDIT:

I actually changed this up quite a bit, that way I don't have to reload images each time:

public ChessGameGUI(Square[][] board) throws IOException 
    {
        Dimension size = new Dimension(500, 400);
        Dimension boardSize = new Dimension(400, 400);

        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(size);

        theBoard = new JPanel();
        layeredPane.add(theBoard, JLayeredPane.DEFAULT_LAYER);
        layeredPane.addMouseListener(this);

        GridLayout chessBoardLayout = new GridLayout(8,8);
        theBoard.setLayout(chessBoardLayout);
        theBoard.setPreferredSize(boardSize);
        theBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        boolean drawWhite = true;

        // setup initial squares
        for(int y = 0; y < 8; y++)
        {
            for(int x = 0; x < 8; x++)
            {
                BorderLayout squareBorder = new BorderLayout();
                JPanel aChessSquare = new JPanel(squareBorder);

                if(drawWhite) aChessSquare.setBackground(Color.WHITE);
                else aChessSquare.setBackground(Color.GRAY);

                // load initial images
                if(board[x][y]._isOccupied)
                {
                    BufferedImage logo = null;

                    try{
                        // load the image
                        String path = findPieceImagePath(board, x, y);
                        logo = ImageIO.read(new File(path));
                        ImageIcon pieceImage = new ImageIcon(logo);

                        JLabel pieceJLabel = new JLabel();
                        pieceJLabel.setIcon(pieceImage); // new
                        pieceJLabel.setVisible(true); // new

                        aChessSquare.add(pieceJLabel);
                    } 
                    catch (IOException e)
                    { 
                        e.printStackTrace(); 
                    } // end try catch
                }// end if
                else
                {
                    JLabel pieceJLabel = new JLabel();
                    pieceJLabel.setVisible(false);
                    aChessSquare.add(pieceJLabel);
                }

                theBoard.add(aChessSquare);

                // alternate background colors
                if(x == 7);
                else drawWhite = !drawWhite;
            }
        }

        System.out.println("the number of components = " + theBoard.getComponentCount());

    }

This loads the initial board just fine, however I can't seem to get a specific component. When I click on one of the pieces, that square should light up. The only square that ever gets the green outline is the one in the top left (0, 0).

public void mousePressed(MouseEvent e) 
    {
        System.out.println("mouse press x = " + e.getX() + " y = " + e.getY());

        // find which piece on the board is being clicked
        int gameX, gameY;
        gameX = e.getX() / 50;
        gameY = e.getY() / 50;

        System.out.println("gameX = " + gameX + " gameY = " + gameY);

        // out of bounds
        if(gameX < 0 || gameY < 0) return;

        // true if selected a piece on this team
        if(myGame._board[gameX][gameY]._isOccupied && !_pieceSelected && myGame._board[gameX][gameY]._team == myGame._turn) 
        {
            System.out.println("selected new piece");
            JPanel curSquare = (JPanel)theBoard.getComponentAt(gameX, gameY);
            curSquare.setBorder(BorderFactory.createLineBorder(Color.green));
            _pieceSelected = !_pieceSelected;



            return;
        }

I guess I am having problems with this line: JPanel curSquare = (JPanel)theBoard.getComponentAt(gameX, gameY);

however, the gameX and gameY values are being set properly.

Thanks

thebiglebowski11
  • 1,451
  • 10
  • 41
  • 76
  • 3
    Aagh! Hurts... my... eyes... You are loading the image every time you render the board. – Martijn Courteaux Feb 07 '13 at 09:48
  • 4
    Simply add the labels to a 2D array, which would make it easier to access the label – MadProgrammer Feb 07 '13 at 10:06
  • Image loading will be slow every time. A quick optimisation would be to use a Map (string would be the "path") to act as a cache and avoid loading things you've already loaded before. There are other ways (a ChessPiece enum or class that stores its ImageIcon along with it, etc.) but that's a whole different kettle of fish. – David Lavender Feb 07 '13 at 10:15
  • See also [this](http://stackoverflow.com/questions/14621870/how-to-work-with-a-specific-element-in-gridlayout/14626413#14626413) example. – David Kroukamp Feb 07 '13 at 10:38
  • Could someone please take a look at the changes? – thebiglebowski11 Feb 07 '13 at 11:16

1 Answers1

1

I think you are missing a line to add the label to each panel. During "// setup initial squares":

curLabel.setVisible(false);

Should be:

curLabel.setVisible(false);
aChessSquare.add(curLabel);
David Lavender
  • 8,021
  • 3
  • 35
  • 55