/** * MandelBrot: Plots Mandelbrot set and displays in Web Browser * * @version 1.0, DATE: 10.31.97 * @author Paul Santa Maria */ import java.applet.*; import java.awt.*; // User Interface components // // MandelPlot: Implements Mandelbrot plot // class MandelPlot extends Canvas { private int maxcol = 399, maxrow = 399, max_colors = 8, max_iterations = 512, max_size = 4; private Color cmap[]; /** * Plot a single point in Mandelbrot set */ private void plot (Graphics g, int x, int y, int color_index) { g.setColor (cmap[color_index]); g.drawLine (x,y, x,y); } /** * Constructor: initialize color table for drawing */ public MandelPlot () { cmap = new Color[max_colors]; cmap[0] = Color.black; cmap[1] = Color.red; cmap[2] = Color.green; cmap[3] = Color.blue; cmap[4] = Color.cyan; cmap[5] = Color.magenta; cmap[6] = Color.yellow; cmap[7] = Color.white; } /** * Actually draws the Mandelbrot set */ public void paint (Graphics g) { float Q[] = new float[400]; double Pmax = 1.75,Pmin = -1.75, Qmax = 1.5, Qmin = -1.5, P, deltaP, deltaQ, X, Y, Xsquare, Ysquare; int color, row, col; deltaP = (Pmax - Pmin)/(double)(maxcol - 1); deltaQ = (Qmax - Qmin)/(double)(maxrow - 1); for (row=0; row<=maxrow; row++) { Q[row] = (float)(Qmin + row*deltaQ); } for (col=0; col<=maxcol; col++) { P = Pmin + col*deltaP; for (row=0; row<=maxrow; row++) { X = Y = 0.0; color = 0; for (color=0; color max_size) break; Y = 2*X*Y+Q[row]; X = Xsquare-Ysquare+P; } plot (g, col, row, (int)(color % max_colors)); } } } } /** * Create/display main window */ public class MandelBrot extends Applet { private MandelPlot canvas; // MandelBrot: Frame constructor public void init () { setLayout (new BorderLayout()); canvas = new MandelPlot (); add ("Center", canvas); } // handleEvent: give us a way to gracefully exit the program public boolean handleEvent (Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit (0); return false; } }