home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 5.6 KB | 272 lines |
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.*;
-
- /*
- * a class for generating and displaying the
- * Mandelbrot set
- */
- public class Mandelbrot extends Applet implements MouseListener,
- MouseMotionListener, ActionListener
- {
-
- /*
- * the maximum number of colors for
- * each pixel
- */
- final int MaxColors = 256;
-
- /*
- * the width and height of the image
- */
- int mWidth, mHeight;
-
- /*
- * the array of pixel values
- */
- int pixels[];
-
- /*
- * the set values
- */
- int mandelSet[];
-
- /*
- * the image produce by the set
- */
- Image theImage = null;
-
- /*
- * the mapping function from set values to pixel values
- */
- int pixMap[] = new int[MaxColors];
-
- /*
- * a flag used for recalculating
- */
- boolean startCalculate = false;
-
- /*
- * instance of MandelRect class
- */
- MandelRect mandelRect;
-
- /*
- * the control buttons
- */
- Button startButton;
- Button zoomButton;
-
- /*
- * called when the applet is loaded
- * initialize the pixmap array and add user interface
- */
- public void init () {
-
- mWidth = 100;
- mHeight = 100;
- pixels = new int [mWidth * mHeight];
- mandelSet = new int [mWidth * mHeight];
-
- mandelRect = new MandelRect (mWidth, mHeight);
-
- int red, green, blue;
- int i;
-
- pixMap[0] = 0xffffffff;
- for (i=1; i<MaxColors-1; i+=1) {
- red = i;
- green = (i<128) ? i << 1 : 255-(i<<1);
- blue = MaxColors-i;
- pixMap[i] = (255 << 24) | (red << 16) | (green << 8) | blue;
- }
- pixMap[MaxColors-1] = 0xff000000;
-
- setLayout(new BorderLayout());
-
- startButton = new Button ("Start over");
- zoomButton = new Button ("Zoom in");
- startButton.addActionListener(this);
- zoomButton.addActionListener(this);
- addMouseListener(this);
- addMouseMotionListener(this);
-
- Panel p = new Panel ();
- p.setLayout (new FlowLayout ());
- p.add (startButton);
- p.add (zoomButton);
- add ("South", p);
- }
-
- /*
- * called when the applet is started
- * forces a recalculation of the set
- */
- public void start () {
-
- startCalculate = true;
- repaint ();
- }
-
- /*
- * call update for efficiency
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- update (g);
- }
-
- /**
- * override default update() method to avoid erase flicker
- * @param g - destination graphics object
- */
- public void update (Graphics g) {
-
- if (startCalculate) {
- calculate ();
- startCalculate = false;
- }
- if (theImage != null) g.drawImage (theImage, 0, 0, this);
- else repaint (1000);
- mandelRect.paint (g);
- }
-
- /*
- * perform the actual set calculation
- */
- void calculate () {
-
- int i, index;
- double width, height;
- double row, col;
- double zr, zi, cr, ci, tzr, tzi;
- double hFactor, vFactor;
- double x, y;
-
- theImage = null;
-
- x = mandelRect.mandelX;
- y = mandelRect.mandelY;
- width = (double) mandelRect.imgWidth;
- height = (double) mandelRect.imgHeight;
- hFactor = mandelRect.mandelWidth/width;
- vFactor = mandelRect.mandelHeight/height;
-
- index = 0;
- for (row=0; row<height; row+=1) {
- for (col=0; col<width; col+=1) {
- zr = 0;
- zi = 0;
- cr = x + col * hFactor;
- ci = y + row * vFactor;
- for (i=1; i<64; i+=1) {
- tzr = zr*zr - zi*zi + cr;
- tzi = 2*zr*zi + ci;
- zr = tzr;
- zi = tzi;
- if (zr*zr + zi*zi > 4.0) break;
- }
- mandelSet[index++] = (i << 2)-1;
- }
- }
-
- for (i=0; i<mWidth*mHeight; i+=1) {
- pixels[i] = pixMap[mandelSet[i]];
- }
-
- theImage = createImage (
- new MemoryImageSource(mWidth, mHeight, pixels, 0, mWidth));
- }
-
- public void mouseClicked(MouseEvent e){}
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
-
- public void mouseReleased(MouseEvent e)
- {
- int x =e.getX();
- int y =e.getY();
-
- mandelRect.setWidthHeight (x, y);
- mandelRect.setPaintRect (true);
- repaint ();
- }
-
- public void mouseMoved(MouseEvent e){}
-
- public void mousePressed(MouseEvent e)
- {
- int x =e.getX();
- int y =e.getY();
-
- mandelRect.setXY (x, y);
- }
-
- public void mouseDragged(MouseEvent e)
- {
- int x =e.getX();
- int y =e.getY();
-
- mandelRect.setWidthHeight (x, y);
- mandelRect.setPaintRect (true);
- repaint ();
-
- }
- /**
- * reorder the images, depending on which button is pressed
- * @param evt - event object
- * @param arg - target object
- */
- public void actionPerformed(ActionEvent evt)
- {
- Object object1 = evt.getSource();
-
- if (object1 == startButton) {
- mandelRect = new MandelRect (mWidth, mHeight);
- startCalculate = true;
- repaint ();
- }
- if (object1 == zoomButton)
- {
- startCalculate = true;
- mandelRect.setPaintRect (false);
- mandelRect.scaleSet ();
- repaint ();
- }
- }
-
- /**
- * application entry point
- * create window and new set
- * @param args - command-line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Mandelbrot set");
- Mandelbrot mandel = new Mandelbrot ();
-
- mandel.init ();
- f.setSize (210, 275);
- f.add ("Center", mandel);
- f.show ();
- f.addWindowListener(new WindowCloser());
-
- mandel.start ();
- }
- }
-
- class WindowCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- Window win = e.getWindow();
- win.setVisible(false);
- win.dispose();
- System.exit(0);
- }
- }
-
-
-