CS106A assignment3 --problem4 Pyramid

/* TODO: Replace these file comments with a description of what your program
 * does.
 */
import acm.graphics.*;
import acm.program.*;

public class Pyramid extends GraphicsProgram {
    /** Width of each brick in pixels */
    private static final double BRICK_WIDTH = 30;

    /** Height of each brick in pixels */
    private static final double BRICK_HEIGHT = 12;

    /** Number of bricks in the base of the pyramid */
    private static final int BRICKS_IN_BASE = 20;
    
    /** Coordinates of the bricks in the leftmost every row*/
    private static double originalX,originalY;
    
    
    public void run() {
        for(int i=BRICKS_IN_BASE;i>0;i--){
            createBrickRow(i);
        }
    }

    // create a row of bricks
    private void createBrickRow(int i) {
    
        //First get the original coordinates
        getOriginalCoordinate(i);
        
        //Then create a row of bricks;
        for(int j=0;j<i;j++){
            double x,y;
            x = originalX + j * BRICK_WIDTH;
            y = originalY;
            GRect brick = new GRect(x,y,BRICK_WIDTH,BRICK_HEIGHT);
            add(brick);
        }
    }

    private void getOriginalCoordinate(int i) {
        // get the original coordinates(x,y);
        originalX = (getWidth() - i*BRICK_WIDTH)/2;
        originalY = getHeight() - (BRICKS_IN_BASE -i)*BRICK_HEIGHT;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 安象思维学习笔记 1 反应模式 毕诚老师: 毕诚老师:刚刚笑的时候什么感受 绛:好玩啊 师:再感受一下 绛:好像这...
    梁绛阅读 230评论 0 1
  • 如何成为一个高手? 我想,热爱学习的你一定想知道答案。那就让我们一起来看看,罗振宇是怎么说的吧。 下面,是我在收看...
    白仙逸阅读 1,686评论 0 1
  • 迷失在自我想象裏的人是可愛的 雖然現實會讓他疼 但夢裏的世界 很美
    憨憨爹阅读 90评论 0 0