Guest User

Untitled

a guest
Jun 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import acm.graphics.*;
  2. import acm.program.*;
  3. import java.awt.*;
  4.  
  5. public class Pyramid extends GraphicsProgram {
  6.  
  7. /** Width of each brick in pixels */
  8. private static final int BRICK_WIDTH = 30;
  9.  
  10. /** Width of each brick in pixels */
  11. private static final int BRICK_HEIGHT = 12;
  12.  
  13. /** Number of bricks in the base of the pyramid */
  14. private static final int BRICKS_IN_BASE = 14;
  15.  
  16. /* Draws a pyramid of the specified number of bricks, centered on the bottom of the screen. */
  17.  
  18. public void run() {
  19. int rowNumber=1; /* rowNumber is the counter to determine which row the program is drawing */
  20. int brickNumber=BRICKS_IN_BASE; /* brickNumber keeps track of how many bricks are to be built in the current row */
  21. int yRow=(getHeight()-BRICK_HEIGHT); /* yRow sets the y coordinate for each row, and hence each brick in the row */
  22.  
  23. while (rowNumber<=BRICKS_IN_BASE) {
  24. int bricksLeftInRow=brickNumber; /* bricksLeftInRow determines how many bricks are left to build in the row */
  25. int xStart=((getWidth()-BRICK_WIDTH*brickNumber)/2); /* xStart is original x coordinate for each row of bricks */
  26. while (bricksLeftInRow>0) {
  27. int xBrick =(xStart+BRICK_WIDTH*(brickNumber-bricksLeftInRow)); /* xBrick sets x coordinate for each brick */
  28. GRect brick = new GRect (xBrick, yRow, BRICK_WIDTH, BRICK_HEIGHT);
  29. add (brick);
  30. bricksLeftInRow--;
  31. }
  32.  
  33. rowNumber++;
  34. yRow-=BRICK_HEIGHT; /* sets height for next row */
  35. brickNumber--; /* sets number of bricks for next row */
  36. }
  37. }
  38. }
Add Comment
Please, Sign In to add comment