Lab 10
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:
- 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.
To review how to compile Java applets, see
Page 5 of Lab 8: "Editing, Compiling and Running Java Applets".
- Download the file HTree.java and save
it in the same place you saved BasicPicture.class. All your
changes should be made to HTree.java.
- 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.
- As a first 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). 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 the 3 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. For help determining these coordinates,
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 H in the tree turns
out. Make an HTML file to view your applet and include:
<applet code = "HTree.class" height = 600 width = 600>
</applet>
- 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.
Thus, below the
drawLines
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.
- 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 be sure 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.
- 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).
emkawas@princeton.edu