Then add code at the end of the drawBranch() method to call itself to draw three smaller branches, each starting at the other endpoint of the line that is already drawn. The coordinates of the other endpoint are newX and newY, defined in an earlier step.
The three smaller branches are at angles ang, ang + 90, and ang + 270. Their len parameters are multiplied by a factor ratio/100. When drawBranch() calls itself, its lev parameter should be decreased by 1. Thus the first of the calls should be
drawBranch(g, newX, newY, ang, len*ratio/100, lev - 1);The other two calls are identical except for the angle.
The code for drawing the line and the three branches should be enclosed in an if statement that executes the code only when lev is greater than 1. An if statement has the following structure.
if (condition) { statements }Here, condition is an expression that is either true or false and statements is a sequence of statements. These statements are only executed when the condition is true. For example, in the following code, the statements are only executed when lev is greater than 1.
if (lev > 1) { statements }
At this point, you should recompile the FractalPanel class to check for errors.
The new controls should be configured as described in the following table.
Title | Options | Initial Selection |
---|---|---|
Ratio | 45, 50, and 55 | 50 |
Levels | 1, 2, 3, 4, 5, 6, and 7 | 3 |
When you have added code for these controls, you should recompile the FractalApplet class and test the applet with appletviewer. Your applet should look like the following demonstration applet.