1 import java.applet.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.awt.image.MemoryImageSource ; 5 import JSci.maths.Complex; 6 import JSci.maths.chaos.*; 7 8 13 public final class MandelbrotPlot extends Applet { 14 private final int colorTable[]=new int[22]; 15 private final int range[]={1,10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000}; 16 private final int N=2000; 17 private Image mandelbrotImage; 18 private Image imageBuffer; 19 20 23 public void init() { 24 for(int i=0;i<colorTable.length;i++) 26 colorTable[i]=grey(255-10*i); 27 mandelbrotImage=drawMandelbrot(); 29 addMouseListener(new MouseAdapter() { 31 public void mouseClicked(MouseEvent evt) { 32 if(imageBuffer==mandelbrotImage) { 33 final int width=getSize().width; 34 final int height=getSize().height; 35 final Complex z=new Complex(evt.getX()*3.0/width-2.0,1.25-evt.getY()*2.5/height); 36 imageBuffer=drawJulia(z); 37 System.err.println(z.toString()); 38 showStatus("Click to return to Mandelbrot set"); 39 } else { 40 imageBuffer=mandelbrotImage; 41 showStatus("Click to generate a Julia set"); 42 } 43 repaint(); 44 } 45 }); 46 } 47 public void start() { 48 imageBuffer=mandelbrotImage; 49 showStatus("Click to generate a Julia set"); 50 } 51 public void paint(Graphics g) { 52 g.drawImage(imageBuffer,0,0,this); 53 } 54 private Image drawMandelbrot() { 55 final int width=getSize().width; 56 final int height=getSize().height; 57 final MandelbrotSet set=new MandelbrotSet(); 58 double x,y; 59 int pixels[]=new int[width*height]; 60 int index=0; 61 for(int j=0;j<height;j++) { 62 for(int i=0;i<width;i++) { 63 x=i*3.0/width-2.0; 64 y=1.25-j*2.5/height; 65 pixels[index++]=colorLookup(set.isMember(x,y,N)); 66 } 67 } 68 return createImage(new MemoryImageSource (width,height,pixels,0,width)); 69 } 70 private Image drawJulia(Complex z) { 71 final int width=getSize().width; 72 final int height=getSize().height; 73 final JuliaSet set=new JuliaSet(z); 74 double x,y; 75 int pixels[]=new int[width*height]; 76 int index=0; 77 for(int j=0;j<height;j++) { 78 for(int i=0;i<width;i++) { 79 x=i*4.0/width-2.0; 80 y=1.25-j*2.5/height; 81 pixels[index++]=colorLookup(set.isMember(x,y,N)); 82 } 83 } 84 return createImage(new MemoryImageSource (width,height,pixels,0,width)); 85 } 86 89 private int colorLookup(int n) { 90 if(n==0) { 91 return 0xff000000; } else { 93 for(int i=0;i<range.length-1;i++) { 94 if(n>=range[i] && n<range[i+1]) 95 return colorTable[i]; 96 } 97 return 0xffffffff; } 99 } 100 103 private static int grey(int n) { 104 return 0xff000000 | n<<16 | n<<8 | n; 105 } 106 } 107 108 | Popular Tags |