Sigmoid Generator Applet

Generates either:

Which type of sigmoid is produced is determined by the content of the 'param' tag within the 'applet' tag used to invoke the applet.

The code of this applet could easily be extended to include the generation of the look-up table previously done by the 'C' program described in the text.

/**
  * Sigmoid Function Generator
  * @author Robert J Morton
  * @version 15 December 1997
*/

import java.awt.*;

public class Sigmoid extends java.applet.Applet implements Runnable
{
   boolean finished = false;          //indicates when the curve has been completed
   double k = .025;                   //non-linearity constant
   double y = k / 4;                  //output value
   double x = 0;                      //input value
   double dx = k / 10;                //plotting (or look-up table) increment
   int s = 200;                       //pixels per unit real (scaling factor)
   int H = 0, V = s - 1;              //window co-ordinate of graphical origin
   int X, Y;                          //dimensions (in pixels) of the applet window
   Image I = null;                    //reference for an off-screen image I
   Graphics i = null;                 //graphics context for the off-screen image
   long TF = 50;                      //total Time Frame for plotting update cycle
   long T;                            //time at which the next new cycle is due to begin
   Thread plotting;                   //declare a thread reference variable
   Font font;                         //and a font reference for annotations
   Color tr;                          //trace colour
   boolean TypeFlag = false;          //set for unipolar presentation

   public void init() {                           //INITIALISE THE APPLET
      int Xy, Yx;                                 //co-ords of axis labels
      tr = new Color( 0, 128, 64);                //create special dark green for trace
      font = new Font("Dialog", Font.PLAIN, 12);  //choose the lettering font for this applet
      Dimension d = size();                       //get size of window as given in HTML applet tag
      X = d.width; Y = d.height;                  //establish window width and height
      I = createImage(X, Y);                      //create the off-screen image I
      i = I.getGraphics();                        //graphics context reference for off-screen image
      if(getParameter("type").equals("bipolar")){ //if applet parameter 'type' = 'bipolar'
         TypeFlag = true;                         //set the TypeFlag true;
         k = .011;                                //non-linearity constant
         y = 0;                                   //output value
         x = 0;                                   //input value
         dx = .0025;                              //plotting (or look-up table) increment
         s = 100;                                 //pixels per unit real (scaling factor)
         H = 100; V = 100;                        //window co-ordinate of graphical origin
         Xy = V + 10; Yx = H - 10;                //co-ords of axis letters
      } else {                                    //if doing a unipolar graph
         Xy = V - 5; Yx = H + 5;                  //co-ords of axis letters
      }
      i.setColor(Color.lightGray);                //set background colour for the off-screen image
      i.fillRect(0, 0, X, Y);                     //paint background of off-screen image
      i.setFont(font);                            //set up the annotation font
      i.setColor(Color.gray);                     //set colour for drawing the graph axes
      i.drawLine(0, V, X, V);                     //draw x axis
      i.drawLine(H, 0, H, Y);                     //draw y axis
      i.setColor(Color.black);                    //set colour for lettering
      i.drawString("X", X - 10, Xy);           
      i.drawString("Y", Yx, 10);                  //print the X and Y axis letters
      i.setColor(tr);                             //set colour to paint the trace on image
      T = System.currentTimeMillis() + TF;        //set end time for current update time frame
   }

   public void paint(Graphics g) {         //set up the graphics
      g.drawImage(I, 0, 0, null);          //(re)draw from the off-screen image I
   }

   public void update(Graphics g) {        //PLOT THE SIGMOID GRAPH
      if(x > 1 || y > 1)                   //if plot has reached edge of graph
         finished = true;                  //set the 'finished' flag
      else {                               //if not yet finished plotting graph
         int h = (int)(s * x);             //convert horizontal plot to pixels
         int v = (int)(s * y);             //convert vertical plot to pixels
         g.setColor(tr);                   //set colour to paint the trace on screen
         int a = h, b, c = V - v, d;       //simplify pixel co-ordinates
         if(TypeFlag) {
            a = H - h; b = H + h;          //simplify pixel co-ordinates
            c = V + v; d = V - v;
            g.drawLine(b, d, b, d);        //do next plot in lower left quadrant
            i.drawLine(b, d, b, d);        //do next plot in lower left quadrant
            y += k * (1 - y);              //advance the output difference equation
         } else  
            y += k * y * (1 - y);          //advance the output difference equation
         x += dx;                          //advance the input value
         g.drawLine(a, c, a, c);           //do next plot in upper right quadrant
         i.drawLine(a, c, a, c);           //do next plot in upper right quadrant
      }
   }

   public void run() {                            //run the plotting thread
      while(true) {                               //permanent loop broken by external event
         if(!finished) repaint();                 //if not yet finished, do next plot
         long s = T - System.currentTimeMillis(); //get time remaining in this cycle's time frame
         if (s < 5) s = 5;                        //in case host PC is too slow for ideal trace speed
         try {Thread.currentThread().sleep(s);    //sleep for remaining time
         } catch (InterruptedException e) {       //allow browser events to break the thread
         }                                        //happens if you return to applet's HTML page
         T = System.currentTimeMillis() + TF;     //set finish time of next time frame
      }
   }

   public void start() {                          //Start program thread by
      plotting = new Thread(this);                //creating the thread object
      plotting.start();                           //and starting it running
   }                                              //[returns a call to run()]

   public void stop() {plotting.stop();}          //Stop program thread

/*  Robert J Morton, the author of this program, 
    is a poor but Right Honourable Fellow of the
    Ancient and Noble Order of the Long-term Unemployed.

    Offers of work please to: robmorton@clara.net  */
}

This page's parent within this Web Site. About this Web Site. Its home page. Email its Author.