/** Mandelbrot Set Generator by Robert John Morton UK-YE572246C 27 October 1997 */ import java.awt.*; public class mandel extends java.applet.Applet implements Runnable { int XBIAS = 220, YBIAS = 130, // pixel co-ordinates of real point 0,0 XMAX = 340, YMAX = 260, // dimensions of the display area (pixels) i, j, v = 0; double Scale = 100; // pixels per unit real eg 100 pixels = 1.00d 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 a colour array for(i = 0; i < XMAX; i++) // for the display area for(j = 0; j < YMAX; j++) // and clear it all A[i][j] = Color.black; // to the background colour. } 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 graph int I = 0; // Clear first dot of next single-colour stretch of line to i = 0; // 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 we've not yet reached end of current line: /* If this dot is a different colour from previous dot then set the painting colour to the colour of the previous dot, draw a line from the start dot to the previous dot and make the reference colour that of this dot. */ if(colour != A[i][j]) { g.setColor(colour); g.drawLine(I,j,i - 1,j); colour = A[I = i][j]; } } g.setColor(colour); // set colour for final stretch of current g.drawLine(I,j,i - 1,j); // line, draw last stretch of current line } } // called in response a request for a repaint() public void update(Graphics g) { if(v < YMAX) { // if last line not yet painted int I = 0, // start pixel for current colour 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, // Reset the values of 'x', 'y', and 'z' y = .01, // in order to start another convergence test. z, /* Convert current pixel co-ordinates to 'real' value co-ordinates on the complex plane. */ cx = (double)(i - XBIAS)/Scale, cy = (double)(YBIAS - v)/Scale; for(int k = 0 ; k < 128 ; k++) { // For each of 128 iterations z = x * y; // compute and add in the next x = x * x - y * y + cx; // term of the complex series. y = z + z + cy; /* If the value of the complex quantity has gone beyond the bounds of the display area: */ if(x < -2.5 || x > 1.5 || y < -2.0 || y > 2.0) { if(k > 19) // Set the maximum permitted number k = 19; // of iterations for divergence, A[i][v] = C[k]; // store the pixel colour and break; // break out of the 'for' loop } } if(colour != A[i][v]) { // If colour has changed, set colour g.setColor(colour); // according to the number of iterations, g.drawLine(I,v,i - 1,v); // draw the plot and set the current colour = A[I = i][v]; // colour as the reference colour. } } g.setColor(colour); // Set colour for final stretch of current g.drawLine(I,v,i - 1,v); // line, draw last stretch of 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) // if not yet finished repaint(); // paint in the next plot // Sleep for 5 milliseconds, ignoring any events during sleep. try { Thread.currentThread().sleep(5); } catch (InterruptedException e) { } } } public void start() { // Start the program thread by Brot = new Thread(this); // creating the run thread object Brot.start(); // then starting it running by } // requesting a call to run() public void stop() { Brot=null; } // Stop this program thread }