0

I'm trying to create a chess board but it doesn't work for some reason.

final int HEIGHT = 600;
final int WIDTH = 800;
final int ROW = 8;
final int COL = 8;

String COLS = "ABCDEFGH";
JFrame frame = new JFrame();
JPanel chessBoard = new JPanel();
JButton[][] chessSquares = new JButton[ROW+1][COL+1];

public void initializeGUI() {
    
    
    frame.setSize(WIDTH, HEIGHT);
    frame.setTitle("Chess");
    chessBoard.setLayout(new GridLayout(ROW, COL));
    
    //Creating the chessboard without the pieces
    JButton temp;
    for(int row=1; row<=8; row++) {
        for(int col=1; col<=8; col++) {
            chessSquares[row][col] = new JButton("row: " + row + "col : " + col);
            if ((row+col) % 2 !=  0) {
                temp = chessSquares[row][col];
                temp.setBackground(Color.black);
                chessBoard.add(temp);
            }
            else {
            temp = chessSquares[row][col];
            temp.setBackground(Color.white);
            chessBoard.add(temp);
            }
        }
    }
    
    
    frame.add(chessBoard);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    
}

Here's an image of what it looks like :

chess board

List of things that is not working :

  • Some squares are not black.
  • The chessboard takes the entire space for the GUI. (I want the chessboard to take just less space. I tried using BorderLayout.South but it doesn't show the entire bord since it's not resizable.)

Note * The printing of row and cols is just to help me later. Not part of the board. It doesn't affect the color of the chessSquares(I tried it)

Edit: After turning setOpaque to true, I get this. enter image description here

Is there a way to make the entire button black ?

  • The code posted works for me. Run it on line [here](https://repl.it/@c0derrepo/GrandioseLeadingMap#Main.java). If it does not work for you, you'll have to provide more info. Side notes: 1. `new JButton[ROW+1][COL+1]` is too big. Use `new JButton[ROW][COL]` 2. Change the loop from ` for(int row=1; row<=8; row++)` to ` for(int row=0; row – c0der Dec 23 '20 at 05:23
  • @c0der I need to keep it at ROW+1 and COL+1 to make it easier to handle events. I'm making a chess board where the row and col starts at 1. Also, I'm using a MAC. – RandomKingRD Dec 23 '20 at 05:33

1 Answers1

2

Some squares are not black.

I think this is an issue with the Mac LAF.

Check out: How to Set the Background Color of a JButton on the Mac OS

The chessboard takes the entire space for the GUI.

This is the way the BorderLayout works. Any component added to the CENTER will take up all the unused space of the frame.

If you want the chessboard size fixed then you need to use a "wrapper" panel.

Something like:

//frame.add(chessBoard);
JPanel wrapper = new JPanel();
wrapper.add( chessBoard );
frame.add(wrapper, BorderLayout.CENTER);

The default layout of a JPanel is the FlowLayout which will respect the preferred size of any component added to it, so now the buttons will be displayed at their preferred size.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    @RandomKingRD Adding to this answer here is an example of chess game I created a while back for anything OP might find useful https://stackoverflow.com/a/14626413/1133011 as you might see I used `JLabel`s as opposed to `JButton`s as it makes more sense you don't need to click a block, you would rather click a chess piece and it will be placed onto the clicked block so a `JButton` is overkill – David Kroukamp Dec 23 '20 at 08:05