I solved the pyramid block question from assignment 2. The original question was:
I sort of solved the problem but in a very chaotic way and I didn't figure out every specification. I'm looking for someone to possible look at my code and help me clear my thinking for how to solve the problem properly.Write a GraphicsProgram subclass that draws a pyramid consisting of bricks arranged in horizontal rows, so that the number of bricks in each row decreases by one as you move up the pyramid, as shown in the following sample run:
The pyramid should be centered at the bottom of the window and should use constants for the following parameters:
BRICK_WIDTH - The width of each brick (30 pixels)
BRICK_HEIGHT - The height of each brick (12 pixels)
BRICKS_IN_BASE - The number of bricks in the base (14)
The numbers in parentheses show the values for this diagram, but you must be able to change those values in your program.
My code is DEFINITELY scary... Sorry just trying to figure things out.
/* * File: PyramidBoxes.java * ------------------------ * This program should make a pyramid of boxes with the following requirements * BRICK_WIDTH The width of each brick (30 pixels) * BRICK_HEIGHT The height of each brick (12 pixels) * BRICKS_IN_BASE The number of bricks in the base (14) */ import acm.graphics.*; import acm.program.*; public class PyramidBoxes extends GraphicsProgram { //Width of each brick private static final int BRICK_WIDTH = 30; //Height of each brick private static final int BRICK_HEIGHT = 12; //Number of bricks in the base private static final int BRICKS_IN_BASE = 14; public void run() { //Set an integer for the height of the window to be manipulated. int windowHeight = getHeight() - 13; // GRect rect = new GRect(BRICK_WIDTH, windowHeight, BRICK_WIDTH,BRICK_HEIGHT); // add (rect); for (int j = 0; j < 14; j++){ buildRow((8*BRICK_WIDTH - j*(BRICK_WIDTH / 2)),(j * BRICK_HEIGHT), j); } } /* * This method will build a row but will need to know the starting value for x,y */ private void buildRow(int startingX_Value, int startingY_Value, int bricks_in_base){ for (int i = bricks_in_base; i > 0; i--){ //This line creates a new instance of GRect with the cascading values int changingX_Value = (startingX_Value + i * BRICK_WIDTH); GRect rect = new GRect((changingX_Value),startingY_Value, BRICK_WIDTH, BRICK_HEIGHT ); add(rect); } } }