import java.awt.*;
import java.applet.*;
import java.lang.*;

public class Dynamic extends Applet
{
      Graphics      sG;

      Graphics      bG;
      Image         bI;

      Graphics      cG;
      Image         cI;

      Color         back        = new Color(192, 255, 192);
      Color         lines       = new Color(0, 255, 0);
      Color         black       = new Color(0, 0, 0);
      Color         blue        = new Color(255, 0, 0);

      double        x, nx, oldx, newx; 
      double        startx      = 1.0;
      double        y, ny, oldy, newy;
      double        starty      = 0.0;
      double        mag, nmag;

      int           sw = 0;

      public void init()
      {
             bI = createImage(this.size().width, this.size().height);
             bG = bI.getGraphics();
             cI = createImage(this.size().width, this.size().height);
             cG = cI.getGraphics();
             drawback();
             cG.drawImage(bI, 0, 0, this);
      }

      public void paint(Graphics g)
      {
             g.drawImage(cI, 0, 0, this);
             sG = getGraphics();
      }

      public void update(Graphics g)
      {
             g.drawImage(cI, 0, 0, this);
      }

      public int xscale(double x)
      {
             int z;
             z = (int) java.lang.Math.round(100 + 50 * x);
             return z;
      }

      public int yscale(double x)
      {
             int z;
             z = (int) java.lang.Math.round(200 - (100 + 50 * x));
             return z;
      }

      public void drawback()
      {
             bG.setColor(back);
             bG.fillRect(0, 0, 201, 201);
             bG.setColor(lines);
             for (int i = 0; i < 201; i = i + 20)
             {
                 bG.drawLine(0, i, 200, i);
                 bG.drawLine(i, 0, i, 200);
             }
             bG.setColor(black);
             bG.drawOval(50, 50, 100, 100);
      }

      public void simulate()
      {
             bG.setColor(black);
             bG.fillOval(xscale(startx) - 2, yscale(starty) - 2, 5, 5);
             oldx = startx;
             oldy = starty;
             for(int i = 0; i < 3000; i = i + 1)
             {
                 mag = java.lang.Math.sqrt(oldx * oldx + oldy * oldy);
                 x = oldx/mag;
                 y = oldy/mag;
                 nx = x + 0.02 * y;
                 ny = y - 0.02 * x;
                 nmag = java.lang.Math.sqrt(nx * nx + ny * ny);
                 nx = nx/nmag;
                 ny = ny/nmag;
                 nmag = 1.0;
                 if (mag > 1.0)
                 {
                     nmag = 0.9995 * mag;
                 }
                 if (mag < 1.0)
                 {
                     nmag = 1.0005 * mag;
                 }
                 newx = nmag * nx;
                 newy = nmag * ny;
                 bG.setColor(blue);
                 bG.drawLine(xscale(oldx), yscale(oldy),
                             xscale(newx), yscale(newy));
                 bG.setColor(black);
                 bG.drawOval(50, 50, 100, 100);
                 oldx = newx;
                 oldy = newy;
                 cG.drawImage(bI, 0, 0, this);
                 cG.setColor(blue);
                 cG.fillOval(xscale(oldx) - 2, yscale(oldy) - 2, 5, 5);
                 sG.drawImage(cI, 0,0, this);
             }
      }

       public boolean mouseDown(Event evt, int mx, int my)
       {
             if (sw == 0)
             {
                 sw = 1;
                 simulate();
                 return true;
             }
             startx = (mx - 100)/50.0;
             starty = -(my - 100)/50.0;
             if (startx * startx + starty + starty == 0.0)
             {
                 startx = 0.02;
             }
             drawback();
             simulate();
             return true;
       }

}



Copyright c 1997 by Frank Wattenberg, Department of Mathematics, Montana State University, Bozeman, MT 59717