Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Stanford CS106a GraphicsHeirarchy

    Hi all

    I've just started the Stanford CS106 online class as an intro to programming. In assignment 2 there's a problem where you have to write a program to display a class hierarchy as per this brief;

    Stanford CS106a GraphicsHeirarchy-problem.jpg

    This is where I'm at in my solution;

    public class GraphicsHierarchy extends GraphicsProgram {

    private static final int BOX_WIDTH = 125;
    private static final int BOX_HEIGHT = 50;


    public void run() {
    drawChart();
    }
    private void drawChart() {
    drawTopBox();
    drawBoxRow();
    drawLines();
    drawGObject();
    }
    private void drawTopBox() {
    double x = getWidth() / 2 - BOX_WIDTH / 2;
    double y = getHeight() / 2 - BOX_HEIGHT * 2;
    GRect topBox = new GRect(x, y, BOX_WIDTH, BOX_HEIGHT);
    add(topBox);
    }
    private void drawBoxRow() {
    double x = getWidth() / 5 - (BOX_WIDTH / 2);
    double y = getHeight() / 2 + BOX_HEIGHT;
    for (int i = 4; i > 0; i--) {
    GRect box = new GRect(x, y, BOX_WIDTH, BOX_HEIGHT);
    add(box);
    x += getWidth() / 5;
    }
    }
    private void drawLines() {
    double startlinex = getWidth() / 2;
    double startliney = getHeight() / 2 - BOX_HEIGHT;
    double endlinex = getWidth() / 5;
    double endliney = getHeight() / 2 + BOX_HEIGHT;
    for (int i = 4; i > 0; i--) {
    GLine line = new GLine (startlinex, startliney, endlinex, endliney);
    add(line);
    endlinex += getWidth() / 5;
    }
    }
    private void drawGObject() {
    double x = getWidth() / 2 - BOX_WIDTH / 2;
    double y = getHeight() / 2 - BOX_HEIGHT * 2;
    GLabel text1 = new GLabel("GObject",x ,y );
    text1.setFont("Helvetica-24");
    add(text1);

    I assumed I'd just stick the text into the boxes using label.getWidth() etc as per the brief. However, if I try and use it in the brackets after GLabel, it doesn't work because the GLabel isn't initiated till a couple of lines later.

    So far the idea of top-down decomposition has been a big theme of the course, so I'm loath to go back to the drawing board and create the boxes around the labels. Or to press on with the lectures and book to use more sophisticated commands to solve the problem. Is there a way of moving the GLabel after its been initiated, or somehow manipulating it with label.getWidth?

    Many thanks for your time and expertise!!!

    Robbie
    Attached Thumbnails Attached Thumbnails Stanford CS106a GraphicsHeirarchy-problem.jpg  

Similar Threads

  1. CheckerBoardKarel - Stanford CS106A Assignment 1
    By mattdotmac in forum New To Java
    Replies: 6
    Last Post: 07-15-2012, 02:58 PM
  2. Stanford cs106a
    By D.good in forum Introductions
    Replies: 1
    Last Post: 02-04-2012, 07:18 PM
  3. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 05:43 PM
  4. Stanford CS106A, my work- your thoughts?
    By chmza in forum New To Java
    Replies: 2
    Last Post: 12-02-2010, 06:53 PM
  5. BigInteger Rational (CS106A-Stanford university)
    By ccie007 in forum New To Java
    Replies: 16
    Last Post: 10-01-2010, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •