Java Lab 3: Fractal Explorer

For the next few labs you will put together a fun little Java application that can draw some amazing fractals. If you have never played with fractals before, you will be amazed at how easy it is to create some breathtakingly beautiful images. We will do this all with the Swing Framework, the Java API that allows you to create graphical user interfaces.

We will be building this application over multiple labs, so our initial version will be pretty simple, but we will build it up over the next few labs to include some neat features, like being able to save the images we generate, and being able to switch between different kinds of fractals. Both the GUI itself and the mechanism for supporting different fractals will depend on class hierarchies.

Here is a simple example of the GUI in its initial state:

And, here are some interesting areas of the fractal: elephants and seahorses!

Creating the User Interface

Before we can draw any fractals, we'll need to create a graphics widget that will allow us to display them. Swing doesn't provide such a component, but it is very easy to create one ourselves. Note that we will be using a wide range of Java AWT and Swing classes in this lab, and there is simply no way we can explain the details of each one. However, there is no need to, because the online Java API docs are very comprehensive and easy to use. Just navigate to the package of a given Java class, select the class itself, and then read the detailed information about how to use the class.

Computing the Mandelbrot Fractal

Next you will write the code to compute the very well-known Mandelbrot fractal. In order to support multiple fractals in the future, you are provided with the FractalGenerator.java source file, which all of your fractal generators will derive from. You will also notice that some very helpful operations are provided to translate from screen coordinates into the coordinate-system of the fractal being computed.

The kinds of fractals we will be working with are computed in the complex plane, and involve very simple mathematical functions that are iterated repeatedly until some condition is satisfied. For the Mandelbrot fractal, the function is zn = zn-12 + c, where all values are complex numbers, z0 = 0, and c is the particular point in the fractal that we are displaying. This computation is iterated until either |z| > 2 (in which case the point is not in the Mandelbrot set), or until the number of iterations hits a maximum value, e.g. 2000 (in which case we assume the point is in the set).

The process of plotting the Mandelbrot set is very simple: we simply iterate over each pixel in our image, compute the number of iterations for the corresponding coordinate, and then set the pixel to a color based on the number of iterations we computed. But, we will get to this in a second - for now, you simply need to implement the above computation.

Putting It All Together

Finally we are ready to begin displaying fractals! Now you will create a FractalExplorer class that allows you to examine different parts of the fractal by creating and showing a Swing GUI, and handling events caused by various user interactions.

As you can see from the above images of the user interface, the Fractal Explorer is very simple, consisting of a JFrame containing a JImageDisplay object that displays the fractal, and a single JButton for resetting the display to show the entire fractal. You can achieve this simple layout by setting the frame to have a BorderLayout, then putting the display in the center of the layout, and the reset button in the "south" part of the layout.

Once you have completed all these steps, you should be able to cruise around the Mandelbrot fractal looking at the amazing detail. If you zoom in enough you will run into two interesting issues:

You will probably also notice that it is kind of annoying how the entire display hangs while the fractal is being drawn. This is something we will explore in future labs, as well as taking advantage of multiple processors to draw our fractals much faster. But for now, once you have your Fractal Explorer completed (and well commented), you can submit your work to csman!


[end lab 3]
Copyright (c) 2003-2012, California Insitute of Technology.
Last updated May 1, 2012.