CS 101 (Fall 2000)
Lab 5: Iteration -- Mandelbrot Set

Authors: Sergey Klibanov and Ron K. Cytron
Lab Assigned Design Due
(In class)

1 PM
(In Lab)
(In Lab)
Lab Due
(In class)
1 PM
3 Oct 6 Oct 11-12 Oct 18-19 Oct 23 Oct


Fractals are mathematical models that can create seemingly complex drawings from very simple specifications. Because they are not easily bored, computers are well-suited to iterative tasks. You will use iteration in this lab to compute a fractal drawing. This lab emphasizes how computation can be applied with relative ease and speed to create what would take a human much longer to accomplish.


By the time you are finished with this lab, you should have:

Before Starting:

  1. Make sure you understand how to use the JavaDoc tool.
  2. Please read over the entire document before you start.
  3. See the wallpaper a student made from this lab.
  4. Take a look at the JavaDoc for this lab
  5. Study the setPixel method of PictureComponent
  6. Study recursion and iteration examples given in class.
  7. Run the sample solution. Be sure to examine the text output as well as the pretty picture. Doing this will help illustrate coordinate conversions.
  8. If you need help, please ask.
[[[ Download PC zip ]]]

Zip contains:

Background Information:

Fractals are an amazing mechanism for describing complex systems in terms of simple and recursive components. The idea of fractals in the complex plane (where the x coordinate is real and the y coordinate is imaginary) dates back to 1918, when Gaston Julia began research that characterized Julia Set fractals. In 1979, Benoit Mandelbrot defined a map over Julia sets that reflected the expense of computing a specific point in the set. Such maps generate interesting patterns, and we will in a sense reproduce Mandelbrot's work in this lab. Since Mandelbrot's original work, computers have become faster and Java has made programming them easier. Thus, our renditions will be more aesthetically pleasing (and won't take overnight to render either). Search here (using Google) to find some pages with more background information.

Problem statement: Drawing a Mandelbrot Set

This lab involves computation over some points of a complex plane. Recall that a complex number has two components: a real and an imaginary part. For each complex point c that we want to display, we compute the function rigor(c) as follows.

Pseudocode for rigor(c):

In other words, rigor(c) is the number of iterations that it takes to compute the value at c. The value of maxIters is arbitrary, but let's assume for now that 100 iterations are allowed for the computation. The function abs(z) computes the distance of z from the complex point (0,0).

To show complex numbers on an x-y axis system, let us assume that the real component of a complex number is registered on the x-axis; the imaginary component is thus registered on the y axis. If we apply the above computation over the complex plane as x ranges from -2 to 2 and y ranges from -2i to 2i, we obtain the following picture.

(-2, 2i)(0, 2i)(2, 2i)
(-2, 0i) (2, 0i)
(-2, -2i)(0, -2i)(2, -2i)
Figure 1: Initial display

You should see something similar when your working lab first starts up. The black areas represent points whose computation exceeded the limit set above (maxIters). In the figures shown in this write-up, the color of a non-black point c is computed as the hue-shift away from Color.blue However, you are free to pick your own function for the color. by the value of 7 * rigor(c).

What is really interesting about a fractal drawing is that one can dive into the drawing and discover ever increasing detail. Below you see the results of zooming into the picture.

(-1.6, 0.4i)(0.7, 0.4i)
(-1.6, -0.5i)(0.7, -0.5i)
(-1.5, 0.06i)(-1.4, 0.06i)
(-1.5, -0.06i)(-1.4, -0.06i)
Zoom on left black circle Zoom in further to the left of black circle
Zooming into the picture changes the fractal plane coordinate of the pixels. Part of what you must implement for this lab concerns the mapping between the fractal plane and the pixels on the screen.


Since this is your first design experience, we will describe what is needed for the one class you must design for this lab. The other classes are described in the usual documentation .

For the class you design, you are to submit the API, in JavaDoc printout. Here is how to do that.

  1. Unzip and install the software for this lab as usual.
  2. Using the descriptions below, create a Complex.java file that contain stubs for the methods in your API. A stub is simply the method definition (return time, name, parameters) along with a simple return statement if needed. For a stub, such returns can return 0, null, etc.---whatever seems appropriate.
  3. Make sure your stubs compile correctly along with the class software.
  4. Annotate your code with JavaDoc comments to tell us how we use your class and what the parameters mean.
  5. Use the JDE menu to generate the JavaDoc for the entire lab.
  6. Print out and turn in only the pages for Complex.
For ideas on how this will be used, you can take a look at the documentation for the classes that you are not designing.


  1. Create an immutable Complex number class capable of addition, multiplication, and taking the absolute value (distance of number from (0,0). The names for your methods should be based on your API.
  2. Test your Complex class from Startup. Be sure to exercise all of your methods and print out the results. The testing code should make it obvious that your Complex class works, by emitting messages to Transcript such as:
    (2,3i) + (5,-8i) = (7,-5i)
    The above would illustrate that your addition code works properly.

  3. Take a look at ZoomingListener to see under what conditions it affects Mandelbrot.
  4. Read over the description of how to draw the current Mandelbrot set, described at the end of this write-up.
  5. Implement Mandelbrot without zooming. Your picture should look like the one at the beginning of this write-up.

    Hint: Place the code that actually creates the drawing in a method so it can be called whenver the displayed drawing should change. For example, zooming will affect what is displayed.

  6. Implement zoomto(Rect r). This method is called for you when you drag a rectangle in the display.
  7. Implement the other zooming methods. You get full credit for these methods only if you have them call zoomto(Rect r) with the appropriate rectangle. Duplicating code to achieve zoomin and zoomout will cost you points!

Implementation of Mandelbrot

Required features

The user should be able to

Extra credit

What to turn in:

  1. Complete a design cover sheet for turning in the API for Complex. Attach the JavaDoc printout for Complex.
  2. Complete a code cover sheet.
  3. Provide a printout of any files you have modified.
  4. Provide a transcript from Startup's textual tests on your classes. Recall that this file is produced automatically by the Transcript and can be found in the file transcript.txt in the same folder as your Lab 5 files.

Last modified 19:34:48 CDT 24 October 2000 by Ron K. Cytron