1.
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, as shown in the following sample run:
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 ableto change those values in your program.
程序源代码:
public void run() {
/* You fill this in. */
/*初始坐标*/
int initX = (getWidth() - BRICK_WIDTH * BRICKS_IN_BASE)/2;
int initY = getHeight() - BRICK_HEIGHT;
/*由最底层向上绘制所需图像*/
for(int i=0;i<BRICKS_IN_BASE;i++){
/*y坐标在绘制每层图像时变化值为BRICK_HEIGHT*/
int yPos = initY - i * BRICK_HEIGHT;
/*初始坐标每次的缩进量为BRICK_WIDTH的一半*/
initX += BRICK_WIDTH/2;
for(int j=0;j<BRICKS_IN_BASE-i;j++){
int xPos = initX + j * BRICK_WIDTH;
GRect br = new GRect(xPos,yPos,BRICK_WIDTH,BRICK_HEIGHT);
add(br);
}
}
}
}