Write a program Checkerboard.java that takes a command-line integer n and plots an n-by-n checkerboard pattern to standard drawing. Color the squares blue and light gray, with the bottom-left square blue. To draw,
~/Desktop/io> javac Checkerboard.java
~/Desktop/io> java Checkerboard 8
~/Desktop/io> java Checkerboard 4
~/Desktop/io> java Checkerboard 5
public class Checkerboard {
public static void main(String[] args) {
final int n = Integer.parseInt(args[0]);
StdDraw.setScale(0, n);
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row % 2 == 0) {
if (col % 2 == 0) {
StdDraw.setPenColor(Color.BLUE);
} else {
StdDraw.setPenColor(Color.LIGHT_GRAY);
}
} else {
if (col % 2 == 0) {
StdDraw.setPenColor(Color.LIGHT_GRAY);
} else {
StdDraw.setPenColor(Color.BLUE);
}
}
StdDraw.filledSquare(col + 0.5, row + 0.5, 0.5);
}
}
}
}
Link To: Java Source Code