/* 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);
}
}
CS106A assignment2 --problem4 TricolorFlag
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...