/** Copyright: Peter Cappello Feel free to use this code as is, provided that you leave these comments in place. Feel free to modify this code, provided that you explicitly indicate that your code is based on Peter Cappello's code A Mandelbrot Applet */ import java.awt.*; import java.applet.*; import java.awt.event.*; import java.awt.image.*; public class MandelbrotApplet extends Applet implements ActionListener, MouseListener { int clr = 1, // bits of R,G,B; lim = 2^(3*clr) lgR = 9, // resolution: descretize region into 2^lgR x 2^lgR squares lgA = 9, // side of atomic region has 2^lgA squares com = 0; // command: 0 - init; 1 - zoom; 2 - unzoom; MandelbrotSet m = new MandelbrotSet(-2.0, -1.25, 2.5, clr, lgR, lgA); final static int PIXELS = 512, // number of pixels in display region Y = 40; // vertical offset: display region below GUI Label resL = new Label("Resolution"), clrL = new Label("Bits of Red, Green, Blue"), comL = new Label("Command"), atmL = new Label("Atomic size"); TextField resT = new TextField("9", 1), clrT = new TextField("1", 4), comT = new TextField("0", 1), atmT = new TextField("9", 3); public void init() { add(resL); add(resT); add(clrL); add(clrT); add(comL); add(comT); add(atmL); add(atmT); resT.addActionListener(this); clrT.addActionListener(this); comT.addActionListener(this); atmT.addActionListener(this); addMouseListener(this); } public void paint(Graphics g) { // Based on command, establish coordinates of region switch (com) { // restore original region coordinates case 0: m.setRegion(-2.0,-1.25,2.5); break; // zoom in, centered on mouse release coordinates case 1: m.zoom(); break; // zoom out, centered on mouse release coordinates case 2: m.unzoom(); break; } // display Mandelbrot set m.draw(this, g); } public void actionPerformed(ActionEvent e) { updateParams(); } private void updateParams() { lgR = Integer.parseInt(resT.getText()); lgR = m.setLgr(lgR); resT.setText(Integer.toString(lgR)); lgA = Integer.parseInt(atmT.getText()); lgA = m.setLga(lgA); atmT.setText(Integer.toString(lgA)); clr = Integer.parseInt(clrT.getText()); clr = m.setClr(clr); clrT.setText(Integer.toString(clr)); com = Integer.parseInt(comT.getText()); repaint(); } public void mousePressed(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) { int y = PIXELS - e.getY() + Y; m.recenter(e.getX(), y, PIXELS); updateParams(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }