JavaFX 8 – Mandelbrot tutorial

Being a Java luddite who still defaults to Swing I thought it was time to learn something new. Here’s a set of tutorials combing JavaFX with the GUI favourite the Mandelbrot fractal.

I won’t dwell on the function f(z)=z2 +C other than to say the JDK doesn’t support Complex numbers, so the initial method looks like this:

private int mandelbrot(double c_re, double c_im) {
    double zx = 0.0, zy = 0.0;
    int iter = 0;
    double xSqr = zx * zx;
    double ySqr = zy * zy;
    while (xSqr + ySqr < 4 && iter < MAX_ITER) {
        tmp = xSqr - ySqr + c_re;
        zy = 2.0 * zx * zy + c_im;
        zx = tmp;
        xSqr = zx * zx;
        ySqr = zy * zy;
    return (iter < MAX_ITER) ? iter : 0;

Stage1 – basic application


source code

What’s good

  • The returned value from the mandelbrot function is written as a RGB value to a WritableRaster and displayed in a ImageView, allowing for the calculation to require only one run and not each time the application is refreshed.
  • ImageView ratio is persevered during resizing.

What’s not:

  • The initial WritableRaster image is used for all resizing operations, this will pixilate badly when the window is enlarged.
  • There are no controls (pan, zoom).
  • Limited colour palette.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: