/* * File: ColorPyramid.java * ----------------------- * This program draws a brick pyramid. * The size of the brick, color of the brick, * and number of bricks on the base are defined as constants. * * Author: CS1MD3, Oct. 2008 */ import acm.graphics.*; import acm.program.*; import java.awt.*; public class ColorPyramid extends GraphicsProgram { public void run() { /* y-coordinate of the top layer */ int initY = (getHeight() - BRICKS_IN_BASE*BRICK_HEIGHT) / 2; /* draw layers, top down */ for (int i = 0; i < BRICKS_IN_BASE; i++) { /* y-coordinate of this layer */ int y = initY + i*BRICK_HEIGHT; /* starting x-coordinate of this layer */ int initX = (getWidth() - (i + 1)*BRICK_WIDTH) / 2; /* draw bricks in this layer */ for (int j = 0; j <= i; j++) { int x = initX + j*BRICK_WIDTH; /* draw a brick */ createBrick(x, y); } /* for j */ } /* for i */ } /* createBrick */ /* Draws a black outline (1 pixel) brick of size */ /* BRICK_WIDTH x BRICK_HEIGHT with BRICK_COLOR */ /* at position (x,y). */ private void createBrick(int x, int y) { /* draw a brick filled interior with BRICK_COLOR */ GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT); brick.setFillColor(BRICK_COLOR); /* fill the interior */ brick.setFilled(true); add(brick); } /* private constants */ private static final int BRICK_WIDTH = 32; private static final int BRICK_HEIGHT = 15; private static final int BRICKS_IN_BASE = 12; private static final Color BRICK_COLOR = Color.RED; }