//mandel.java import java.awt.*; import java.util.*; public class mandel extends java.applet.Applet{ private TextField paramField[] = new TextField[5]; private MandCanvas paper; private final static int defaultValue[] = {0,0,32,1,2}; public void init(){ Panel thePanel; String lavelName[] = {"center X","center Y","Max Loop","zoom","n"}; setLayout(new BorderLayout(4,4)); thePanel=new Panel(); paper = new MandCanvas(); thePanel.setLayout(new GridLayout(7,2,2,2)); for(int i = 0 ; i < 5 ; i++ ){ thePanel.add(new Label(lavelName[i],Label.RIGHT)); thePanel.add(paramField[i] = new TextField("" + defaultValue[i])); } thePanel.add( new Button("decrease")); thePanel.add( new Button("reDraw")); thePanel.add( new Button("Reset")); add("North",new Label("MandelZoom by Task",Label.CENTER)); add("West",thePanel); add("Center",paper); paper.resize(size().height - 60,size().height - 60); paper.setArea(defaultValue[0],defaultValue[1],defaultValue[2],defaultValue[3],defaultValue[4]); showStatus("hello! Mand Zoom Applet"); } public String getAppletInfo() { String ainfo = "MandelZoom v.0.2 (21 June 1996), by Task"; return ainfo; } public void showParam(){ paramField[0].setText("" + paper.centerX); paramField[1].setText("" + paper.centerY); paramField[2].setText("" + paper.max); paramField[3].setText("" + paper.zoom); paramField[4].setText("" + paper.n); } public void getParam ( ) { double centerX,centerY,n; int max,zoom; Double buffer; Integer ibuff; try { buffer = Double.valueOf(paramField[0].getText()); centerX = buffer.doubleValue(); } catch (Exception e) { centerX = defaultValue[0]; } try { buffer = Double.valueOf(paramField[1].getText()); centerY = buffer.doubleValue(); } catch (Exception e) { centerY = defaultValue[1]; } try { ibuff = Integer.valueOf(paramField[2].getText()); max = ibuff.intValue(); } catch (Exception e) { max = defaultValue[2]; } try { ibuff = Integer.valueOf(paramField[3].getText()); zoom = ibuff.intValue(); } catch (Exception e) { zoom = defaultValue[3]; } try { buffer = Double.valueOf(paramField[4].getText()); n = buffer.doubleValue(); } catch (Exception e) { n = defaultValue[1]; } paper.setArea(centerX,centerY,max,zoom,n); } public boolean mouseUp(Event evt,int x,int y){ if(evt.target instanceof MandCanvas){ this.stop(); paper.zoom( x - paper.location().x , y - paper.location().y ); showParam(); this.start(); return true; } return false; } public boolean mouseMove (Event evt,int x,int y){ if (evt.target instanceof MandCanvas){ Complex code = paper.getPosition ( x - paper.location().x, y - paper.location().y); showStatus("r = " + code.r + " i = " + code.i); return true; } return false; } public boolean action(Event evt,Object o){ if (evt.target instanceof Button){ String name = ((Button)evt.target).getLabel(); if(name.equals("Reset")){ paper.stop(); paper.setArea(defaultValue[0],defaultValue[1],defaultValue[2],defaultValue[3],defaultValue[4]); showParam(); showStatus("push Reset"); paper.start(); return true; }else if(name.equals("decrease")){ paper.stop(); paper.decrease(); showParam(); showStatus("push decrease"); paper.start(); return true; }else if(name.equals("reDraw")){ paper.stop(); getParam(); showStatus("push redraw"); paper.start(); return true; } return false; } return false; } public void destroy(){ this.stop(); } public void start(){ paper.start(); } public void stop (){ paper.stop(); } public void update (Graphics g){ paint(g); } } /** * Mand Canvas class * culc Mandelbrot & disp double buffered & zoom */ class MandCanvas extends Canvas implements Runnable{ public double centerX,centerY,mx1,mx2,my1,my2,n = 3.0; int x1,y1,x2,y2,max,zoom; long miliTime; Thread t; Image offscreenImage; Graphics offscreenGfx; MandCanvas(){ } public void start(){ Dimension r = size(); if (offscreenImage == null){ offscreenImage = createImage(size().width,size().height); //System.out.println("-- bc width = " + size().width + " height = " + size().height); offscreenGfx = offscreenImage.getGraphics(); }else{ offscreenGfx.clearRect(0,0,size().width,size().height); } x1 = 0; y1 = 0; x2 = r.width - 1; y2 = r.height - 1; if(t==null){ t=new Thread(this); t.start(); } } public void stop (){ if(t!=null){ t.stop(); t=null; } } public Complex getPosition (int x , int y){ double base = (double)1.0 / (((double)( size().width + size().height ) / (double)8.0 ) * (double)zoom); double mx = centerX + (x - size().width/2)* base; double my = centerY + (y - size().height/2)* base; return new Complex( mx , my ); } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ g.drawImage(offscreenImage,0,0,this); } public void setArea(double centerX,double centerY,int max,int zoom,double n){ this.centerX = centerX; this.centerY = centerY; this.max = max; this.zoom = zoom; this.n = n; } public void decrease(){ if (zoom > 2){ zoom /= 2; }else{ zoom = 1; } } public void zoom(int x,int y){ double base = (double)1.0 / (((double)(size().width + size().height ) / (double)8.0 ) * (double)zoom); centerX += (x - size().width/2)* base; centerY += (y - size().height/2)* base; zoom *= 2; } public void run(){ //Mandelbrot(offscreenGfx); loopCulcMand(offscreenGfx); repaint(); } private int culcPoint ( double x0 , double y0 , int max){ //double zr0,zi0,zr,zi; Complex Z = new Complex(0.0,0.0); Complex C = new Complex(x0,y0); Complex Z2 = new Complex(0.0,0.0); int s; //zr0 = x0; //zi0 = y0; for ( s = 0 ; s < max ; s++ ) { // Z = Complex.sqrt(new Complex(Z.r - C.r , Z.i - C.i)); // if(Z.r*Z.r+Z.i*Z.i>=4.0) break; Z2 = Complex.pow(Z,n); Z.r = Z2.r + C.r; Z.i = Z2.i + C.i; if(Z.r*Z.r+Z.i*Z.i>=4.0) break; /* zr=zr0*zr0-zi0*zi0+x0; zi=2*zr0*zi0+y0; if(zr*zr+zi*zi>=4.0) break; zr0=zr; zi0=zi; */ } return s; } public void Mandelbrot(Graphics g){ repaint(); Date now = new Date(); miliTime = now.getTime() + 1000; //System.out.println("imageWidth = " + rw + " imageHeight = " + rh); double base = (double)1.0 / (((double)( x2 + y2 ) / (double)8.0 ) * (double)zoom); mx1 = centerX - (double) x2 * base / 2.0; mx2 = mx1 + (double)x2 * base; my1 = centerY - (double)y2 * base / 2.0; my2 = my1 + (double)y2 * base; double xs = (mx2 - mx1) / x2; double ys = (my2 - my1) / y2; int newWidth = x2 / 2; int newHeight = y2 / 2; processFrame(g, new Rectangle(x1, y1, newWidth, newHeight), xs, ys, max); processFrame(g, new Rectangle(x1, y1+newHeight, newWidth,y2-newHeight), xs, ys, max); processFrame(g, new Rectangle(x1+newWidth, y1, x2-newWidth, newHeight), xs, ys, max); processFrame(g, new Rectangle(x1+newWidth, y1+newHeight, x2-newWidth,y2-newHeight), xs, ys, max); repaint(); } private void processFrame(Graphics g, Rectangle r, double xs, double ys, int l) { int x1 = r.x; int y1 = r.y; int x2 = r.x + r.width; int y2 = r.y + r.height; boolean cracked = false; double x0, y0; Date now = new Date(); if (miliTime < now.getTime()){ miliTime = now.getTime() + 1000; repaint(); try{Thread.sleep(50); } catch(InterruptedException e){} } int s = culcPoint(mx1+x1*xs, my1+y1*ys, l); // 1 x0 = mx1 + x1 * xs; for (int y = y1; y <= y2; y++) { y0 = my1 + y * ys; int smpl = culcPoint(x0, y0, l); if (smpl < l){ g.setColor(new Color(128+smpl*20,smpl*20,255-smpl*20)); g.drawLine(x1,y,x1,y); } else { g.setColor(Color.black); g.drawLine(x1,y,x1,y); } if (smpl != s) { cracked = true; } } // 2 y0 = my1 + y1 * ys; for (int x = x1; x <= x2; x++) { x0 = mx1 + x * xs; int smpl = culcPoint(x0, y0, l); if (smpl < l){ g.setColor(new Color(128+smpl*20,smpl*20,255-smpl*20)); g.drawLine(x,y1,x,y1); } else { g.setColor(Color.black); g.drawLine(x,y1,x,y1); } if (smpl != s) { cracked = true; } } // 3 x0 = mx1 + x2 * xs; for (int y = y1; y <= y2; y++) { y0 = my1 + y * ys; int smpl = culcPoint(x0, y0, l); if (smpl < l){ g.setColor(new Color(128+smpl*20,smpl*20,255-smpl*20)); g.drawLine(x2,y,x2,y); } else { g.setColor(Color.black); g.drawLine(x2,y,x2,y); } if (smpl != s) { cracked = true; } } // 4 y0 = my1 + y2 * ys; for (int x = x1; x <= x2; x++) { x0 = mx1 + x * xs; int smpl = culcPoint(x0, y0, l); if (smpl < l){ g.setColor(new Color(128+smpl*20,smpl*20,255-smpl*20)); g.drawLine(x,y2,x,y2); } else { g.setColor(Color.black); g.drawLine(x,y2,x,y2); } if (smpl != s) { cracked = true; } } if ((r.width <= 1)||(r.height <= 1)) return; if (cracked) { int newWidth = r.width / 2; int newHeight = r.height / 2; processFrame(g, new Rectangle(r.x+1, r.y+1, newWidth, newHeight), xs, ys, l); processFrame(g, new Rectangle(r.x+1, r.y+newHeight+1, newWidth, r.height-newHeight-2), xs, ys, l); processFrame(g, new Rectangle(r.x+newWidth+1, r.y+1, r.width-newWidth-2, newHeight), xs, ys, l); processFrame(g, new Rectangle(r.x+newWidth+1, r.y+newHeight+1, r.width-newWidth-2, r.height-newHeight-2), xs, ys, l); } else { if (s < l){ g.setColor(new Color(128+s*20,s*20,255-s*20)); g.fillRect(r.x, r.y, r.width, r.height); } else { g.setColor(Color.black); g.fillRect(r.x, r.y, r.width, r.height); } } } public void loopCulcMand(Graphics g){ Date now = new Date(); miliTime = now.getTime() + 1000; repaint(); //System.out.println("imageWidth = " + rw + " imageHeight = " + rh); double base = (double)1.0 / (((double)( x2 + y2 ) / (double)8.0 ) * (double)zoom); mx1 = centerX - (double) x2 * base / 2.0; mx2 = mx1 + (double)x2 * base; my1 = centerY - (double)y2 * base / 2.0; my2 = my1 + (double)y2 * base; double xs = (mx2 - mx1) / x2; double ys = (my2 - my1) / y2; int newWidth = x2 / 2; int newHeight = y2 / 2; Rectangle rectList[] = new Rectangle[4]; rectList[0] = new Rectangle(x1, y1, newWidth-1, newHeight-1); rectList[1] = new Rectangle(x1, y1+newHeight, newWidth-1,y2-newHeight); rectList[2] = new Rectangle(x1+newWidth, y1, x2-newWidth, newHeight-1); rectList[3] = new Rectangle(x1+newWidth, y1+newHeight, x2-newWidth,y2-newHeight); Rectangle nextList[]; boolean culcDone = false; int q = rectList.length; do{ nextList = new Rectangle[q*4]; int listMax = q; q = 0; for(int p = 0; p < listMax; p++){ Rectangle r = rectList[p]; int x1 = r.x; int y1 = r.y; int x2 = r.x + r.width; int y2 = r.y + r.height; boolean cracked = false; double x0, y0; now = new Date(); if (miliTime < now.getTime()){ miliTime = now.getTime() + 1000; repaint(); try{Thread.sleep(50); } catch(InterruptedException e){} } int s = culcPoint(mx1+x1*xs, my1+y1*ys, max); if (s < max){ g.setColor(new Color(128+s*20,s*20,255-s*20)); g.fillRect(r.x,r.y,r.width,r.height); } else { g.setColor(Color.black); g.fillRect(r.x,r.y,r.width,r.height); } // 1 x0 = mx1 + x1 * xs; for (int y = y1; y <= y2; y++) { y0 = my1 + y * ys; int smpl = culcPoint(x0, y0, max); if (smpl < max){ g.setColor(new Color(128+smpl*20,smpl*20,255-smpl*20)); g.drawLine(x1,y,x1,y); } else { g.setColor(Color.black); g.drawLine(x1,y,x1,y); } if (smpl != s) { cracked = true; } } // 2 y0 = my1 + y1 * ys; for (int x = x1; x <= x2; x++) { x0 = mx1 + x * xs; int smpl = culcPoint(x0, y0, max); if (smpl < max){ g.setColor(new Color(128+smpl*20,smpl*20,255-smpl*20)); g.drawLine(x,y1,x,y1); } else { g.setColor(Color.black); g.drawLine(x,y1,x,y1); } if (smpl != s) { cracked = true; } } // 3 x0 = mx1 + x2 * xs; for (int y = y1; y <= y2; y++) { y0 = my1 + y * ys; int smpl = culcPoint(x0, y0, max); if (smpl < max){ g.setColor(new Color(128+smpl*20,smpl*20,255-smpl*20)); g.drawLine(x2,y,x2,y); } else { g.setColor(Color.black); g.drawLine(x2,y,x2,y); } if (smpl != s) { cracked = true; } } // 4 y0 = my1 + y2 * ys; for (int x = x1; x <= x2; x++) { x0 = mx1 + x * xs; int smpl = culcPoint(x0, y0, max); if (smpl < max){ g.setColor(new Color(128+smpl*20,smpl*20,255-smpl*20)); g.drawLine(x,y2,x,y2); } else { g.setColor(Color.black); g.drawLine(x,y2,x,y2); } if (smpl != s) { cracked = true; } } if (cracked) { newWidth = r.width / 2; newHeight = r.height / 2; nextList[q] = new Rectangle(r.x+1, r.y+1, newWidth-1, newHeight-1); q+=1; nextList[q] = new Rectangle(r.x+1, r.y+newHeight+1,newWidth-1, r.height-newHeight-2); q+=1; nextList[q] = new Rectangle(r.x+newWidth+1, r.y+1, r.width-newWidth-2, newHeight-1); q+=1; nextList[q] = new Rectangle(r.x+newWidth+1, r.y+newHeight+1,r.width-newWidth-2, r.height-newHeight-2); q+=1; } else { if (s < max){ g.setColor(new Color(128+s*20,s*20,255-s*20)); g.fillRect(r.x, r.y, r.width, r.height); } else { g.setColor(Color.black); g.fillRect(r.x, r.y, r.width, r.height); } } } rectList = new Rectangle[q]; for(int i = 0;i < q; i++){ rectList[i] = nextList[i]; } }while(q != 0); repaint(); } }