java语言的科学与艺术-编程练习4.11

 1 /*
 2  * 画出一个用矩形砖块的金字塔,每行递减1
 3  */
 4 
 5 import acm.program.*;
 6 import acm.graphics.*;
 7 
 8 public class drawPyramid extends GraphicsProgram {
 9     
10     public void run(){
11         double x = (getWidth() - BRICK_IN_BASE * BRICK_WIDTH) / 2;
12         double y = (getHeight() + BRICK_IN_BASE * BRICK_HEIGHT) / 2;
13         for (int i = 2 ; i <= BRICK_IN_BASE; i++){
14             for(int j = i; j <= BRICK_IN_BASE; j++){
15                 add (new GRect(x - i * BRICK_WIDTH /2 + j * BRICK_WIDTH, y - i * BRICK_HEIGHT,BRICK_WIDTH, BRICK_HEIGHT));
16             }
17         }
18     }
19     
20     /* private constants */
21     private static final double BRICK_WIDTH = 20;
22     private static final int BRICK_HEIGHT = 10;
23     private static final int BRICK_IN_BASE = 20;
24 }

posted on 2012-12-06 20:09  mybluecode  阅读(191)  评论(0编辑  收藏  举报