// Checkerboard.java // Solution to Short Assignment 8 by THC. // Draws a checkerboard with black and red squares. import java.applet.Applet; import java.awt.*; public class Checkerboard extends Applet { private final int DIMENSION = 8; // Want 8 x 8 board private final int SQUARE_SIZE = 30; // Each square is 30 x 30 pixels public void init() { // Set the size of the applet to be exactly the checkerboard size. setSize(DIMENSION * SQUARE_SIZE, DIMENSION * SQUARE_SIZE); // Draw the checkerboard. repaint(); } public void paint(Graphics page) { // Draw all the black and red squares. Color squareColor; // Color of the current square for (int row = 0; row < DIMENSION; row++) { for (int column = 0; column < DIMENSION; column++) { // A square is black if the row and column numbers are both even // or both odd. Otherwise, the square is red. if (row % 2 == column % 2) squareColor = Color.black; else squareColor = Color.red; drawSquare(page, row, column, squareColor); } } } // Draw a given square of the checkerboard. Row and column numbers start // from 0. public void drawSquare(Graphics page, int row, int column, Color color) { page.setColor(color); page.fillRect(column * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE); } }