CS106A assignment2 --problem4 TricolorFlag

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

public class TricolorFlag extends GraphicsProgram {
    /* The width and height of the flag. Many flags have an aspect ratio of 3:2 and
     * our choice of constants here assumes this. Feel free to change these constants
     * if you're drawing a flag with a different aspect ratio.
     */
    private static final double FLAG_WIDTH  = 450;
    private static final double FLAG_HEIGHT = 300;
    
    //the first rect (x,y)
    private static final double x = 100;
    private static final double y = 50;
    private static final double width = FLAG_WIDTH/3;
    private static final double height = FLAG_HEIGHT;
    
    //the second rect(x1,y1)
    private static final double x1 = x+FLAG_WIDTH/3;
    private static final double y1 = y;
    private static final double width1 = FLAG_WIDTH/3;
    private static final double height1 = FLAG_HEIGHT;
    
    //the third rect(x2,y2)
    private static final double x2 = x1+FLAG_WIDTH/3;
    private static final double y2 = y;
    private static final double width2 = FLAG_WIDTH/3;
    private static final double height2 = FLAG_HEIGHT;
    /**
     * The program is divided to two methods;
     */
    public void run() {
        drawFlag();
        signName();
    }

    /**
     * the mothod is to draw flag,and it draws three rect
     */
    private void drawFlag() {
        drawRect(x,y,width,height,Color.black);
        drawRect(x1,y1,width1,height1,Color.yellow);
        drawRect(x2,y2,width2,height2,Color.red);
    }

    /**
     * draw a rect
     * @param x     The x coordinate of the upper-left corner of the box .
     * @param y     The y coordinate of the upper-left corner of the box .
     * @param width     The width of the rect.
     * @param height    The height of the rect.
     * @param color     color of the rect
     */
    private void drawRect(double x, double y, double width, double height,Color color) {
        GRect rect = new GRect(x,y,width,height);
        rect.setFilled(true);
        rect.setFillColor(color);
        add(rect);
    }

    private void signName() {
        double labelX = getWidth()-300;
        double labelY = getHeight() - 50;
        GLabel label = new GLabel("My First Flag", labelX, labelY);
        label.setFont("SansSerif-36");
        label.setColor(Color.black);
        add(label);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天晚上,陪奶奶在聊天。你陪我长大,我陪你变老。
    谁动走了我的奶酪阅读 217评论 2 1
  • Java性能权威指南(3) Java编程思想 (1) 深入理解Java虚拟机 (2) 大话设计模式 (2) 重构 ...
    加油小杜阅读 95评论 0 0
  • 前两天陪人下乡,进村时一只狗朝我奔来,摇头摆尾,嘴里还发出呜呜声,我知道它是认错人了,可就在我看它的瞬间,我呆住...
    寂静不见阅读 252评论 0 0