Problem 3

The problem 3 is Graphics Hierarchy : which means to write a GraphicsProgram subclass that draws a partial diagram of the acm.graphics class hierarchy (if you look below at the output to see what it does look like), the rules as such are

  • The width and height of the class boxes should be specified as named constants so that they are easy to change.
  • The labels should be centered in their boxes. You can find the width of a label by calling label.getWidth() and the height it extends above the baseline by calling label.getAscent(). If you want to center a label, you need to shift its origin by half of these distances in each direction.
  • The connecting lines should start and end at the center of the appropriate edge of the box.
  • The entire figure should be centered both vertically and horizontally.

The way that I looked at the problem was to break it into 2 parts, one is displaying the box on the screen with the text centred within that box. And the second part was to display the boxes within the correct part on the screen.

The first part of the problem, only need to pass in the X,Y coordinates and the text and then find the size of the text and move that text to the middle of the box, the second part is place the boxes within the screen accordingly to the size of the screen and the boxes dimensions, thus bottom four they are centred in the middle (screen width / 2 – ((box width * 4) / 2)), then join to the middle of the boxes to the middle of the top box.

Here is the output of the Graphics Hierarchy problem,

Graphics Hierarchy
Graphics Hierarchy

Here is the java source file, but the zip file above has the whole assignment within it.

/*
 * File: GraphicsHierarchy.java
 * ----------------------------
 * This program is a stub for the GraphicsHierarchy problem, which
 * draws a partial diagram of the acm.graphics hierarchy.
 */
 
import acm.graphics.*;
import acm.program.*;
 
public class GraphicsHierarchy extends GraphicsProgram {
 
	private void createBox(int X, int Y, String name)
	{
		GRect gObjectBox = new GRect(X, Y, boxWidth, boxHeight);
		GLabel text = new GLabel(name);
 
		// replace the X,Y with the new cords to place the text into the middle of the box
		X += (boxWidth / 2) - (text.getWidth()/2);
		Y += (boxHeight / 2) + (text.getAscent()/2);
		text.setLocation(X, Y);
 
		add(gObjectBox);
		add(text);
	}
 
	public void run() {
		// You fill this in
		// find the centre, to then build around it
		int centreX = getWidth() / 2;
		int centreY = (getHeight() / 2)-boxHeight;
 
		String names[] = new String[5];
		names[0] = "GObject";
		names[1] = "GLabel";
		names[2] = "GLine";
		names[3] = "GOval";
		names[4] = "GRect";
 
		createBox(centreX - (boxWidth / 2), centreY - (rowDistances / 2),names[0]);
 
		int placeX = centreX - ((belowBoxs * boxWidth)/2) - ((belowBoxs * boxDistances)/2);
		for (int i =0; i < belowBoxs; i++)
		{
			createBox(placeX, centreY + (rowDistances / 2), names[i+1]);
			GLine joiningLine = new GLine(centreX, centreY - ((rowDistances / 2)-boxHeight),
										placeX + (boxWidth/2), centreY + (rowDistances/2));
			add(joiningLine);
			placeX += (boxWidth + boxDistances);
		}
	}
 
	private static final int belowBoxs = 4;
	private static final int boxWidth = 100;
	private static final int boxHeight = 50;
	private static final int boxDistances = 20;
	private static final int rowDistances = 150;
}

One thought on “Problem 3”

  1.  
    /*
     * File: GraphicsHierarchy.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 {
    	private static final int BOX_HEIGHT = 20;
    	private static final int BOX_WIDTH = 100;
     
    	public void run() {
    		drawRowOne();
    		drawRowTwo();
    	}
     
    	private void drawRowOne() {
    		int centerX = getWidth()/2;
    		int centerY = getHeight()/3;
     
    		drawBox(centerX, centerY, "GObject");
    	}
     
    	private void drawRowTwo() {
    		String boxType = "GLabel";
    		for (int i = 1; i &lt; 5; i++) {
    			int centerY = getHeight() * 2/3;
    			int centerX = getWidth()/5 * i;
     
    			switch (i) {
    			case 1: break;
    			case 2: boxType = &quot;GLine&quot;; break;
    			case 3: boxType = &quot;GOval&quot;; break;
    			case 4: boxType = &quot;GRect&quot;; break;
    			}
     
    			drawBox(centerX, centerY, boxType);
    			drawLineToRowOne(centerX, centerY);
    		}
    	}
     
    	private void drawBox(int centerX, int centerY, String boxType) {
    		int boxX = centerX - BOX_WIDTH/2;
    		int boxY = centerY - BOX_HEIGHT/2;
    		GRect box = new GRect(boxX, boxY, BOX_WIDTH, BOX_HEIGHT);
    		add(box);
     
    		GLabel label = new GLabel(boxType);
    		int labelX = centerX - (int) label.getWidth()/2;
    		int labelY = centerY + (int) label.getAscent()/2;
    		add(label, labelX, labelY);	
     
    	}
     
    	private void drawLineToRowOne(int centerX, int centerY) {
    		int x0 = centerX;
    		int y0 = centerY - BOX_HEIGHT/2;
    		int x1 = getWidth()/2;
    		int y1 = getHeight()/3 + BOX_HEIGHT/2;
    		GLine line = new GLine(x0, y0, x1, y1);
    		add(line);
    	}
     
    }

Leave a Reply

Your email address will not be published.