Step 5


Introduction

In this step you will modify your program to draw a complete fractal and add two more controls to it. One control, a "Levels" control, modifies the level of deatil drawn in the fractal. It changes the value of the levels variable that you already have in the FractalPanel class. The other control, a "Ratio" control, controls the size ratio for the basic pattern that is repeated in the fractal. It changes the value of a new ratio instance variable that you will add.

Drawing a Complete Fractal

The first thing to do is make some changes in the FractalPanel class. You will need to add an integer variable ratio for controlling the size ratio in the fractal. Its initial value should be 3.

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
    }

Adding Setter Methods for Communication

At this point, the controller has no way to change the settings of the levels and ratio instance variables, so you will need to add two methods setLevels() and setRatio() that the controller can use. You should pattern these two methods after the setAngle() and setLength() methods that you already have.

At this point, you should recompile the FractalPanel class to check for errors.

Adding Controls to the Control Panel

Now you need to modify the FractalApplet class to add controls for the number of levels and the fractal ratio. To do this, you should add new local JComboBox variables ratioControl and levelControl into the init() method. These variables should be set up and added into the control panel similar to the existing angleControl and lengthControl variables. The new controls should also have listeners similar to the listeners in the existing controls.

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.

Demonstration Applet