// // The Mandelbrot Set // // Mark Maloof // July 1998 // // Portions taken from Hearn and Baker. Windows taken from Fractal // Programming in C by Roger T. Stevens. PixelMap code taken from // Java How-To by Madhu Siddalingaiah and Stephen Lockwood. // import java.awt.*; import java.awt.event.*; /*-----------------------------------------------------------------------*/ public class mandy extends Frame { public mandy() { super("COSC 286 Mandelbrot Set"); // Create the window this.setSize(300, 250); // Specify frame size this.setResizable(false); // Prevent resizing (supposedly) // Create a ScrollPane ScrollPane pane = new ScrollPane(ScrollPane.SCROLLBARS_NEVER); pane.setSize(300, 250); // Specify its size this.add(pane, "Center"); // Add it to the frame DrawingArea scribble; scribble = new DrawingArea(this); // Create a drawing area pane.add(scribble); // Add it to the ScrollPane MenuBar menubar = new MenuBar(); // Create a menubar this.setMenuBar(menubar); // Add it to the frame Menu file = new Menu("File"); // Create a File menu menubar.add(file); // Add to menubar // Create a quit menu item with a shortcut, and add it to the menu MenuItem q; file.add(q = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q))); // Create and register an action listener object for the menu item. q.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // Create and register action listener for the window closing event. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Set the window size and pop it up. this.pack(); this.show(); } // mandy public static void main(String[] args) { new mandy(); } // main } // mandy class /*-----------------------------------------------------------------------*/ class DrawingArea extends Component { protected Frame frame; public DrawingArea(Frame frame) { this.frame = frame; } // DrawingArea public void paint(Graphics g) { Dimension d = getSize(); // Dimensions of the drawing area Mandelbrot m = new Mandelbrot(d.width-1, d.height-1); //m.setWindow(-0.702973, -0.642879, 0.374785, 0.395415); //m.setWindow(-0.691594, -0.690089, 0.386608, 0.387494); //m.setWindow(-0.691060, -0.690906, 0.387103, 0.387228); //m.setWindow(-0.793114, -0.723005, 0.037822, 0.140974); //m.setWindow(-0.749337, -0.744948, 0.109349, 0.115851); //m.setWindow(-0.745465, -0.745387, 0.112896, 0.113034); //m.setWindow(-0.745464, -0.745388, 0.112897, 0.113030); m.plot(g); } // paint } // DrawingArea class /*-----------------------------------------------------------------------*/ class Mandelbrot { int xvMax, yvMax; int maxIter; double realMin, realMax; double imagMin, imagMax; double realInc, imagInc; int[] pixelMap; Mandelbrot(int xvMax, int yvMax) { this.xvMax = xvMax; this.yvMax = yvMax; this.maxIter = 5000; this.realMin = -2.0; this.realMax = 1.2; this.imagMin = -1.2; this.imagMax = 1.2; this.realInc = (realMax - realMin) / xvMax; this.imagInc = (imagMax - imagMin) / yvMax; initializePixelMap(); } // Mandelbrot public void setWindow(double realMin, double realMax, double imagMin, double imagMax) { this.realMin = realMin; this.realMax = realMax; this.imagMin = imagMin; this.imagMax = imagMax; this.realInc = (realMax - realMin) / xvMax; this.imagInc = (imagMax - imagMin) / yvMax; } // setWindow private void initializePixelMap() { pixelMap = new int[maxIter]; for (int i = 0; i < maxIter-1; i++) { int red = i; int green = (i < 128) ? i << 1 : 255-(i<<1); int blue = maxIter - i; pixelMap[i] = (255 << 24) | (red << 16) | (green << 8) | blue; } // for pixelMap[maxIter-1] = 0xff000000; } // initializePixelMap private int iterate(Complex z0) { Complex z = new Complex(z0); int count = 0; while (z.magnitudeSqr() < 4.0 && count < this.maxIter) { z.sqr(); z.add(z0); count++; } // while return count; } // iterate public void plot(Graphics g) { Complex z = new Complex(); z.r = this.realMin; for (int x = 0; x < this.xvMax; x++) { z.i = this.imagMin; for (int y = 0; y < this.yvMax; y++) { int count = iterate(z); if (count == this.maxIter) g.setColor(Color.black); else //g.setColor(Color.white); g.setColor(new Color(pixelMap[count])); g.drawLine(x, y, x, y); z.i += this.imagInc; } // for z.r += this.realInc; } // for } // plot } // Mandelbrot class /*=======================================================================*/ class Complex { double r; // the real part double i; // the imaginary part Complex () { this.r = 0.0; this.i = 0.0; } // Complex Complex (Complex c) { this.r = c.r; this.i = c.i; } // Complex public String toString() { return "{" + this.r + ", " + this.i + "}"; } // toString public void add (Complex c) { this.r += c.r; this.i += c.i; } // add public void sqr () { double rsqr = this.r * this.r - this.i * this.i; double isqr = 2.0 * this.r * this.i; this.r = rsqr; this.i = isqr; } // sqr public double magnitudeSqr () { return this.r * this.r + this.i * this.i; } // magnitude } // Complex class