rubikscube.info home

Mandelbrot Set Fractal Viewer in Java - Programming Tutorial

Created by Josef Jelinek

Basic Applet

The Applet Source Code

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:

Zoomable Interactive Applet

The Applet Source Code

Controls:

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:

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

The Applet Source Code

Controls:

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:

Applet with Assynchronous Interlaced Double-buffering

The Applet Source Code

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.

01 02 03 04

05 06 07 08

09 10 11 12

Mandelbrot + Julia Applet

The Applet Source Code

Controls:

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

The Applet Source Code

Controls:

rubikscube.info home

Valid HTML 4.01 Transitional Valid CSS!