ColorPyramid - int x = initX + j*BRICK_WIDTH; /* draw a...

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

View Full Document Right Arrow Icon
/* * 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.*; i public class ColorPyramid 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 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++) {
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 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; }...
View Full Document

This note was uploaded on 10/27/2009 for the course COMP SCI 1MD3 taught by Professor Various during the Winter '07 term at McMaster University.

Ask a homework question - tutors are online