Brick Pyramid

01
/*
02
 * File: Pyramid.java
03
 * ------------------
04
 * This program is a stub for the Pyramid problem, which draws
05
 * a brick pyramid.
06
 */
07
 
08
import acm.graphics.*;
09
import acm.program.*;
10
 
11
public class Pyramid extends GraphicsProgram {
12
 
13
//create constants for the brick width, brick height, and the number of 
14
//bricks in the base of the pyramid
15
 
16
	public static final int BRICK_WIDTH = 30;
17
	public static final int BRICK_HEIGHT = 12;
18
	public static final int BRICKS_IN_BASE = 12;
19
 
20
	public void run() {
21
 
22
		for(int i=0;i<BRICKS_IN_BASE; i++) {
23
 
24
			drawRowOfBricks(
25
 
26
				(getWidth()-(BRICK_WIDTH*(BRICKS_IN_BASE-(i))))/2,
27
				(i*(-BRICK_HEIGHT))-BRICK_HEIGHT,
28
				BRICKS_IN_BASE-i
29
 
30
			);
31
 
32
		}
33
	}
34
 
35
	private void drawBrick(int x, int y, int brick_width, int brick_height) { 
36
 
37
		GRect myBrick = new GRect(x, y, brick_width, brick_height);
38
		add (myBrick); 
39
 
40
	}
41
 
42
	public void drawRowOfBricks(int x, int y, int numOfBricks) {
43
 
44
		for(int i=0;i<numOfBricks; i++)
45
 
46
			drawBrick(x+(i*BRICK_WIDTH), y+getHeight(), BRICK_WIDTH, BRICK_HEIGHT);
47
 
48
	}
49
 
50
}
51

pyramid

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Facebook
  • TwitThis
  • StumbleUpon
  • LinkedIn
  • Digg
  • del.icio.us
  • Squidoo
  • Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>