Created
June 5, 2012 19:24
-
-
Save hutchdoescoding/2877162 to your computer and use it in GitHub Desktop.
This is my solution to the 'Pyramid' assignment for the Stanford CS106A lectures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* File name: Pyramid.java | |
* This program will display a pyramid centered within the screen. | |
*/ | |
import acm.program.*; | |
import acm.graphics.GRect; | |
public class Pyramid extends GraphicsProgram { | |
public void run() { | |
// I realize I didn't have to use a separate method, and could have placed all the code | |
// in the run() method, but I figured this would be better in case I wanted to expand the program later on | |
DrawPyramid(); | |
} | |
// Declared the constants for the Width, Height, and amount of starting bricks. | |
public static final int BRICK_WIDTH = 20; | |
public static final int BRICK_HEIGHT = 10; | |
public static final int BRICKS_IN_BASE = 8; | |
/* Create the method for drawing the Pyramid | |
* This first part of this method Gathers the Canvas Height and Width and then stores them in variables. | |
* It then calculates the center line of the canvas, and creates variables for where the first brick should be placed. | |
* I realize that the 'StartingPointY' variable is a little redundant, but it was for my benefit when implementing | |
* further down. | |
*/ | |
private void DrawPyramid() { | |
int CanvasWidth = getWidth(); | |
int CanvasHeight = getHeight(); | |
int CanvasMiddle = CanvasWidth / 2; | |
int StartingPointX = CanvasMiddle - (BRICK_WIDTH * (BRICKS_IN_BASE / 2)); | |
int StartingPointY = CanvasHeight; | |
//Created an outer for loop for drawing as many rows as are in the BRICKS_IN_BASE | |
for (int i = 1; i <= BRICKS_IN_BASE; i++) { | |
int Y = StartingPointY - (i * BRICK_HEIGHT); | |
//The inner for loop will keep drawing bricks for as many bricks as are supposed to be in that row | |
for (int j = BRICKS_IN_BASE; j >= i; j--) { | |
int X = StartingPointX - (BRICK_WIDTH / 2) + (j * BRICK_WIDTH) - (i * (BRICK_WIDTH / 2)); | |
add (new GRect(X, Y, BRICK_WIDTH, BRICK_HEIGHT)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work! Here is my solution, it's almost the same :)
/*
*
https://see.stanford.edu/materials/icspmcs106a/13-assignment-2-simple-java.pdf
TASK 01
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
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.
*/
package assigment2;
import acm.graphics.*;
import acm.program.GraphicsProgram;
public class BricksPyramid1 extends GraphicsProgram {
}