1

I am currently working on a Connect Four project in Java. I'm kind of stuck on how to go about creating the board, which will be a 6x7 grid of JLabel images.

I just created a 6x7 panel using GridLayout as the layout manager and then added 42 "blank spot" images to it (to simulate a new, empty Connect Four board).

Here's my code:

public class Board extends JFrame
{
    public JPanel boardPanel = new JPanel(new GridLayout(6, 7));

    public Board (String inTitle)
    {
        super(inTitle); //this just creates the window with the given title, not relevant
        URL /*blah*/;
        for (int i = 0; i < 42; i++)
        {
            boardPanel.add(new JLabel(new ImageIcon(/*blah*/)));
        }
        add(boardPanel);
     }
}

The thing is, this works to just initialize the board, but I'm not quite sure how to then go about changing specific spots in the board to a red or yellow disc (by changing the image) once they're occupied. Can you access specific cells in a GridLayout by index? Should I be using some sort of array instead?

Mike Ross
  • 63
  • 6
  • 4
    *"Can you access specific cells in a GridLayout by index?"* See [this](http://stackoverflow.com/questions/7702697/how-to-get-x-and-y-index-of-element-inside-gridlayout) for example. – user1803551 Dec 14 '15 at 02:36
  • That helped a lot, don't know why I didn't see that upon searching. Thanks! – Mike Ross Dec 14 '15 at 03:02

0 Answers0