I want to display an 8x8 checkerboard, but with the bellow code I only get horizontal lines with alternating colors.
Color color = Color.gray;
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
if (color == Color.gray)
{
color = Color.lightGray;
}
else
{
color = Color.gray;
}
g.setColor(color);
g.fillRect(row*80, col*80, 80, 80);
}
}
You create your board columns-wise, each column going vertically.
You toggle the color for each field, not wrong.
At the end of the column, you end up with a different color than you started the column with, which is correct.
Then you toggle the color and start the next column, which means you start the next column with the same color you started the preceding one with. I.e. you always use the same color in the same line.
To solve either toggle once more after each column, or do not toggle for the first field of each column.