Solution to ProgramHeirarchy – Assignment 2

/*
 * File: ProgramHierarchy.java
 * Name: 
 * Section Leader: 
 * ---------------------------
 * This file is the starter file for the ProgramHierarchy problem.
 */

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class ProgramHierarchy extends GraphicsProgram {	
	// width of the box
	private static final int BOX_WIDTH = 175;
	
	//height of the box
	private static final int BOX_HEIGHT = 45;
	
	public void run() {
               
		//middle point of the window
		double middle = getWidth()/2;
		
		//height window middle
		double height_center = getHeight()/2;
		
		//middle of the box itself
		double box_mid = BOX_WIDTH/2;
		
		// half the box height
		double box_half = BOX_HEIGHT/2;
		
		// base of top square = height_center -40
		double base_top= height_center -40 ;
		
		//roof of bottom boxes
		double roof_bottom = height_center + 40 ;
		
		/*
		 * The boxes here
		 */
		
		// top rect
		GRect top = new GRect(middle-box_mid,base_top -BOX_HEIGHT,BOX_WIDTH,BOX_HEIGHT);
		add(top);
		
		//left rect 
		GRect left = new GRect(middle/2 -box_mid,roof_bottom,BOX_WIDTH,BOX_HEIGHT);
		add(left);
		
		// mid rect
		GRect mid = new GRect(middle-box_mid,roof_bottom,BOX_WIDTH,BOX_HEIGHT);
		add(mid);

		//right rect
		GRect right = new GRect(middle + middle/2 - box_mid,roof_bottom,BOX_WIDTH,BOX_HEIGHT);
		add(right);
		
		/*
		 * the lines connecting here
		 */
		
		//leftLine
		GLine leftLine = new GLine(middle/2,roof_bottom,middle,base_top);
		add(leftLine);
		
		//midline
		GLine midLine = new GLine(middle,base_top,middle,roof_bottom);
		add(midLine);
		
		//rightLine
		GLine rightLine = new GLine(middle,base_top,middle+middle/2,roof_bottom);
		add(rightLine);
		
		/*
		 * the labels
		 */
		
		// TopLabel
		GLabel topLabel = new GLabel("Program");
		topLabel.move(middle-topLabel.getWidth()/2, base_top+topLabel.getAscent()/2 -BOX_HEIGHT/2);
		add(topLabel);
		
		//LeftLabel
		GLabel leftLabel = new GLabel("GraphicsProgram");
		leftLabel.move(middle/2-leftLabel.getWidth()/2, roof_bottom+leftLabel.getAscent()/2+BOX_HEIGHT/2);
		add(leftLabel);

		//Middle Label
		GLabel midLabel = new GLabel("ConsoleProgram");
		midLabel.move(middle-midLabel.getWidth()/2, roof_bottom+midLabel.getAscent()/2 +BOX_HEIGHT/2);
		add(midLabel);

		//rightLabel
		GLabel rightLabel = new GLabel("DialogProgram");
		rightLabel.move(middle+middle/2-rightLabel.getWidth()/2, roof_bottom+rightLabel.getAscent()/2 +BOX_HEIGHT/2);
		add(rightLabel);
			
	}
}


Leave a Reply