Applet to Generate the Mandelbrot Set
/** Mandelbrot Set Generator by Robert J Morton 27 October 1997 */
import java.awt.*;
public class Mandelbrot extends java.applet.Applet implements Runnable
{
int XBIAS = 220, YBIAS = 130; //pixel co-ordinates of real point 0,0
int XMAX = 340, YMAX = 260; //dimensions of the display area (pixels)
double Scale = 100; //pixels per unit real eg 100 pixels = 1.00d
int i, j, v = 0;
boolean finished = false;
Color C[], A[][], colour; //colour array and current plot colour
Thread Brot; //declare a thread reference variable
public void init() {
C = new Color[20]; //create colour array
C[ 0] = Color.lightGray; //create colours for the display
C[ 1] = new Color( 0, 0, 128); //series diverged after 2 iterations
C[ 2] = new Color( 0, 136, 0); //series diverged after 3 iterations
C[ 3] = new Color(144, 0, 0); //series diverged after 4 iterations
C[ 4] = new Color( 0, 152, 152); //series diverged after 5 iterations
C[ 5] = new Color(160, 0, 160); //series diverged after 6 iterations
C[ 6] = new Color(168, 168, 0); //etc.
C[ 7] = new Color(176, 176, 176);
C[ 8] = new Color( 16, 16, 184);
C[ 9] = new Color( 16, 192, 16);
C[10] = new Color(200, 16, 16);
C[11] = new Color( 16, 208, 208);
C[12] = new Color(216, 16, 216);
C[13] = new Color(224, 224, 16);
C[14] = new Color(232, 232, 232);
C[15] = new Color( 64, 64, 240);
C[16] = new Color( 64, 248, 64);
C[17] = new Color(252, 64, 64);
C[18] = new Color(255, 255, 64);
C[19] = new Color(255, 255, 255);
A = new Color[XMAX][YMAX]; //create colour array for the display area
for(i = 0 ; i < XMAX ; i++) //clear the colour array to background colour
for(j = 0 ; j < YMAX ; j++)
A[i][j] = Color.black;
}
public void paint(Graphics g) { //PAINT / RE-PAINT THE PICTURE SO FAR
for(j = 0 ; j < YMAX ; j++) { //for each of the 180 lines in the picture
int I = 0; //starting dot of next single-colour stretch of line
i = 0; //to find next pixel in current line with a different colour
colour = A[I][j]; //make colour of first dot the reference colour
while(++i < XMAX) { //while not yet reached end of current line
if(colour != A[i][j]) { //if this dot a different colour from previous dot
g.setColor(colour); //set colour to colour of previous dot
g.drawLine(I, j, i - 1, j); //draw line from start dot to previous dot
colour = A[I = i][j]; //make reference colour that of this dot
}
}
g.setColor(colour); //set colour for final stretch of current line
g.drawLine(I, j, i - 1, j); //draw the last stretch of the current line
}
}
public void update(Graphics g) { //called in response a request for a repaint()
if(v < YMAX) { //if last line not yet painted
int I = 0; //start pixel for current colour
int i = 0; //current pixel of current colour
colour = A[I][v]; //make colour of first dot the reference colour
while(++i < XMAX) { //for each pixel on the current line...
double x = .01, y = .01, z, //reset values to start another convergence test
cx = (double)(i - XBIAS) / Scale, //Convert current pixel co-ordinates to real
cy = (double)(YBIAS - v) / Scale; // co-ordinates on the complex plane.
for(int k = 0 ; k < 128 ; k++) { //if not yet completed 128 iterations
z = x * y; //compute and add in the next term of the
x = x * x - y * y + cx; //complex series
y = z + z + cy;
if(x < -2.5 || x > 1.5 || //if the value of the complex quantity has
y < -2.0 || y > 2.0) { //gone beyond the bounds of the display area
if(k > 19) k = 19; //max number of iterations for divergence
A[i][v] = C[k]; //store the pixel colour
break;
}
}
if(colour != A[i][v]) { //if colour has changed
g.setColor(colour); //set colour according to number of iterations
g.drawLine(I, v, i - 1, v); //draw the plot
colour = A[I = i][v]; //set current colour as reference colour
}
}
g.setColor(colour); //set colour for final stretch of current line
g.drawLine(I, v, i - 1, v); //draw the last stretch of the current line
v++; //advance downwards to the next line
} else finished = true;
}
public void run() { //run the Track Recorder painting thread
while(true) { //permanent loop
if(!finished) repaint(); //paint in the next plot
try { Thread.currentThread().sleep(5); //sleep for the time remaining
} catch (InterruptedException e) { } //catch interrupt from GUI or browser
}
}
public void start() { //Start the program thread by
Brot = new Thread(this); //creating the thread object
Brot.start(); //and starting it running by
} //requesting a call to run()
public void stop() { Brot.stop(); } //Stop this a program thread
/* Robert J Morton, the author of this program,
is a poor but Right Honourable Fellow of the
Ancient and Noble Order of the Long-term Unemployed.
Offers of work please to: robmorton@clara.net */
}
This page's parent within this Web Site. About this Web Site. Its home page. Email its Author.