Assignment2_GraphicsHierarchy

/*
 * 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);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值