/* * File: Checkerboard2.java * ------------------------ * * This is the ACM Java Task Force Checkerboard.java acm.graphics * example, extended to include a few more graphics operations. * Here's the original description: * * ----- * This program draws a checkerboard. The dimensions of the * checkerboard is specified by the constants NROWS and * NCOLUMNS, and the size of the squares is chosen so * that the checkerboard fills the available vertical space. * ----- * * Modifications by L. McCann 2007/02/11 */ import acm.graphics.*; import acm.program.*; import acm.util.*; // JTFTools import java.awt.*; // Colors public class Checkerboard2 extends GraphicsProgram { /** Runs the program */ public void run() { final int NUM_COLORS = 4; double sqSize = (double) getHeight() / NROWS; Color[] color = new Color[NUM_COLORS]; RandomGenerator rand = new RandomGenerator(); color[0] = Color.BLACK; color[1] = Color.RED; color[2] = Color.BLUE; color[3] = Color.GREEN; for (int i = 0; i < NROWS; i++) { for (int j = 0; j < NCOLUMNS; j++) { double x = j * sqSize; double y = i * sqSize; GRect sq = new GRect(x, y, sqSize, sqSize); if ((i + j) % 2 != 0) { sq.setColor(color[rand.nextInt(NUM_COLORS)]); sq.setFilled(true); add(sq); // places the GRect obj on the canvas } JTFTools.pause(100); // pause for a tenth of a second } } } // run /* Private constants */ private static final int NROWS = 8; /* Number of rows */ private static final int NCOLUMNS = 8; /* Number of columns */ }