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.

1. Since you will be inheriting from our Basic Picture class you must download and save the file BasicPicture.java onto the Desktop, 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.
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 } ```
• Use the drawLine() function to draw this line.
• Use `currentColor` for the Color parameter to drawLine().
• You must compute the coordinates of the ends of the line. To help you with this, refer to this figure, which shows the size of the three lines of the H relative to currentSize. Also, the coordinate system in Java is different from the normal Cartesian coordinate system in two ways: the origin (0,0) is in the upper left corner, and the y values increase as you go down.

Once you have completed this, now is a good time to stop, compile, and run your new program to verify that your 1st line in the tree turns out. Make an HTML file to view your applet and include:

``` <applet code="HTree.class" height=600 width=600> </applet> ```

Throughout this lab you will need to hold down the "shift" key when you hit the "Reload" button in Netscape. We're not sure why, but you do have to do this if you want Netscape to reload your most recent work.

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 use the drawLine() function to draw the other 2 lines that make up an H.
• Use `currentColor` for the Color parameter to drawLine().
• In order to draw an H of size currentSize, centered around the point (currentX,currentY), you must compute the starting and ending (x,y) coordinates for each of the three lines that comprise the H, as with the center line. For help determining these coordinates, refer to this figure. Also remember that the Java coordinate system is different from the cartesian one.

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(); ```
• For color you should create a Color variable newColor and set it to the nextColor after currentColor in a color sequence that we provide you with. To do this add the line:
``` Color newColor = nextColor(currentColor); ```
before your `new` statements, and use newColor as the Color parameter in the `new` statement.
• Angle should always be 0 (If you wish, you can explore changing the angle in the exercise on the next page of this lab. It can produce some very interesting pictures.)
• Info should always be `currentInfo`.
• X and Y are the coordinates of the center of the next smaller H
• Size is the size of the next smaller H. We suggest that you choose this Size to be 1/2 the size of the H we just drew.

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