Pyramid - int initX = (getWidth() - (i + 1)*BRICK_WIDTH) /...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * 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.*; i public class Pyramid extends GraphicsProgram { p 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 */
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int initX = (getWidth() - (i + 1)*BRICK_WIDTH) / 2; /* draw bricks in layer i */ for (int j = 0; j &lt;= 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; }...
View Full Document

Ask a homework question - tutors are online