Although this approach isn't really necessary for something as simple as a chess board, when I think of an elegant way to render something related to the view, I want to make it as easy as possible to change the rendered view as possible. For example, suppose you decided you wanted to alternate black and white on each row, but not each column. The one-liners used in answers so far would have to be re-written.
If I was to go as far as I could with this and make it as easy to redesign the pattern on the Chess board as possible, here is what I would do:
1) I would make a file that indicates what color each square in the chess board is.
For example, I could make a file chess_board_pattern.config
that looks something like this:
bwbwbwbw
wbwbwbwb
bwbwbwbw
wbwbwbwb
bwbwbwbw
wbwbwbwb
bwbwbwbw
wbwbwbwb
2) I would write a class/component/whatever that can read this file and create some kind of object that represents the board pattern:
public class BoardPattern {
private Color[][] pattern;
public BoardPattern(File patternFile)
{
pattern = new Color[8][8];
//Parse the file and fill in the values of pattern
}
public Color[][] getPattern {
return pattern;
}
}
3) I would then use that class in the function that actually draws the board.
File patternFile = new File("chess_board_pattern.ini");
Color[][] pattern = new BoardPattern(patternFile).getPattern();
ChessBoardDrawable chessBoard = new ChessBoardDrawable();
for(int row = 0; row < 8; row++) {
for(int column; column < 8; column++) {
chessBoard.drawSquare(row, column, Color[row][column]);
}
}
Again, this is a lot harder than is necessary for a Chess board. I think in general, though, when working on more complicated projects, it's best to come up with generalized solutions like this instead of writing code that's difficult to change later.