Mandelbrot Set Fractal Viewer in Java - Programming Tutorial
Created by Josef Jelinek
Basic Applet
Computation of a value for each point P (complex number) (good values for P are in ([-2, 0.5], [-1.25, 1.25]), MAX is a number of allowed iterations):
Z[0] := 0 value := 0 WHILE |Z[value]| < 2 and value < MAX DO Z[value + 1] := Z[value] ^ 2 + P value := value + 1; END
Rewritten into an optimized java routine the code looks this way:
int mandel(double px, double py) { double zx = 0.0, zy = 0.0; double zx2 = 0.0, zy2 = 0.0; int value = 0; while (value < MAX && zx2 + zy2 < 4.0) { zy = 2.0 * zx * zy + py; zx = zx2 - zy2 + px; zx2 = zx * zx; zy2 = zy * zy; value++; } return value; }
Notes:
Because there is no interaction (mouse, keyboard, ...), the color palette is spread evenly to all values (0 to MAX).
Drawbacks of the implementation:
- The drawn image is not chached and is recomputed each time the paint event occurs.
Zoomable Interactive Applet
Controls:
- Dragging while pressing left mouse button - zooming into the area
- Right mouse button - increasing the number of iterations (preciseness)
- Middle mouse button - resetting to the initial state
Notes:
The color distribution is changed and each number of iterations has its own color from the palette (the coloring is independent of zooming and the maximum number of iterations). The selected area is not shown graphically, because repainting an interactive rectangle without recomputing the image is complicated.
Drawbacks of the implementation:
- The drawn image is not cached and is recomputed each time the paint event occurs.
- The drawing is performed the in paint() method and blocks the AWT thread untill the image is completed. This complicates interaction that is also controlled by the AWT thread.
Running an applet as a Java application
To be able to run any applet independently of a web browser, just add the following main() method into the class source:
public static void main(String[] args) { Frame frame = new Frame("Fractal Viewer"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Applet applet = new Mandel6(); frame.add(applet, BorderLayout.CENTER); frame.setSize(301, 301); frame.show(); applet.init(); applet.start(); }
The application can be now run both as an applet using web browser and as an application using: "java ClassName".
Well Featured Applet
Controls:
- Dragging with Left-mouse-button - zooming into the selected area
- Dragging with Left-mouse-button and Shift-key - moving to the specified offset
- Right mouse button - increasing the number of iterations (preciseness)
- Escape-key - reinitialization
- O-key - zoom out
- P-key - changing the active palette
- S-key - smoothing the transitions between iteration colors
- A-key - 5-point antialiasing of the image
Notes:
The selected area is shown graphically, however the code that maintains the repainting rectangles and lines is complex and error prone (especially switching rectangle and line with the shift-key). It uses XOR painting mode and different states that are handled by the paint() method.
Drawbacks of the implementation:
- The drawn image is not cached and is recomputed each time the paint event occurs.
- The drawing is performed in the paint() method and blocks the AWT thread untill the image is completed. This complicates interaction that is also controlled by the AWT thread.
Applet with Assynchronous Interlaced Double-buffering
Notes:
The image drawing is performed in a new thread and is performed to an offscreen image buffer. The paint() method only flushes this image to the screen and draws selecting rectangles etc. (they need not be cleared because they are drawn directly to the screen and the image buffer remains unaffected). When an event occurs, the current drawing thread is interrupted and restarted, so the response time of the applet is much shorter (especially when the drawind is slow (e.g. because of antialiasing)). The interlaced displaying gives a quicker overview of the resulting effect of the user's action.
Gallery
All images were obtained as a snapshot of this applet.
Click on the image for a larger version.
Mandelbrot + Julia Applet
Controls:
- Dragging with Left-mouse-button - zooming into the selected area
- Dragging with Left-mouse-button and Shift-key - moving to the specified offset
- Left-mouse-button and Alt-key - activation/deactivation of Julia mode for the selected point of Mandelbrot set
- Right-mouse-button - increasing the number of iterations (preciseness)
- Escape-key - reinitialization
- I-key - zoom in
- O-key - zoom out
- J-key - switching Julia mode
- P-key - changing the active palette
- S-key - smoothing the transitions between iteration colors
- A-key - 5-point antialiasing of the image
Notes:
This applet has synchronized resource access to prevent exceptions from accidental concurrent access to the same resource.
Julia fractals are another nice fractals with the same formula but different starting conditions. There are infinite number of Julia fractals, each one corresponds to one point of the Mandelbrot fractal. To see Julia fractal press Alt-key and click left-mouse-button on the chosen location.
The routine to compute the value was changed to the following one in order to support Julia fractal and transition smoothing (the value is now 256 times larger):
private int mandel(double zRe, double zIm, double cRe, double cIm) { double zRe2 = zRe * zRe; double zIm2 = zIm * zIm; double zM2 = 0.0; int count = 0; while (zRe2 + zIm2 < 4.0 && count < maxCount) { zM2 = zRe2 + zIm2; zIm = 2.0 * zRe * zIm + cIm; zRe = zRe2 - zIm2 + cRe; zRe2 = zRe * zRe; zIm2 = zIm * zIm; count++; } if (count == 0 || count == maxCount) return 0; zM2 += 0.000000001; return 256 * count + (int)(255.0 * Math.log(4.0 / zM2) / Math.log((zRe2 + zIm2) / zM2)); }
Mandelbrot, Phoenix, Julia Applet with Julia Preview
Controls:
- Dragging with Left-mouse-button - zooming into the selected area
- Dragging with Left-mouse-button and Shift-key - moving to the specified offset
- Moving mouse with Control-key pressed - interactive preview of corresponding Julia mode of the fractal
- Left-mouse-button and Control-key - activation/deactivation of Julia mode for the selected point of fractal
- Right-mouse-button - increasing the number of iterations (preciseness)
- Escape-key - reinitialization
- Space-key - next fractal
- I-key - zoom in
- O-key - zoom out
- J-key - switching Julia mode
- P-key - changing the active palette
- S-key - smoothing the transitions between iteration colors
- A-key - 5-point antialiasing of the image