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-stdmap-1.jva

applet-math-fractal-stdmap-1.jva / applet-math-fractal-stdmap-1


  // The Standard map 1,   Evgeny Demidov,  26 Aug 2003
  import java.awt.*;
  import java.awt.event.*;
  import java.awt.image.*;
  import java.util.StringTokenizer;
  public class applet-math-fractal-stdmap-1 extends java.applet.Applet implements MouseListener,
    KeyListener, ActionListener {
    int w,h,w2,h2, it=3000, rand=50;
    double Xo=3, Po=-4, K=.3,  Pi2 = Math.PI*2,
      Xc=Math.PI, Yc=0, dX=Pi2, Mx, My;
    Image buffImage; Graphics buffGraphics;
    boolean   bClear = true;
    Label     lbK, lbIt;
    TextField tfK, tfXY, tfIt, tfRand;
    Button    btClear, btRand;
  
  public void init() {
    double  dx = Pi2,  dy = 4.;
    w = getSize().width;   h = getSize().height;
    w2 = w/2;  h2 = h/2;
    buffImage = createImage(w, h);
    buffGraphics = buffImage.getGraphics();
    String s=getParameter("K");
    if (s != null) K = Double.valueOf(s).doubleValue();
    s=getParameter("it");  if (s != null) it = Integer.parseInt(s);
    s=getParameter("rand"); if (s != null) rand = Integer.parseInt(s);
    s=getParameter("coord");  if (s != null){
      StringTokenizer st = new StringTokenizer(s);
      Xc = Double.valueOf(st.nextToken()).doubleValue();
      Yc = Double.valueOf(st.nextToken()).doubleValue();
      dx = Double.valueOf(st.nextToken()).doubleValue();
      dy = Double.valueOf(st.nextToken()).doubleValue();}
    Mx = dx/w;  My = dy/h;
    this.setLayout( new FlowLayout(FlowLayout.LEFT, 0, 0) );
    tfXY = new TextField( ""+(float)Xc+" "+(float)Yc+"; "+(float)dx+
      " "+(float)dy, 30);   add(tfXY);
    lbK = new Label("K", Label.RIGHT); add(lbK);
    tfK = new TextField( "" + K, 5);   add(tfK);
    tfK.addKeyListener(this);
    lbIt = new Label("it", Label.RIGHT); add(lbIt);
    tfIt = new TextField( "" + it, 5);   add(tfIt);
    tfIt.addKeyListener(this);
    btClear = new Button("Clear"); btClear.addActionListener(this); add(btClear);
    btRand = new Button("Rand");   btRand.addActionListener(this);  add(btRand);
    tfRand = new TextField( "" + rand, 3);   add(tfRand);
    tfRand.addKeyListener(this);
    addMouseListener(this);
    clear();
    random();
  }
  public void destroy() {
    removeMouseListener(this);
  }
  
  public void random(){
    buffGraphics.setColor(Color.black);
    for(int i = 0; i < rand; i++){
      Xo = Xc + Mx*w*(Math.random() - .5);
      Po = Yc + My*h*(Math.random() - .5);
      iterate();}
  }
  
  public void iterate() {
    double X=Xo, P=Po;
    for(int i = 0; i < it; i++){
      P = P + K*Math.sin(X);
      X = X + P;
      while(X < 0.) X += Pi2;
      while(X > Pi2) X -= Pi2;
      int ix  = w2 + (int)((X-Xc)/Mx), iy  = h2 - (int)((P-Yc)/My);
      buffGraphics.drawLine( ix,iy, ix,iy);
    }
    repaint();
  }
  
  public void mouseClicked(MouseEvent e){}       // event handling
  public void mousePressed(MouseEvent e) {
    int mx0 = e.getX() - w2,  my0 = h2 - e.getY();
    Xo = mx0*Mx + Xc;  Po = my0*My + Yc;
    if ( e.isAltDown() ){
      Xc = Xo;  Yc = Po;
      My /= 2.;  if ( !e.isShiftDown() ) Mx /= 2.;
      tfXY.setText(""+(float)Xc+" "+(float)Yc+"; "+(float)(w*Mx)+
        " "+(float)(h*My) );
      clear(); random();
      return;}
    if ( e.isControlDown() ){
      Xc = Xo;  Yc = Po;
      My *= 2.;  if ( !e.isShiftDown() ) Mx *= 2.;
      tfXY.setText(""+(float)Xc+" "+(float)Yc+"; "+(float)(w*Mx)+
        " "+(float)(h*My) );
      clear(); random();
      return;}
    buffGraphics.setColor(Color.red);
    iterate();
  }
  public void mouseReleased(MouseEvent e){}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e)  {}
  public void actionPerformed(ActionEvent e){
    if ( e.getActionCommand().equals("Clear") ){
      clear(); repaint();}
    else random();
  }
  public void keyTyped(KeyEvent e) {}
  public void keyPressed(KeyEvent e) {}
  public void keyReleased(KeyEvent e) {
    final int keyEnter = 10;
    if (e.getKeyCode() == keyEnter) {
      try{
        double Ko = K;
        K = Double.valueOf(tfK.getText()).doubleValue();
        it = Integer.parseInt(tfIt.getText());
        rand = Integer.parseInt(tfRand.getText());
        if (Ko != K){ clear(); random();}
      }catch ( NumberFormatException ne) {}
    }
    e.consume();
  }
  
  public void clear(){
    buffGraphics.setColor(Color.white);
    buffGraphics.fillRect(0, 0, w, h);
  }
  
  public void paint(Graphics g) {
    g.drawImage(buffImage, 0, 0, this);
    showStatus("X=" + (float)Xo + "  P=" + (float)Po );
  }
  public void update(Graphics g){ paint(g); }
  
  }
  


(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.