Applet Code -- Moving from One Point to Another


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

public class MovingPoint extends Applet
{
       Panel             tpanel = new Panel();
       private TextField tfield = new TextField("0.0000");

       String            ttext  = "0";

       double            t      = 0.0;

       Graphics          sG;
       Graphics          bG;
       Image             bI;

       Color             black        = new Color(0, 0, 0);
       Color             white        = new Color(255, 255, 255);
       Color             back         = new Color(247, 243, 234);
       Color             grid         = new Color(64, 255, 64);
       Color             red          = new Color(255, 64, 64);
       Color             blue         = new Color(96, 96, 255);
       Color             gray         = new Color(64, 64, 64);

       double            ax           = -1.0;
       double            ay           =  0.8;
       double            bx           =  1.0;
       double            by           = -1.0;

       int               width        = 200;
       int               wholewidth   = 201;
       int               top          =  40;

       int               px, py;

       public void init()
       {
              bI = createImage(wholewidth, wholewidth);
              bG = bI.getGraphics();
              drawback();

              setBackground(back);
              setFont(new Font("Courier", Font.BOLD, 12));
              tpanel.setBackground(back);
              tpanel.setFont(new Font("Courier", Font.BOLD, 12));
              tpanel.add(new Label(" t: "));
              tpanel.add(tfield);
              tpanel.add(new Button("Move"));
              add(tpanel);
       }

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

       public void update(Graphics g)
       {
              g.drawImage(bI, 0, top, this);
       }
 
       public void drawback()
       {
              bG.setColor(back);
              bG.fillRect(0, 0, wholewidth, wholewidth + top);
              bG.setColor(back);
              bG.setColor(grid);
              for (int i = 0; i < wholewidth + 1; i = i + 10)
              {
                  bG.drawLine(0, i, wholewidth, i);
                  bG.drawLine(i, 0, i, width);
              }
              bG.setColor(black);
              px = xscale((1.0 - t) * ax + t * bx);
              py = yscale((1.0 - t) * ay + t * by);
              bG.fillOval(px - 4, py - 4, 9, 9);
              bG.setColor(gray);
              bG.drawLine(xscale(ax), yscale(ay), xscale(bx), yscale(by));
              bG.setColor(red);
              bG.fillOval(xscale(ax) - 2, yscale(ay) - 2, 5, 5);
              bG.setColor(blue);
              bG.fillOval(xscale(bx) - 2, yscale(by) - 2, 5, 5);
              
       }

       public int xscale(double x)
       {
              int ix;
              ix = (int) Math.round(50.0 * (x + 2.0));
              return ix;
       }
 
       public int yscale(double y)
       {
              int iy;
              iy = (int) Math.round(50 * (y + 2.0));
               return iy;
       }

       public boolean mouseDown(Event evt, int mx, int my)
       {
              if (my > top)
              {
                  t = (mx - 50.0)/100.0;
                  tfield.setText(java.lang.Double.toString(t));
              }
              else
              {
                  ttext = tfield.getText().trim();
                  t = Double.valueOf(ttext).doubleValue();
              }
              drawback();
              sG.drawImage(bI, 0, top, this);
              return true;
       }

       public boolean action(Event evt, Object arg)
       {
              if (arg.equals("Move"))
              {
                  ttext = tfield.getText().trim();
                  t = Double.valueOf(ttext).doubleValue();
                  drawback();
                  sG.drawImage(bI, 0, top, this);
              }
              return true;
       }
       
}


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