SDLFunTimes
SDLFunTimes

Reputation: 785

Help with rendering the Mandelbrot set in Java

I wrote an implementation of the Mandelbrot set in Java using a JComponent but I'm getting strange results when I render it. Besides that everything compiles right. I'm just not for sure what I'm doing wrong with it. Any code review also would be appreciated.

My source is posted on pastebin since it would take up too much room here:

JMandelbrot.java Mandelbrat.java

Upvotes: 2

Views: 1092

Answers (3)

Joseph
Joseph

Reputation: 61

Take a look at this example of Mandelbrot fractal rendering using Marvin: http://marvinproject.sourceforge.net/en/plugins/mandelbrot.html

There is also a plug-in for Julia set rendering: http://marvinproject.sourceforge.net/en/plugins/juliaSet.html

Upvotes: 0

Christian Semrau
Christian Semrau

Reputation: 9013

Problem:

  • The image is as I expect from the code. The Mandelbrot set has a diameter of 2, so you only see some pixels in the middle.

Solution:

  • Change the renderPoint method to accept double arguments and call it as renderPoint((x - h)/100.0, (k - y)/100.0) to see something more interesting.
  • Change the iteration count and color coding, because now you are computing 255^3 iterations for each inner pixel. I managed to see something nice by changing the return of renderPoint to return (((r << 16) | (g << 8) | b)<<4) and setting MaxColorBit = 16.

A code review:

  • (int)Math.floor(Width / 2) can be replaced by Width / 2, because that is an integer division.
  • You should start your attributes Width and Height with small letters (width and height), because that is a Java convention which helps differentiate classes and attributes.
  • The iterations attribute is not used.

Upvotes: 4

Josh Lee
Josh Lee

Reputation: 177624

  • You're drawing the fractal correctly, but it's really small. The entire Mandelbrot set fits in a circle of radius 2, so it barely covers a few pixels in the middle of your 400x500 window.

    You should devise some kind of mapping from the screen window (which goes from (0,0) to (width,height)) to the complex plane, which should have values in the neighborhood of -2-2i to 2+2i or so. A quick fix would be to divide the x-h and k-y expressions by 100 before passing them to renderPoint, and changing the arguments of renderPoint from int to double. Best would be to specify the desired viewing rectangle and use that to determine the mapping.

  • You are computing the fractal image in the GUI thread. This is a no-no, as the application will appear to hang before the window is finished opening. I would change the invocation of render() in the constructor to look like this:

    new Thread() {
      public void run() { render(); }
    }.start();
    

Upvotes: 2

Related Questions