Divergence Applet



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

public class Divergence extends Applet
{
     Panel         controls1 = new Panel();
     Panel         controls2 = new Panel();
 
     private       TextField FunctionText  = new TextField("sin(pi * x) + x/2", 45);
     private       TextField MinText       = new TextField("-1", 15);
     private       TextField MaxText       = new TextField("1", 15);
 
     Expr          equation;

     Variable      x     = Variable.make("x");
     Variable      Picon = Variable.make("Pi");
     Variable      PIcon = Variable.make("PI");
     Variable      picon = Variable.make("pi");
     Variable      Econ  = Variable.make("E");
     Variable      econ  = Variable.make("e");

     String        fcn   = "x^3 - x";
     String        UserInput;

     Graphics      sG;
     Graphics      bG;
     Image         bI;
     Graphics      dG;
     Image         dI;

     Color         black        = new Color(0, 0, 0);
     Color         back         = new Color(247, 243, 234);
     Color         lines        = new Color(128, 128, 255);
     Color         white        = new Color(255, 255, 255);
     Color         rhocolor     = new Color(128, 64, 0);
     Color         barcolor     = new Color(255, 160, 160);


     double        xmin = -5.0;
     double        xmax =  5.0;
     double        ymin =  0.0;
     double        ymax =  0.0;
     double        dy = 0.0;
     double        xx;
     double        dx;
     double        yy, y1, y2;
     int           iy1, iy2;

     double[]      foo  = new double[81];
     double[]      rho  = new double[239];
     double[]      drho = new double[239];

     double        rhomax;
     double        h;
     double        n = 50;

     public void init()
     {
             setBackground(back);
             bI = createImage(this.size().width, this.size().height);
             bG = bI.getGraphics();
             dI = createImage(this.size().width, this.size().height);
             dG = dI.getGraphics();
             
             drawback();

             picon.set_value(Math.PI);
             Picon.set_value(Math.PI);
             PIcon.set_value(Math.PI);
             econ.set_value(Math.E);   
             Econ.set_value(Math.E);

             controls1.setBackground(back);
             controls1.setFont(new Font("Courier", Font.BOLD, 12));
             controls1.add(new Label(" Enter P(x) at right."));     
             controls1.add(FunctionText);
             controls2.setBackground(back);
             controls2.setFont(new Font("Courier", Font.BOLD, 12));
             controls2.add(new Label(" Range of x -- Minimum x"));
             controls2.add(MinText);
             controls2.add(new Label(" Maximum x"));
             controls2.add(MaxText);
             add(controls1);
             add(controls2);
     }

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

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

     public void drawback()
     {
            bG.setColor(black);
            bG.fillRect(0, 0, 601, 251);
            bG.setColor(back);
            bG.fillRect(2, 2, 597, 247);
            bG.setColor(lines);
            for (int i = 10; i < 251; i = i + 24)
            {
                 bG.drawLine(i, 5, i, 245);
            }
            for (int i = 5; i < 246; i = i + 24)
            {
                 bG.drawLine(10, i, 250, i);
            }
            bG.drawLine(10, 124, 250, 124);
            bG.drawLine(10, 126, 250, 126);
            bG.drawLine(129, 5, 129, 245);
            bG.drawLine(131, 5, 131, 245);
            dG.drawImage(bI, 0, 0, this);
     }

     public boolean mouseDown(Event evt, int mx, int my)
     {
           if (my > 50)
           {
               drawBlueGraph();
           }

           if ((my > 50) && (mx > 300))
           {
               drawRedGraph();
           }

           return true;
     }

     public void drawRedGraph()
     {
              dx = (xmax - xmin)/240.0;
              xx = xmin;
              rhomax = 0.0;
              for (int i = 0; i < 239; i = i + 1)
              {
                  xx = xx + dx;
                  x.set_value(xx - dx/2.0);
                  y1 = equation.value();
                  x.set_value(xx + dx/2.0);
                  y2 = equation.value();
                  rho[i] = 0.0;
                  drho[i] = -(y2 - y1)/dx;
                  rhomax = java.lang.Math.max(rhomax,
                           java.lang.Math.abs(drho[i]));
              }

              h = 0.0;

              if (rhomax > 0)
              {
                  h = 1.0/(rhomax * n);
              }

              for (int j = 0; j <=n ; j = j + 1)
              {
                  dG.drawImage(bI, 0, 0, this);

                  dG.setColor(barcolor);
                  for (int i = 0; i < 239; i = i + 1)
                  {
                      yy = 245.0 - 240.0 * (rho[i] + 1.0) / 2.0;
                      iy1 = (int) java.lang.Math.round(yy);
                      dG.drawLine(351 + i, iy1, 351 + i, 245);
                  }
       
                  drawBrownGrid(); 

                  for (int i = 0; i < 239; i = i + 1)
                  {
                      rho[i] = rho[i] + h * drho[i];
                  }
               }
     }

     public void drawBrownGrid()
     {
                  dG.setColor(rhocolor);
                  for (int i = 350; i < 591; i = i + 24)
                  {
                      dG.drawLine(i, 5, i, 245);
                  }
                  for (int i = 5; i < 246; i = i + 24)
                  {
                      dG.drawLine(350, i, 590, i);
                  }
                  dG.drawLine(350, 124, 590, 124);
                  dG.drawLine(350, 126, 590, 126);
                  dG.drawLine(469, 5, 469, 245);
                  dG.drawLine(471, 5, 471, 245);

                  sG.drawImage(dI, 0, 80, this);
     }

     public void drawBlueGraph()
     {
           {  
              try
              {
                    equation = Parser.parse(FunctionText.getText());
              }
              catch (Syntax_error e)
              {
                    System.err.println ("Syntax error: " + e);
              }

              UserInput = MinText.getText().trim();
              xmin = Double.valueOf(UserInput).doubleValue();
              UserInput = MaxText.getText().trim();
              xmax = Double.valueOf(UserInput).doubleValue();

              dx = (xmax - xmin)/80.0;

              x.set_value(xmin);
              ymin = equation.value();
              ymax = ymin;
              foo[0] = ymin;

              xx = xmin;
              for(int i = 1; i < 81; i = i + 1)
              {
                    xx = xx + dx;
                    x.set_value(xx);
                    foo[i] = equation.value();
                    ymin = java.lang.Math.min(ymin, foo[i]);
                    ymax = java.lang.Math.max(ymax, foo[i]);
              }

              dy = ymax - ymin;

              if (ymin == ymax)
              {
                 dy = 2;
                 ymin = ymin - 1;
                 ymax = ymax + 1;
              }

              drawback();
              bG.setColor(black);
              for(int i = 0; i < 80; i = i + 1)
              {
                    yy = 245.0 - 240.0 * (foo[i] - ymin) / dy;
                    iy1 = (int) java.lang.Math.round(yy);     
                    yy = 245.0 - 240.0 * (foo[i + 1] - ymin) / dy;
                    iy2 = (int) java.lang.Math.round(yy);
                    bG.drawLine(3 * i + 10, iy1, 3 * i + 13, iy2);
                    dG.drawImage(bI, 0, 0, this);
                    sG.drawImage(dI, 0, 80, this);      
              }

           }

           drawBrownGrid();

     }

}  


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