Fractal Project

In this project you will write a multithreaded program to render fractals on the screen. If you are running your program on hardware that supports multithreading, then this results in a program that renders faster than a single-threaded version.
picture of the Mandelbrot set

Requirements

You are to write a Java program (applet or application) that renders the Mandelbrot set. All of the requirements below need to be met.

  1. Your program needs to render the Mandelbrot set on the screen, in a manner similar to the image above. Every pixel in the image corresponds to a number c in the complex plane. We start by letting Z0 = 0, and we recursively define Zn+1 = (Zn)2 + c. The pixel is colored according to the least n for which |Zn| > 2. If after some number of iterations |Zn| is still at most 2, we give up and assign some fixed color there. You might want to write a ComplexNumber class with methods for add, multiply, and norm (i.e. absolute value - the distance to the origin).
  2. The user can change the maximum number of iterations of the formula by using keyboard keys or some GUI element (for example, you can use the n and m keys to multiply or divide the max number of iterations by 2). This is useful when zooming in on the fractal.
  3. The work of computing the fractal image should be split up into many smaller tasks; break the image up into rectangular pieces. Each piece is a task that a thread can execute. The number of divisions can be fixed as a constant (say, a 5 by 5 grid of tasks). Be careful with your design so that "seams" don't appear between the different pieces in the final image on account of small arithmetic logical errors. The program should start two worker threads that consume these tasks.
  4. The program needs to provide the ability to zoom in on areas of the fractal. Draw a little rectangle on the screen as the user clicks and drags, so they can see what area they are zooming in on - be careful that the aspect ratio of the rectangle matches the aspect ratio of your applet or application. Otherwise it won't make sense to the user. MouseListener and MouseMotionListener will be useful. See this for example.
  5. Don't draw lots of pixels directly to the screen. That's slow. Use an alternative such as an off-screen image (i.e. double buffer). See for example, this

One possible design would be to implement the following classes: Task, TaskQueue, Worker, and FractalAppplet (or FractalApplication).

The project is due at the beginning of class on Friday, September 10, 2010. Be sure to give your entire program a good proofread before handing it in; even small errors will result in me handing it back for another iteration.