/************************************************************************* * Compilation: javac MandelbrotZoom.java * Execution: java MandelbrotZoom * Dependencies: InteractiveDraw.java * * Plots the Mandelbrot set and allows the user to zoom in by selecting * a rectangle. * *************************************************************************/ import java.awt.Color; public class MandelbrotZoom implements DrawListener { private double xmin = -1.5; // center x coordinate private double ymin = -1.0; // center y coordinate private double xmax = 0.5; // width of plot private double ymax = 1.0; // height of plot private boolean dragging; // is the user currently dragging the mouse? private int ITERS = 256; private int SIZE = 200; private InteractiveDraw d = new InteractiveDraw(SIZE, SIZE); public MandelbrotZoom() { d.addListener(this); d.setScale(xmin, ymin, xmax, ymax); draw(); d.show(); } // return number of Mandelbrot iterations to check z = x + iy private static int mand(Complex c, int ITERS) { Complex z = c; for (int t = 0; t < ITERS; t++) { if (z.abs() >= 2.0) return t; z = z.times(z).plus(c); } return ITERS - 1; } private void draw() { System.out.println("Computing...."); d.setScale(0, 0, SIZE, SIZE); double width = xmax - xmin; double height = ymax - ymin; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { double a = xmin + i * width / SIZE; double b = ymin + j * height / SIZE; Complex c = new Complex(a, b); int gray = ITERS - 1 - mand(c, ITERS); d.setColorRGB(gray, gray, gray); d.go(i, j); d.spot(0); } } d.show(); d.setScale(xmin, ymin, xmax, ymax); System.out.println("Done."); } // user clicks the mouse at (x, y) public void mousePressed(double x, double y) { xmin = xmax = x; ymin = ymax = y; d.setColor(Color.yellow); d.XOROn(); d.go(x, y); dragging = true; } private void drawRectangle() { double x = 0.5 * (xmin + xmax); double y = 0.5 * (ymin + ymax); double width = Math.abs(xmax - xmin); double height = Math.abs(ymax - ymin); d.go(x, y); d.spot(width, height); } // user is dragging the mouse, currently at (x, y) public void mouseDragged(double x, double y) { if (!dragging) return; drawRectangle(); xmax = x; ymax = y; drawRectangle(); d.show(); } // user releases the mouse at location (x, y) public void mouseReleased(double x, double y) { if (!dragging) return; drawRectangle(); d.XOROff(); d.setScale(xmin, ymin, xmax, ymax); draw(); d.show(); dragging = false; } // save image to file public void keyTyped(char c) { d.save("mandel" + c + ".png"); } public static void main(String args[]) { MandelbrotZoom m = new MandelbrotZoom(); } }