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 compile开发者_如何学Pythons 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
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 acceptdouble
arguments and call it asrenderPoint((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
toreturn (((r << 16) | (g << 8) | b)<<4)
and settingMaxColorBit = 16
.
A code review:
(int)Math.floor(Width / 2)
can be replaced byWidth / 2
, because that is an integer division.- You should start your attributes
Width
andHeight
with small letters (width
andheight
), because that is a Java convention which helps differentiate classes and attributes. - The
iterations
attribute is not used.
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
to2+2i
or so. A quick fix would be to divide thex-h
andk-y
expressions by100
before passing them torenderPoint
, and changing the arguments ofrenderPoint
fromint
todouble
. 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();
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
精彩评论