Lab 9
Page 3


Writing the H-Tree Applet

In this exercise you will be making a recursive drawing known as an "H-tree", pictured at right.

Follow these steps:

  1. Since you will be inheriting from our Basic Picture class you must download and save the file BasicPicture.java into the directory where you will be working on your extension to this class. Then, you must load it into TextPad and compile it. You will need to Configure your Tools menu for compiling, just as you did in Lab 8.
  2. Download the file HTree.java and save it in the same place you saved BasicPicture.java. All your changes should be made to HTree.java.
  3. In the BasicPicture class we provided a drawLine() procedure. Since your class inherited from BasicPicture, drawLine() is now a part of your class too. This is the power of inheritance. When you use drawLine() it will look something like this:
    drawLine(Color, StartX, StartY, EndX, EndY, Thickness);
    This draws a line (go figure!) from the starting (x,y) coordinate to the ending (x,y) coordinate, in the specified color and thickness.

  4. As your first step, you should try to draw one of the lines necessary for the H - say, the center line. All your code should go inside the draw method, which looks like:
    public void draw() {

    // All your code goes here

    }
  5. As the next step you should try to draw the first H in the H tree (i.e. the big black H in the center of the picture). Once again, compile and see if this works, before going onto the next section.

  6. When your first H looks right it is time to start thinking about recursion to finish the picture. What you want to do is make it so when we draw the first H object it creates and draws 4 smaller H objects. These 4 smaller objects then each draw 4 even smaller objects until the picture is complete. However, first things first: All recursion must have a base case or it will never end. In the H tree we do not keep trying to draw infinitely smaller H's but eventually quit when the size of the current H is small enough. To accomplish this, you need to enclose the code that creates and draws the smaller H's in an if statement that checks to see if the size is not too small. This if statement goes just below the drawLines. If you mess up (or omit) your base case, Netscape will likely crash! If this happens, do not, repeat not, start the debugger (some dialog box may give you this option). What you want to do is quit and restart Netscape and (of course) repair your base case! If you don't know what to fix, ask a lab assistant for help. Write the if statement now but go to step 7 to see what goes in the action part of the if statement to be executed if the size is big enough.

  7. As the action of the if statement, you want to create and draw 4 smaller H objects recursively. (The recursion will take care of these smaller H objects drawing even smaller H objects.) Thus, inside the { ...} that set off the action part of the if statement, you should create and draw 4 smaller H objects in the correct place, with the correct color and size. The code for creating and drawing looks something like this:
    HTree UpperRightH = new HTree(Color, X, Y, Size, Angle, Info);
    UpperRightH.draw();

  8. When you have finished your modifications to HTree.java, add a link to your HTree.java in the HTML file you are using to view the applet (linking the .java file as a text file as we have done for the last two labs).

PREVIOUS 1 | 2 | 3 | 4 | 5 NEXT