/*
* File: Pyramid.java
* ------------------
* This program is a stub for the Pyramid problem, which draws
* a brick pyramid.
*/
import acm.graphics.*;
import acm.program.*;
public class Pyramid extends GraphicsProgram {
/* define your constants here. */
private final int BRICKS_IN_BASE = 12;
private final int BRICK_WIDTH = 30;
private final int BRICK_HEIGHT = 12;
public void run() {
// You fill this in
double mid = getWidth() / 2;
double height = getHeight();
int count = BRICKS_IN_BASE;
while (count != 0) {
addBrick(mid, count, height);
count--;
height -= BRICK_HEIGHT;
}
}
public void addBrick(double mid, int count, double height) {
double initLocation = mid - count * BRICK_WIDTH / 2;
for (int i = 0; i < count; i++) {
GRect rect = new GRect(initLocation, height, BRICK_WIDTH,
BRICK_HEIGHT);
add(rect);
initLocation += BRICK_WIDTH;
}
}
}
Assignment2_Pyramid
最新推荐文章于 2022-07-26 22:12:35 发布