In this exercise you will be making a recursive drawing known as an "H-tree", pictured at right.
Follow these steps:
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.
public void draw() {
// All your code goes here
}
currentColor
for the Color parameter to
drawLine().
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>
currentColor
for the Color parameter to drawLine().
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.
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();
Color newColor = nextColor(currentColor);
before your new
statements, and use newColor as the
Color parameter in the new
statement.
currentInfo
.
PREVIOUS | 1 | 2 | 3 | 4 | 5 | NEXT |