/** * Sigmoid Function Generator * @author Robert John Morton UK-YE572246C * @version 15 December 1997 */ import java.awt.*; public class sigmoid2 extends java.applet.Applet implements Runnable { private boolean finished = false, // indicates when the curve has been completed firstpass = true; // first pass flag private double k = 0.025, // non-linearity constant x = 0, // input value y = k / 4, // output value dx = k / 10; // plotting (or look-up table) increment private int s = 200, // pixels per unit real (scaling factor) H = 0, // horizontal window co-ordinate of graphical origin V = s - 1, // vertical window co-ordinate of graphical origin X = 200, Y = 200, // dimensions (in pixels) of the applet window ph = 0, pv = 200, // horiz and vert pixel positions of previous pass mx = 14, my = 15; // x and y margins for the applet private Image I = null; // reference for an off-screen image I private Graphics i = null; // graphics context for the off-screen image private long TF = 100, // total Time Frame for plotting update cycle T; // time at which next new cycle is due to begin private volatile Thread TH; // thread reference variable // lettering font for applet private Font font = new Font("Dialog",Font.PLAIN,12); private Color bg = new Color(0,0,0), // background colour tr = new Color(0,255,128); // trace colour public void init(){ // INITIALISE THE APPLET setBackground(bg); // set applet's background colour // Dimension d = getSize(); // get size of window from HTML 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 for off-screen image // For the off-screen image, paint image in background colour i.setColor(bg); i.fillRect(0,0,X,Y); // Set colour for the graph axes and draw them. i.setColor(Color.white); i.drawLine(0, V, X, V); i.drawLine(H, 0, H, Y); int Xy = V - 5, // co-ords of axis letters Yx = H + 5; i.setFont(font); // set up the annotation font i.drawString("X", X - 10, Xy); // print the X and Y axis letters i.drawString("Y", Yx, 10); i.setColor(tr); // set colour to paint the trace on the image // Set thesystem time at which the first time frame will terminate. T = System.currentTimeMillis() + TF; } /* Set up the graphics by re-drawing from the off-screen image I in the event that the applet becomes un-eclipsed. */ public void paint(Graphics g){ g.drawImage(I, mx, my, null); } /* Set up the graphics by re-drawing from the off-screen image I whenever repaint() is called. */ public void update(Graphics g){ g.drawImage(I,mx,my,null); } public void newplot() { // PLOT THE SIGMOID GRAPH if(x > 1 || y > 1) // if plot has reached edge of graph, finished=true; // set the 'finished' flag else { // else plot has not reached edge of graph int h = (int)(s * x), // convert horizontal plot to pixels v = V - (int)(s * y); // convert vertical plot to pixels if(firstpass || h > ph) { /* Set the colour to paint the trace on the off-screen image and do next plot on the off-screen image. */ i.drawLine(ph,pv,h,v); ph = h; pv = v; // save pixel positions ready for next pass } y += k * y * (1 - y); // advance the output difference equation x += dx; // advance the input value } } public void run() { // run the plotting thread while(TH != null){ // loop while thread is alive newplot(); if(!finished) // if not finished, do next plot repaint(); /* Get the amount of time remaining in the current time frame, imposing a minumum limit of 5 milliseconds, then put thread to sleep for this remaining amount of time. */ long s = T - System.currentTimeMillis(); if (s < 5) s = 5; try { TH.sleep(s); } /* Catch any events that may occur during thread's sleep but do nothing about them because they are normal expected external events. Afterwards, set the system time at which the next time frame is due to terminate. */ catch (InterruptedException e) { } T = System.currentTimeMillis() + TF; } } /* Start the program thread by creating the thread object and then starting it running [returns a call to run()]. */ public void start() { TH = new Thread(this); TH.start(); } public void stop() {TH = null;} //Stop program thread }