media @ VU
[] readme course(s) preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthought(s) appendix reference(s) example(s) resource(s) _

talk show tell print

applet-math-fractal-logistic.jva

applet-math-fractal-logistic.jva / applet-math-fractal-logistic


  // Colored bifurcations map,  Evgeny Demidov,  18 Nov 2005
  
  import java.awt.*;
  import java.awt.image.*;
  import java.awt.event.*;
  import java.util.StringTokenizer;
  
  public class applet-math-fractal-logistic extends java.applet.Applet implements MouseListener {
  Image img;              IndexColorModel iColor;
  int MaxIt = 256, w,h, w2,wh, pixArr[],  maxColor = 20;
  double maxIZI2=4., Ymid=.0, Xmid=-.5, DelX=3., Ratio=1,  Xm, Ym;
  boolean   showXY = false;
  TextField tfXY;
  
  public void init() {
    w = getSize().width;    h = getSize().height;
    w2 = w/2;  wh = w*h;
    pixArr = new int[wh];
    String s=getParameter("XYmidDelC"); if (s != null) {
      StringTokenizer st = new StringTokenizer(s);
      Xmid = Double.valueOf(st.nextToken()).doubleValue();
      Ymid = Double.valueOf(st.nextToken()).doubleValue();
      DelX = Double.valueOf(st.nextToken()).doubleValue();
      Ratio = Double.valueOf(st.nextToken()).doubleValue();}
    s=getParameter("MaxIt"); if (s != null) MaxIt=Integer.parseInt(s);
    s=getParameter("MaxColor"); if (s != null) maxColor=Integer.parseInt(s);
    byte rColor[] = new byte[maxColor+2], bColor[] = new byte[maxColor+2],
         gColor[] = new byte[maxColor+2];
    int M=maxColor/2;
    long M4 = (long)M*M*M*M;
    for (int i = 0; i < M; i++){
      long dum = i;  dum *=dum;  dum *=dum;
      bColor[i] = gColor[M+i] = gColor[M-i] = rColor[maxColor-1-i] =
       (byte)(255 - (255*dum)/M4);}
    rColor[0] = gColor[0] = bColor[0] = (byte)255;
    iColor = new IndexColorModel( 8, maxColor+2, rColor,gColor,bColor);
    s=getParameter("showXY");
    if ( (s != null) && (s.equalsIgnoreCase("Y")) ) {
      showXY = true;
      setLayout(new BorderLayout());
      s = ""+Xmid+", "+Ymid+"; "+(float)DelX+", "+(float)(DelX/Ratio);
      tfXY = new TextField( s );
      add("South",  tfXY);
      h -= tfXY.getPreferredSize().height;}
    addMouseListener(this);
    draw();
  }
  
  public void destroy() {  removeMouseListener(this); }
  public void mouseClicked(MouseEvent e) {}      //1.1 event handling
  public void mousePressed(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent  e) {}
  
  public void mouseReleased(MouseEvent e) {
    Xmid = Xmid+(e.getX()-w2)*DelX/w;
    Ymid = Ymid-(e.getY()-h/2)*DelX/(w*Ratio);
    if ( e.isControlDown() ) {
      if ( e.isShiftDown() )  Ratio /= 2.;
      else DelX *= 2.;}
    else {
      if ( e.isShiftDown() )  Ratio *= 2.;
      else DelX /= 2.;}
    if (showXY) tfXY.setText( ""+Xmid+", "+Ymid+"; "+
      (float)DelX +", "+(float)(DelX/Ratio));
    draw();
    repaint();
  }
  
  public void draw() {
    int iy,i, maxPix=0, norm;
    double Xo,C, StX=DelX/w, StY=StX/Ratio;
    for (i=0; i < wh; i++) pixArr[i] = 0;
    for (iy=0, C=Ymid+StY*h/2; iy < h; iy++, C-=StY) {
        double X=.5;   int n=0, offset = w*iy;
        do  {
          int nX = (int)((X - Xmid)/StX) + w2;
          if ( (nX >= 0) && (nX < w)) pixArr[nX + offset] += 1;
          X=C*X*(1-X);  n++;
        } while (n < MaxIt);
    }
    int mCol = maxColor-1;
    for (i=0; i < wh; i++) if (pixArr[i] > mCol) pixArr[i] = mCol;
    img = createImage(new MemoryImageSource(w, h, iColor, pixArr, 0, w));
  }
  
  public void paint(Graphics g) {
    g.drawImage(img, 0, 0, this);
    showStatus( "X=" + (float)Xmid + "  C=" + (float)Ymid +
      "  dX=" + (float)DelX );
  }
  
  }
  


(C) A. Eliëns 2/9/2007

You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.