Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import acm.graphics.GRect;
  2. import acm.program.GraphicsProgram;
  3.  
  4. import java.awt.*;
  5.  
  6. public class MethodicalPyramid extends GraphicsProgram {
  7.  
  8.     private static final int BRICK_H = 50;
  9.     private static final int BRICK_L = 100;
  10.  
  11.     /**
  12.      * Returns the color to be used for bricks in the given layer.
  13.      *
  14.      * @param layerIndex     index of the layer whose color to return. {@code 0} is the
  15.      *                       bottom layer, {@code numberOfLayers - 1} is the top layer.
  16.      * @param numberOfLayers the number of layers in the pyramid.
  17.      * @return the color to be used for the given layer, or {@code null} if
  18.      * {@code layerIndex} is invalid.
  19.      */
  20.     private Color layerColor(int layerIndex, int numberOfLayers) {
  21.         if (layerIndex >= numberOfLayers || layerIndex < 0) {
  22.             return null;
  23.         } else if (layerIndex != 0) {
  24.             int green;
  25.             int blue = green = (220 / numberOfLayers * layerIndex);
  26.             Color color = new Color(255, green, blue);
  27.             return color;
  28.         } else {
  29.             return new Color(255, 0, 0);
  30.         }
  31.     }
  32.  
  33.     /**
  34.      * Draws the given layer with bricks filled with the given color. If
  35.      * {@code layerIndex} is invalid, no bricks at all should be drawn.
  36.      *
  37.      * @param layerIndex     index of the layer to draw. {@code 0} is the bottom layer,
  38.      *                       {@code numberOfLayers - 1} is the top layer.
  39.      * @param numberOfLayers the number of layers in the pyramid.
  40.      * @param layerColor     color the layer's bricks should be filled with.
  41.      */
  42.     public void drawLayer(int layerIndex, int numberOfLayers, Color layerColor) {
  43.         if (layerIndex >= numberOfLayers || layerIndex < 0) {
  44.             //null;
  45.         } else {
  46.             int brickAmount = numberOfLayers - layerIndex;
  47.             int x;
  48.             int y = (BRICK_H * numberOfLayers) - (BRICK_H * layerIndex);
  49.             for (int i = 0; brickAmount > i; i++) {
  50.                 x = 50 + (i * 100) + (layerIndex * 50);
  51.                 GRect brick = new GRect(x, y, BRICK_L, BRICK_H);
  52.                 brick.setFilled(true);
  53.                 brick.setColor(layerColor);
  54.                 add(brick);
  55.             }
  56.         }
  57.     }
  58.  
  59.  
  60.     public void run() {
  61.         int n = readInt("Enter a number");
  62.         for (int i =0; i <= n; i++){
  63.             drawLayer(i,n,layerColor(i,n));
  64.         }
  65.     }
  66.  
  67.     public static void main(String[] args) {
  68.         new MethodicalPyramid().start();
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement