/************************************************************************* * 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 ColorMandelbrotZoom 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; Color[] colors; private InteractiveDraw d = new InteractiveDraw(SIZE, SIZE); public ColorMandelbrotZoom() { // read in color map colors = new Color[ITERS]; for (int t = 0; t < ITERS; t++) { int r = StdIn.readInt(); int g = StdIn.readInt(); int b = StdIn.readInt(); colors[t] = new Color(r, g, b); } // draw Mandelbrot set and set up mouse listener 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 z, int ITERS) { Complex c = z; for (int t = 0; t < ITERS; t++) { if (c.abs() >= 2.0) return t; c = c.times(c).plus(z); } 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 x = xmin + i * width / SIZE; double y = ymin + j * height / SIZE; Complex z = new Complex(x, y); int t = mand(z, ITERS); d.setColor(colors[t]); 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[]) { new ColorMandelbrotZoom(); } }