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?