// Sequential mono-color mandelbrot
set program.
// Compile: javac MonoMandl
// Execute: java MonoMandl
import java.awt.*;
import javax.swing.*;
class MonoMandl
{
// Program to calculate Mandelbrot
set.
public static
void main(String[] args)
{
// Create frame for output.
JFrame frame = new JFrame("Mandelbrot
Set Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MandelbrotPanel
panel = new MandelbrotPanel();
frame.getContentPane().add(panel);
frame.setSize(MandelbrotPanel.HEIGHT, MandelbrotPanel.WIDTH);
frame.show();
}
}
class MandelbrotPanel
extends JPanel
{
public static
int HEIGHT=800, WIDTH=800;
int
maxIts = 100;
double minX = -2, minY = -2, maxX = 2, maxY = 2;
// Override JPanel's
method to draw this component.
public void paintComponent(Graphics graphics)
{
super.paintComponent(graphics);
double x,
y;
// Calculate
and draw the mandelbrot set.
for (int xPixel=0; xPixel<WIDTH;
xPixel++)
{ for (int yPixel=0; yPixel<HEIGHT; yPixel++)
{
x = minX
+ xPixel * (maxX - minX) / WIDTH;
y = minY + yPixel *
(maxY - minY) / HEIGHT;
if (calculatePoint(x,
y, maxIts))
graphics.setColor(Color.black);
else graphics.setColor(Color.white);
graphics.drawRect(xPixel,yPixel,1,1);
}
}
}
// Calculate
if point is in Mandelbrot set or not.
boolean
calculatePoint(double cReal,
double cImag, int max)
{
double
temp, zReal = 0, zImag = 0,
lengthSq;
int
iterations = 0;
// Iterate over z(n+1)
= z(n) squared + c
// Stop when result exceeds 4 or
iteration count exceeded
do
{ temp = zReal
* zReal - zImag*zImag + cReal;
zImag = 2.0 * zReal * zImag + cImag;
zReal = temp;
lengthSq = zReal*zReal + zImag*zImag;
iterations++;
} while (lengthSq
< 4.0 && iterations < max);
if
(iterations == max) return true;
return
false;
}
}