001: import java.applet.*;
002: import java.awt.*;
003: import java.awt.event.*;
004: import java.awt.image.MemoryImageSource;
005: import JSci.maths.Complex;
006: import JSci.maths.chaos.*;
007:
008: /**
009: * Plot of the Mandelbrot set.
010: * @author Mark Hale
011: * @version 1.1
012: */
013: public final class MandelbrotPlot extends Applet {
014: private final int colorTable[] = new int[22];
015: private final int range[] = { 1, 10, 20, 30, 40, 50, 60, 70, 80,
016: 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
017: private final int N = 2000;
018: private Image mandelbrotImage;
019: private Image imageBuffer;
020:
021: /**
022: * Initialise the applet.
023: */
024: public void init() {
025: // create colour palette
026: for (int i = 0; i < colorTable.length; i++)
027: colorTable[i] = grey(255 - 10 * i);
028: // draw Mandelbrot set to an image buffer
029: mandelbrotImage = drawMandelbrot();
030: // mouse listener
031: addMouseListener(new MouseAdapter() {
032: public void mouseClicked(MouseEvent evt) {
033: if (imageBuffer == mandelbrotImage) {
034: final int width = getSize().width;
035: final int height = getSize().height;
036: final Complex z = new Complex(evt.getX() * 3.0
037: / width - 2.0, 1.25 - evt.getY() * 2.5
038: / height);
039: imageBuffer = drawJulia(z);
040: System.err.println(z.toString());
041: showStatus("Click to return to Mandelbrot set");
042: } else {
043: imageBuffer = mandelbrotImage;
044: showStatus("Click to generate a Julia set");
045: }
046: repaint();
047: }
048: });
049: }
050:
051: public void start() {
052: imageBuffer = mandelbrotImage;
053: showStatus("Click to generate a Julia set");
054: }
055:
056: public void paint(Graphics g) {
057: g.drawImage(imageBuffer, 0, 0, this );
058: }
059:
060: private Image drawMandelbrot() {
061: final int width = getSize().width;
062: final int height = getSize().height;
063: final MandelbrotSet set = new MandelbrotSet();
064: double x, y;
065: int pixels[] = new int[width * height];
066: int index = 0;
067: for (int j = 0; j < height; j++) {
068: for (int i = 0; i < width; i++) {
069: x = i * 3.0 / width - 2.0;
070: y = 1.25 - j * 2.5 / height;
071: pixels[index++] = colorLookup(set.isMember(x, y, N));
072: }
073: }
074: return createImage(new MemoryImageSource(width, height, pixels,
075: 0, width));
076: }
077:
078: private Image drawJulia(Complex z) {
079: final int width = getSize().width;
080: final int height = getSize().height;
081: final JuliaSet set = new JuliaSet(z);
082: double x, y;
083: int pixels[] = new int[width * height];
084: int index = 0;
085: for (int j = 0; j < height; j++) {
086: for (int i = 0; i < width; i++) {
087: x = i * 4.0 / width - 2.0;
088: y = 1.25 - j * 2.5 / height;
089: pixels[index++] = colorLookup(set.isMember(x, y, N));
090: }
091: }
092: return createImage(new MemoryImageSource(width, height, pixels,
093: 0, width));
094: }
095:
096: /**
097: * Returns a RGBA value.
098: */
099: private int colorLookup(int n) {
100: if (n == 0) {
101: return 0xff000000; // black
102: } else {
103: for (int i = 0; i < range.length - 1; i++) {
104: if (n >= range[i] && n < range[i + 1])
105: return colorTable[i];
106: }
107: return 0xffffffff; // white
108: }
109: }
110:
111: /**
112: * Returns a grey RGBA value.
113: */
114: private static int grey(int n) {
115: return 0xff000000 | n << 16 | n << 8 | n;
116: }
117: }
|