Connect 4 - from console to GUI
I have a fully working version of connect four for the console. Now I would like to get it into a nice gui.
I use a class BoardPanel extends JPanel, in that class I create a 2D array of JLabels and fill the panel with them.
Code:
grid= new JLabel[rows][columns];
for (int x = 0; x < rows; x++){
for (int y = 0; y < columns; y++){
grid[x][y] = new JLabel(empty); //empty is an ImageIcon
grid[x][y].setBorder(new LineBorder(Color.BLACK));
super.add(grid[x][y]);
}
}
Now I'm looking for a way to "sync" my console board (a 2D array) with my array of JLabels.
Console board:
Code:
public void setBoard () {
for (int indexRow = 0; indexRow <rows; indexRow ++) {
for (int indexColumn = 0; indexColumn < columns; indexColumn ++) {
board [indexRow] [indexColumn] = empty; //here 'empty' is a ' '-character
}
}
public String show () {
String s = "";
for (int indexRow= 0; indexRow<rows; indexRow++) {/ / print the numbers for the first row
s + = String.format ("% d", rows - indexRow);
for (int indexColumn = 0; indexColumn <columns; indexColumn++) {
s + = String.format ("% c", board [indexRow] [indexColumn]);
}
s + = String.format ("\ n ");
}
s + = "";
for (int index = 1; index <= columns; index + +)
s + = "" + index + "";
return s;
}