Help with displaying a Mandelbrot set in Java
I wrote an implementation of a Mandelbrot set in Java using JComponent, but I am getting strange results when rendering it. Also, everything is ready. I'm just not sure what I'm doing with it. Any code review would also be appreciated.
My source has been posted to pastebin as it will take up too much space:
a source to share
Problem:
- The image is as I expect from the code. The Mandelbrot set has a diameter of 2, so you only see a few pixels in the middle.
Decision:
- Modify the method
renderPoint
to accept argumentsdouble
and name it likerenderPoint((x - h)/100.0, (k - y)/100.0)
to see something more interesting. - Change the number of iterations and color coding because now you are calculating 255 ^ 3 iterations for each inner pixel. I was able to see something nice by changing the return
renderPoint
toreturn (((r << 16) | (g << 8) | b)<<4)
and settingMaxColorBit = 16
.
Code review:
-
(int)Math.floor(Width / 2)
can be replaced withWidth / 2
, because it is integer division. - You should start your attributes
Width
andHeight
with small letters (Width
andHeight
) because it is the Java agreement, which helps to distinguish the classes and attributes. - The attribute is
iterations
not used.
a source to share
-
You are drawing the fractal correctly, but it is really small. The entire Mandelbrot set fits into a circle of radius 2, so it barely covers a few pixels in the middle of the 400x500 window.
You have to design some kind of mapping from the screen window (from
(0,0)
to(width,height)
) to the complex plane, which should have values in the vicinity of-2-2i
to2+2i
or so on. A quick fix would be to split the expressionsx-h
andk-y
by100
before passing them torenderPoint
, and change the argumentsrenderPoint
fromint
todouble
. Your best bet would be to specify the desired view rectangle and use that to define the display. -
You compute a fractal image on the GUI thread. This is no-no, as the application seems to hang before the window is finished. I would change the call
render()
in the constructor to look like this:new Thread() { public void run() { render(); } }.start();
a source to share
Take a look at this example of Mandelbrot fractal rendering by Marvin: http://marvinproject.sourceforge.net/en/plugins/mandelbrot.html
There is also a Julia rendering plugin: http://marvinproject.sourceforge.net/en/plugins/juliaSet.html
a source to share