/*
* File: .java
* ----------------------------
* This program is a stub for the GraphicsHierarchy problem, which
* draws a partial diagram of the acm.graphics hierarchy.
*/
import acm.program.*;
import acm.graphics.*;
public class GraphicsHierarchy extends GraphicsProgram {
/* define your constants here. */
public void run() {
// You fill this in
// GLabel label1 = new GLabel("Program");
// double lbWidth = label1.getWidth();
// double lbHeight = (gre1.getHeight() - label1.getAscent())/2;
// double lbX = gre1.getX() + (gre1.getWidth() - lbWidth)/2;
// double lbY = gre1.getY() + lbHeight + label1.getHeight()/2;
// label1.move(lbX,lbY);
// add(label1);
double width = getWidth() / 4;
double height = getHeight() / 6;
double x1 = 0.3, x2 = 1.5, x3 = 2.7;
double y = 3;
drawRect(width, height, x1, x2, x3, y);
drawLine(width, height, x1, x2, x3);
drawLabel(width, height, x2, x2, x3, y);
}
public void drawRect(double width, double height, double x1, double x2,
double x3, double y) {
GRect rect1 = new GRect(setRectLocation(width, x2), height, width,
height);
GRect rect2 = new GRect(setRectLocation(width, x1), setRectLocation(
height, y), width, height);
GRect rect3 = new GRect(setRectLocation(width, x2), setRectLocation(
height, y), width, height);
GRect rect4 = new GRect(setRectLocation(width, x3), setRectLocation(
height, y), width, height);
add(rect1);
add(rect2);
add(rect3);
add(rect4);
}
public double setRectLocation(double widthOrHeight, double multiple) {
return (widthOrHeight * multiple);
}
public void drawLine(double width, double height, double x1, double x2,
double x3) {
GLine line1 = new GLine(width * 1.5 + width / 2, height * 2,
setLineLocation(width, x1), height * 3);
GLine line2 = new GLine(width * 1.5 + width / 2, height * 2,
setLineLocation(width, x2), height * 3);
GLine line3 = new GLine(width * 1.5 + width / 2, height * 2,
setLineLocation(width, x3), height * 3);
add(line1);
add(line2);
add(line3);
}
public double setLineLocation(double width, double x) {
return (width * x + width / 2);
}
public void drawLabel(double width, double height, double x1, double x2,
double x3, double y) {
GLabel label1 = new GLabel("Program");
add(label1, setLabelLocationX(label1, width, x2),
setLabelLocationY(label1, height, 1));
GLabel label2 = new GLabel("GraphicsProgram");
add(label2, setLabelLocationX(label2, width, x1 / 5),
setLabelLocationY(label2, height, y));
GLabel label3 = new GLabel("ConsoleProgram");
add(label3, setLabelLocationX(label3, width, x2),
setLabelLocationY(label3, height, y));
GLabel label4 = new GLabel("DialogProgram");
add(label4, setLabelLocationX(label4, width, x3),
setLabelLocationY(label4, height, y));
}
public double setLabelLocationX(GLabel label, double width, double x) {
return ((setRectLocation(width, x) - label.getWidth() / 2 + width / 2));
}
public double setLabelLocationY(GLabel label, double height, double y) {
return (setRectLocation(height, y) + label.getAscent() / 2 + height / 2);
}
}
Assignment2_GraphicsHierarchy
最新推荐文章于 2021-02-06 15:50:21 发布