/* * File: Pyramid.java * ------------------ * This program draws a brick pyramid. * The size of the brick and number of bricks on the base * are defined as constants. */ import acm.graphics.*; import acm.program.*; public class Pyramid 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 layer i */ int y = initY + i*BRICK_HEIGHT; /* starting x-coordinate of layer i */ int initX = (getWidth() - (i + 1)*BRICK_WIDTH) / 2; /* draw bricks in layer i */ for (int j = 0; j <= i; j++) { /* x-coordinate of brick j in layer i */ int x = initX + j*BRICK_WIDTH; /* draw brick j in layer i */ GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT); add(brick); } /* for j */ } /* for i */ } /* private constants */ private static final int BRICK_WIDTH = 32; private static final int BRICK_HEIGHT = 15; private static final int BRICKS_IN_BASE = 12; }