Applet Code -- Slope Fields



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

public class SlopeField extends Applet
{
      Graphics      sG;
      Graphics      bG;
      Image         bI;
      Graphics      cG;
      Image         cI;

      Color         back         = new Color(247, 243, 234);
      Color         lines        = new Color(128, 128, 255);
      Color         black        = new Color(0, 0, 0);
      Color         blue         = new Color(0, 0, 255);
      Color         green        = new Color(0, 255, 0);
      Color         red          = new Color(255, 0, 0);

      double        dx, dy, mag, slope;
      int           idx, idy;

      public double foo(double x, double y)
      {
             double value;
             value = 0.25 * (y - 1.0) * (5.0 - y);
             return value;
      }

      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();
             bG.setColor(back);
             bG.fillRect(0, 0, 221, 221);
             bG.setColor(lines);
             for (int i = 10; i < 211; i = i + 25)
             {
                 bG.drawLine(10, i, 210, i);
                 bG.drawLine(i, 10, i, 210);
             }
             bG.setColor(blue);
             bG.drawLine(10, yscale(0.0), 210, yscale(0.0));
             bG.drawLine(10, yscale(0.0) + 1, 210, yscale(0.0) + 1);
             bG.drawLine(10, yscale(0.0) - 1, 210, yscale(0.0) - 1);
             bG.drawLine(xscale(0.0), 10, xscale(0.0), 210);
             bG.drawLine(xscale(0.0) + 1, 10, xscale(0.0) + 1, 210);
             bG.drawLine(xscale(0.0) - 1, 10, xscale(0.0) - 1, 210);
             bG.setColor(black);
             for (double x = -1.0; x < 7.1; x = x + 1.0)
             {
                 for (double y = -1.0; y < 7.1; y = y + 1.0)
                 {
                     bG.fillOval(xscale(x) - 2, yscale(y) - 2, 5, 5);
                     slope = foo(x, y);
                     mag = java.lang.Math.sqrt(1 + slope * slope);
                     dx = 10.0 / mag;
                     dy = 10.0 * slope / mag;
                     idx = (int) java.lang.Math.round(dx);
                     idy = (int) -java.lang.Math.round(dy);
                     bG.drawLine(xscale(x) - idx, yscale(y) - idy,
                                 xscale(x), yscale(y));
                     bG.drawLine(xscale(x), yscale(y),
                                 xscale(x) + idx, yscale(y) + idy);
                 }
             }
             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 yscale(double y)
      {
             int iy;
             iy = (int) java.lang.Math.round(185.0 - y * 25.0);
             return iy;
      }
      public int xscale(double x)
      {
             int ix;
             ix = (int) java.lang.Math.round(25.0 * x + 35.0);
             return ix;
      }

      public boolean mouseDown(Event evt, int mx, int my)
      {
             double    x, y, px, py;
             int       ix, iy, ipx, ipy;

             if ((mx > 9) && (mx < 211) && (my > 9) && (my < 211))
             {
                  cG.drawImage(bI, 0, 0, this);
                  cG.setColor(black);
                  cG.fillOval(mx  - 3, my - 3, 7, 7);
                  sG.drawImage(cI, 0, 0, this);
                  x = (mx - 35)/25.0;
                  y = (185 - my)/25.0;
                  py = y;
                  for (px = x + 0.1; px < 8.05; px = px + 0.1)
                  {
                      py = py + 0.1 * foo(x, y);
                      cG.setColor(red);
                      ix = xscale(x);
                      iy = yscale(y);
                      ipx = xscale(px);
                      ipy = yscale(py);
                      for (int qx = -1; qx < 2; qx = qx + 1)
                      {
                          for (int qy = -1; qy < 2; qy = qy + 1)
                          {
                              cG.drawLine(ix + qx, iy + qy, ipx + qx, ipy + qy);
                          }
                      }
                      x = px;
                      y = py;
                      cG.setColor(black);
                      cG.fillOval(mx  - 2, my - 2, 5, 5);
                      sG.drawImage(cI, 0, 0, this);
                      if (py >  7.0) px = 9.0;
                      if (py < -1.0) px = 9.0;
                  }
             }
             return true;
      }

}


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