Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import acm.graphics.*;
- import acm.program.*;
- import java.awt.*;
- public class Pyramid extends GraphicsProgram {
- /** Width of each brick in pixels */
- private static final int BRICK_WIDTH = 30;
- /** Width of each brick in pixels */
- private static final int BRICK_HEIGHT = 12;
- /** Number of bricks in the base of the pyramid */
- private static final int BRICKS_IN_BASE = 14;
- /* Draws a pyramid of the specified number of bricks, centered on the bottom of the screen. */
- public void run() {
- int rowNumber=1; /* rowNumber is the counter to determine which row the program is drawing */
- int brickNumber=BRICKS_IN_BASE; /* brickNumber keeps track of how many bricks are to be built in the current row */
- int yRow=(getHeight()-BRICK_HEIGHT); /* yRow sets the y coordinate for each row, and hence each brick in the row */
- while (rowNumber<=BRICKS_IN_BASE) {
- int bricksLeftInRow=brickNumber; /* bricksLeftInRow determines how many bricks are left to build in the row */
- int xStart=((getWidth()-BRICK_WIDTH*brickNumber)/2); /* xStart is original x coordinate for each row of bricks */
- while (bricksLeftInRow>0) {
- int xBrick =(xStart+BRICK_WIDTH*(brickNumber-bricksLeftInRow)); /* xBrick sets x coordinate for each brick */
- GRect brick = new GRect (xBrick, yRow, BRICK_WIDTH, BRICK_HEIGHT);
- add (brick);
- bricksLeftInRow--;
- }
- rowNumber++;
- yRow-=BRICK_HEIGHT; /* sets height for next row */
- brickNumber--; /* sets number of bricks for next row */
- }
- }
- }
Add Comment
Please, Sign In to add comment