Created
March 16, 2015 12:42
-
-
Save kenechiokolo/dc7ec86f197ca3b01751 to your computer and use it in GitHub Desktop.
CS106A: Assignment 2.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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.*; | |
/* This program draws the acm hierarchy. The philosphy behind it | |
* is that the boxes are first created at the origin, then moved | |
* to their correct positions. The same applies to the labels and | |
* the lines. | |
*/ | |
public class ProgramHierarchy extends GraphicsProgram { | |
// assigns boxes a constant width of 150 pixels | |
private static final double BOX_WIDTH = 150; | |
// assigns boxes a constant height of 50 pixels | |
private static final double BOX_HEIGHT = 50; | |
public void run() { | |
createFigure(); | |
} | |
// creates entire figure | |
private void createFigure() { | |
createBoxes(); | |
createLabels(); | |
drawLines(); | |
} | |
// creates boxes and positions them | |
private void createBoxes() { | |
createCentreBox(); | |
createFlankBoxes(); | |
createTopBox(); | |
} | |
// creates labels and positions them | |
private void createLabels() { | |
createConsoleLabel(); | |
createGraphicsLabel(); | |
createDialogLabel(); | |
createProgramLabel(); | |
} | |
// draws lines and positions them | |
private void drawLines() { | |
drawVerticalLine(); | |
drawDiagonalLines(); | |
} | |
// creates centre box and positions it | |
private void createCentreBox() { | |
positionCentre(); | |
} | |
// creates flank boxes and positions them | |
private void createFlankBoxes() { | |
positionRightFlank(); | |
positionLeftFlank(); | |
} | |
// creates top box and positions it | |
private void createTopBox() { | |
positionTop(); | |
} | |
// creates console label and positions it | |
private void createConsoleLabel() { | |
GLabel consoleLabel = new GLabel ("ConsoleProgram", 0, 0); | |
add (consoleLabel); | |
consoleLabel.move((getWidth() / 2) - (consoleLabel.getWidth() / 2), getHeight() / 2 + (consoleLabel.getHeight()/2)); | |
} | |
// creates graphics label and positions it | |
private void createGraphicsLabel() { | |
GLabel graphicsLabel = new GLabel ("GraphicsProgram", 0, 0); | |
add (graphicsLabel); | |
graphicsLabel.move((getWidth() / 4) - (graphicsLabel.getWidth() / 2), getHeight() / 2 + (graphicsLabel.getHeight()/2)); | |
} | |
// creates dialog label and positions it | |
private void createDialogLabel() { | |
GLabel dialogLabel = new GLabel ("DiaologProgram", 0, 0); | |
add (dialogLabel); | |
dialogLabel.move((getWidth() / 4*3) - (dialogLabel.getWidth() / 2), getHeight() / 2 + (dialogLabel.getHeight()/2)); | |
} | |
// creates program label and positions it | |
private void createProgramLabel() { | |
GLabel programLabel = new GLabel ("Program", 0, 0); | |
add (programLabel); | |
programLabel.move((getWidth() / 2) - (programLabel.getWidth() / 2), getHeight() / 4 + (programLabel.getHeight()/2)); | |
} | |
// draws vertical line and positions it | |
private void drawVerticalLine() { | |
GLine verticalLine = new GLine (getWidth()/2, (getHeight() / 2) - (BOX_HEIGHT/2), getWidth() / 2, (getHeight()/4) + (BOX_HEIGHT/2)); | |
add (verticalLine); | |
} | |
// draws diagonal lines and positions them | |
private void drawDiagonalLines() { | |
GLine diagonalRightLine = new GLine (getWidth()/4*3, (getHeight() / 2) - (BOX_HEIGHT/2), getWidth() / 2, (getHeight()/4) + (BOX_HEIGHT/2)); | |
add (diagonalRightLine); | |
GLine diagonalLeftLine = new GLine (getWidth()/4, (getHeight() / 2) - (BOX_HEIGHT/2), getWidth() / 2, (getHeight()/4) + (BOX_HEIGHT/2)); | |
add (diagonalLeftLine); | |
} | |
// creates box at origin coordinates | |
private GRect createBox() { | |
GRect box = new GRect (0, 0, BOX_WIDTH, BOX_HEIGHT); | |
add (box); | |
return box; | |
} | |
// creates a box called centreBox | |
GRect centreBox = createBox(); | |
// moves centreBox box to the centre of the program | |
private void positionCentre() { | |
findXCentre(); | |
findYCentre(); | |
double x = findXCentre(); | |
double y = findYCentre(); | |
centreBox.move(x, y); | |
} | |
// returns the centre coordinates for x minus half the box width | |
private double findXCentre() { | |
double x = getWidth() / 2 - (BOX_WIDTH/2); | |
return x; | |
} | |
// returns the centre coordinates for y minus half the box height | |
private double findYCentre() { | |
double y = getHeight() / 2 - (BOX_HEIGHT/2); | |
return y; | |
} | |
// creates a box called rightBox | |
GRect rightBox = createBox(); | |
// moves rightBox from origin to right of centreBox | |
private void positionRightFlank() { | |
findXCentre(); | |
findYCentre(); | |
double rx = findXCentre() + getWidth() / 4; | |
rightBox.move(rx, findYCentre()); | |
} | |
// creates a box called leftBox | |
GRect leftBox = createBox(); | |
// moves leftBox from origin to left of centreBox | |
private void positionLeftFlank() { | |
findXCentre(); | |
findYCentre(); | |
double rx = findXCentre() - getWidth() / 4; | |
leftBox.move(rx, findYCentre()); | |
} | |
// creates a box called topBox | |
GRect topBox = createBox(); | |
// moves topBox from origin to top of centreBox | |
private void positionTop() { | |
findXCentre(); | |
findYCentre(); | |
double ry = findYCentre() - getHeight() / 4; | |
topBox.move(findXCentre(), ry); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment