// file:$ISIP_IFC/util/demo/isip_comm_client_speaker_verification/ConfidenceMonitor.java
// version: $Id: ConfidenceMonitor.java 10572 2006-06-26 21:54:34Z rei5 $

/*
 * @(#)ConfidenceMonitor.java	1.26 99/04/23
 *
 * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

// import necessary java libraries
//
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.Date;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

/**
 * The confidence monitor is used to display the confidence measure. A
 * value of zero means the speaker is an imposter. A value of 100
 * means is very confident that the user is an authorised speaker.
 */
public class ConfidenceMonitor extends JPanel {
    
    //-------------------------------------------------------------------------
    //
    // public members
    //
    //-------------------------------------------------------------------------
    public static JCheckBox dateStampCB = new JCheckBox("Output Date Stamp");
    public Surface surf;
    public JPanel controls;
    public boolean doControls;
    public JTextField tf;

    /**
     * the constructor of the class
     */  
    public ConfidenceMonitor() {
       
	// set the layout
	//
	setLayout(new BorderLayout());
        add(surf = new Surface());
        controls = new JPanel();
	Font font = new Font("serif", Font.PLAIN, 10);
        JLabel label = new JLabel("Sample Rate");
	
	// set the labels
	// 
        label.setFont(font);
        label.setForeground(Color.black);
        controls.add(label);
        tf = new JTextField("300");
	controls.add(tf);
        controls.add(label = new JLabel("ms"));
        label.setFont(font);
        label.setForeground(Color.black);
        controls.add(dateStampCB);
        dateStampCB.setFont(font);
        addMouseListener(new MouseAdapter() {
		public void mouseClicked(MouseEvent e) {
		    removeAll();
		    if ((doControls = !doControls)) {
			surf.stop();
			add(controls);
		    } else {
			try {
			    surf.sleepAmount = Long.parseLong(tf.getText().trim());
			} catch (Exception ex) {}
			surf.start();
			add(surf);
		    }
		    validate();
		    repaint();
		}
	    });
    }
    
   
    /**
     * a runnable class 
     */
    public class Surface extends JPanel implements Runnable {

	//-------------------------------------------------------------------------
	//
	// public members
	//
	//-------------------------------------------------------------------------
        public Thread thread;
        public int pts_all[];
        public long sleepAmount = 50;
	public int max_points = 0;
	public int MAX_POINTS = 50000;
	public int position = 0;
	public int pts_received = 0;
        
	//-------------------------------------------------------------------------
	//
	// private members
	//
	//-------------------------------------------------------------------------
    	private int w, h;
        private BufferedImage bimg;
        private Graphics2D big;
        private Font font = new Font("Times New Roman", Font.PLAIN, 11);
        private Runtime r = Runtime.getRuntime();
        private int columnInc;
        private int pts[];
        private int ptNum;
        private int ascent, descent;
        private float freeVolume, totalVolume;
        private Rectangle graphOutlineRect = new Rectangle();
        private Rectangle2D mfRect = new Rectangle2D.Float();
        private Rectangle2D muRect = new Rectangle2D.Float();
        private Line2D graphLine = new Line2D.Float();
        private Color graphColor = new Color(46, 139, 87);
        private Color mfColor = new Color(0, 100, 0);
        private String usedStr;
	private int flag = 0;


	// other public methods
	//
	Thread runner;
	
	// the refresh/update time 
	//
	int delayGlb= 0;
	int Mhe = 255;
	int Mwi = 150;
	int Hhe;
	int Dhe;
	int Ahe;
	double XMin = 0.0;                            
	double XMax =100 ; 
	double XNew = 0.0;
	double XOld; 
	
	// true : with header
	//
	boolean WHeader =true;
	
	// true: with digital part
	//
	boolean WDigital = true;
	
	// true: with analog part
	//
	boolean WAnalog = true;
	String Header = "CONFIDENCE";            
	String Unit = " % Match";
	String iStr; 
	boolean CHANGED;
	
	// Colors
	//
	Color HBGColor;     // Header Background 
	Color HFGColor;     // Header Foreground
	Color ABGColor;     // Analog Background
	Color AFGColor;     // Analog Foreground;
	Color DBGColor;     // Digital Background;
	Color DFGColor;     // Digital Foreground;
	Color ERRColor;     // Color if values are out of range
	int SCAColor;       // Color of scale (from scacolor to black)  
	
	/**
	 * the constructor for teh class
	 */    	
        public Surface() {
            setBackground(Color.black);
            addMouseListener(new MouseAdapter() {
		    public void mouseClicked(MouseEvent e) {
			if (thread == null) start(); else stop();
		    }
		});
	    pts_all = new int[MAX_POINTS];
	    for (int i= 0; i< MAX_POINTS; i++)
		pts_all[i] = 0;

	    HBGColor = Color.white;
	    ABGColor = Color.white;
	    DBGColor = Color.white;
	    
	    HFGColor = Color.black;
	    AFGColor = Color.black;
	    DFGColor = Color.black;
	    ERRColor = Color.black;
	    
	    SCAColor = 2;      // blue
	       
	    if (WHeader == true) {
		Hhe  = Mhe / 5;
	    } 
	    else 
		Hhe = 0;
	    if (WDigital == true) {
		Dhe  = Mhe / 100;
	    }
	    else 
		Dhe = 0;
	    if (WAnalog == true) {
		Ahe  = Mhe - Hhe - Dhe;
	    } 
	    else {
		Ahe = 0; 
		if (WDigital == true) {
		    if (WHeader == true) {
			Hhe = Mhe /3;
			Dhe = Mhe - Hhe;
		    }
		    else {
			Hhe = 0;
			Dhe = Mhe;
		    }
		} 
		else {    // only header (!!!)
		    Hhe = Mhe;
		}
	    }
	    XOld = XNew;
	    CHANGED = true;
        }

	/**
	 * get the minimum size
	 */    
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }
	
	/**
	 * get the maximum size
	 */    
        public Dimension getMaximumSize() {
            return getPreferredSize();
        }
	
	/**
	 * get the preferred size
	 */    
        public Dimension getPreferredSize() {
            return new Dimension(185,200);
        }
	
	/**
	 * the color of the monitor
	 *
	 * @param old color of the monitor
	 * @param s a string value
	 */    
	public Color WhichColor(Color old, String s) {
	    Color c = new Color(0,0,0);
	    c = old; 
	    String ds = s.toUpperCase();
	    if (ds.equals("WHITE") == true) { 
		c = Color.white;
	    }
	    else {
		if (ds.equals("BLACK") == true) {
			c =Color.black;
		    }
		else {
		    if (ds.equals("LIGHTGRAY") == true) {
				c = Color.lightGray;
			    }
			    else {
				if (ds.equals("GRAY") == true) {
					c = Color.gray;
				    }
				else {
				    if (ds.equals("DARKGRAY") == true) {
					c = Color.darkGray;
				    }
				    else {
					if (ds.equals("RED") == true) {
					    c = Color.red;
					}
					else {
					    if (ds.equals("GREEN") == true) {
						c = Color.green;
					    }
					    else {
						if (ds.equals("BLUE") == true) {
						    c = Color.blue;
						}
						else {
						    if (ds.equals("YELLOW") == true) {
							c = Color.yellow;
						    }
						    else {
							if (ds.equals("MAGENTA") == true) {
							    c = Color.magenta;
							}
							else {
							    if (ds.equals("CYAN") == true) {
								c = Color.cyan;
							    }
							    else {
								if (ds.equals("PINK") == true) {
								    c = Color.pink;
								}
								else {
								    if (ds.equals("ORANGE") == true) {
									c = Color.orange;
								    }
								}    
							    } 
							}
						    }  
						}
					    }
					} 
				    }
				}
			    }   
		}
	    } 
	    return c;
	} // END of WhichCoor
	
	/**
	 * check the color
	 */    
	public int CheckSCAColor(int old, String s) {
	    int c = old;
	    String ds = s.toUpperCase();
	    if (ds.equals("RED") == true){
		c = 1;
	    }
	    else {
		if (ds.equals("GREEN") == true) {
		    c = 2;
		}
		else { 
		    if (ds.equals("BLUE") == true) {
			c = 3;
		    }
		}
	    } 
	    return c;
	}  // End of CheckSCAColor
	
	
	/**
	 * set the color
	 */    
	public void SetColors(String hbgcolor, 
			      String hfgcolor,
			      String abgcolor,
			      String afgcolor,
			      String dbgcolor,
			      String dfgcolor,
			      String errcolor,
			      String scacolor) {
	    if (hbgcolor.length() != 0) 
		HBGColor = WhichColor(HBGColor,hbgcolor);
	    if (hfgcolor.length() != 0)  
		HFGColor =  WhichColor(HFGColor,hfgcolor);
	    if (abgcolor.length() != 0) 
		ABGColor = WhichColor(ABGColor,abgcolor);
	    if (afgcolor.length() != 0)  
		AFGColor = WhichColor(AFGColor,afgcolor);
	    if (dbgcolor.length() != 0) 
		DBGColor = WhichColor(DBGColor,dbgcolor);
	    if (dfgcolor.length() != 0)  
		DFGColor = WhichColor(DFGColor,dfgcolor);
	    if (errcolor.length() != 0)  
		ERRColor = WhichColor(ERRColor,errcolor);
	    if (scacolor.length() != 0) 
		SCAColor = CheckSCAColor(SCAColor,scacolor);
	    repaint();    
	    
	} // End of SETCOLORS
	
	/**
	 * the paint method
	 */    
        public void paint(Graphics g) {
	    
	    Font f;
	    FontMetrics fm;
	    String s;
	    int i;
	    int x,y;
	    double XN;
	    int Fhe,fhe;
	    int xc,yc,r1,r2,lz;
	    int x1,y1,x2,y2,dy;
	    int x0,y0,re,gr,bl,w1,c1,c2,c3;
	    int fx,fy,lx,ly;
	    double dx;
	    double xx;
	    double VAlpha;
	    double VAlphaRad; 
	    boolean OutOfRange;
     
	    /* ============================= FRAMES =========================== */
            if (big == null) {
                return;
            }

	    big.setBackground(Color.white);
	    big.clearRect(0,0,w,h);

	    // draw the main frame 
	    //
	    big.setColor(Color.white);
	    big.drawRect(0,0,Mwi,Mhe);

	    // draw the  frame around the header
	    //
	    if  (WHeader == true) {  
		big.setColor(HBGColor);
		big.fillRect(0,0,Mwi-1,Hhe-1);
		big.setColor(Color.black);
		big.drawRect(0,0,Mwi-1,Hhe-1);
	    }
	    // draw the frame around the anal. instr.
	    //
	    if (WAnalog == true) {  
		big.setColor(ABGColor);
		big.fillRect(0,Hhe,Mwi-1,Ahe-1);
		big.setColor(Color.black);
		big.drawRect(0,Hhe,Mwi-1,Ahe-1);
		}
	    // draw the frame around the dig. instr.
	    //
	    if (WDigital == true) { 
		big.setColor(DBGColor);
		big.fillRect(0,Hhe+Ahe,Mwi-1,Dhe-1);
		big.setColor(Color.black);
		big.drawRect(0,Hhe+Ahe,Mwi-1,Dhe-1);
		} 
  
	    /*===================== HEADER ===========================*/ 
	    // HEADER
	    if (WHeader == true) {  
		Fhe = 21;  // Calculate the font
		do { 
		    Fhe--; 
		    f = new Font("Ariel",Font.BOLD,Fhe);
		    big.setFont(f);
		    fm  = getFontMetrics(f);
		    fhe = fm.getHeight();
		} 
		while (
		       (fhe+2)>Hhe);
 
		// print the header       
		//
		x = (Mwi/2) - (fm.stringWidth(Header)/2);
		y = (Hhe/2) + (fhe/2) - 2;
		if (x > 0) {
		    big.setColor(HFGColor);
		    big.drawString(Header, x, y);
		}
		
	    } // End of HEADER  
	    
	    /* ============================== ANALOG - SPEEDO ================ */
	    // Speedometer
	    if (WAnalog == true) {     
		fx = 0; 
		fy = 0;
		lx = 0;
		ly = 0;   
		
		// Calculate the center and r1,r2
		//
		if (Ahe < Mwi) { 
		    r1 = (Ahe / 2) - (Ahe/7);
		    lz  = (Ahe/5) - 4;
		    if (lz > 10) {
			lz = 10;
		    }
		    r2 = r1 - lz;
		}
		else { 
		    r1 = (Mwi / 2) - (Mwi/7);
		    lz  = (Mwi/5)-4;
		    if (lz > 10) {
			lz = 10;
		    }
		    r2 = r1 - lz;
		}          
		x0 = Mwi / 2 - r1; 
		//x,y of the corner 1
		//
		y0 = Mhe - Dhe -(Ahe/2) - r1;
		x1 = Mwi/2 - r2; 
		// the corner of the inner circle
		//
		y1 = Mhe - Dhe-(Ahe/2)-r2;
		//draw the arcs
		//
		for (i = 0; i < 10; i++)
		    { if (i < 5) {
			    c1 = 204 - i * 51; 
			    c2 = 204 - i * 51;
			    c3 = 255;
			}
			else {
			    c1 = 0;
			    c2 = 0;
			    c3 = 255 - (i - 4) * 51;
			}   
			re = 0;
			gr = 0;
			bl = 255;
			switch (SCAColor) {
			case 1: {
			    re = c3;
			    gr = c2;
			    bl = c1;
			}
			    break;
			case 2: {
			    re = c2;
			    gr = c3;
			    bl = c1;
			}
			    break;
			case 3: {
			    re = c1;
			    gr = c2;
			    bl = c3;
			}
			    break; 
			}
			w1 = 240 - i * 30;
			big.setColor(new Color(re, gr, bl));
			big.fillArc(x0, y0, 2*r1, 2*r1, w1, -30);
			big.setColor(AFGColor);
			big.drawArc(x0, y0, 2*r1, 2*r1, w1, -30);
		    }
		big.setColor(ABGColor);
		big.fillOval(x1, y1, 2*r2, 2*r2);
		big.setColor(AFGColor);
		big.drawOval(x0, y0, 2*r1, 2*r1);
		big.drawOval(x1, y1, 2*r2, 2*r2);
		big.setColor(ABGColor);
		big.fillArc(x0 - 1, y0 - 1, 2*(r1 + 1), 2*(r1 + 1),240,60); 
		// end of draw arcs           

		big.setColor(AFGColor);          
		// draw the center point
		//
		xc = Mwi/2;  // X-Center
		yc = Mhe - Dhe - (Ahe/2);   // Y-Center
		big.fillOval(xc-1, yc-1, 2, 2);

		// draw the markerlines
		//
		dx =  (XMax - XMin) / 10.0; 
		for (i = 1; i <= 11; i++) { 
		    if (i == 11) {
			xx = (XMax - XMin);
		    }
		    else {
			xx =  (i - 1) *dx;
		    }
		    VAlpha =  240.0 - xx * (300 / (XMax - XMin)) ;
		    
		    // [GRAD] -> [RAD]
		    //
		    VAlphaRad = (VAlpha * 2.0 * Math.PI/360.0);
		    // Begin
		    //
		    x1 = xc + (int) (Math.round(r2*Math.cos(VAlphaRad))); 
		    y1 = yc  - (int) (Math.round(r2*Math.sin(VAlphaRad)));
		    // End

		    x2 = xc + (int) (Math.round(r1*Math.cos(VAlphaRad))); 
		    y2 = yc  - (int) (Math.round(r1*Math.sin(VAlphaRad)));
		    // draw the line
		    //
		    big.drawLine(x1,y1,x2,y2); 

		    // notice the first and last x/y-position
		    //
		    if (i == 1) {
			fx = x2; 
			fy = y2;
		    }
		    if (i==11) {
			lx = x2; 
			ly = y2;
		    }                 
		}
		// end of draw markerlines
		
		// print the scale- text at th first and last position
		// calculate the font
		//
		dy = Mhe - Dhe - fy;
		Fhe = 21;  // the large font
		do { 
		    Fhe--; 
		    f = new Font("Ariel", Font.BOLD, Fhe);
		    big.setFont(f);
		    fm  = getFontMetrics(f);
		    fhe = fm.getHeight();
		    } 
		while ((fhe + 1) > (dy)); 
		// left
		//
		s = String.valueOf(XMin);
		x =  fx - (fm.stringWidth(s)/2) - 2;
		y =  fy + fm.getHeight();
		if (y > (Hhe + Ahe - 3)) {
		    y = Hhe + Ahe - 3;
		}           // 1.01 
		if (x > 0) { 
		    big.setColor(AFGColor);
		big.drawString(s,x,y);
		}
         
		// right
		//
		s = String.valueOf(XMax);
		x = lx - (fm.stringWidth(s)/2) + 2;
		y = ly + fm.getHeight();
		if (y > (Hhe + Ahe - 3)) {
		    y = Hhe + Ahe - 3;
		}           //1.01
		if ((lx + (fm.stringWidth(s)/2)) < Mwi) {
		    big.setColor(AFGColor);
		    big.drawString(s,x,y);
		}                    
		
		// draw the value
		// Calculate VAlpha [GRAD]
		//
		XN = GetX();
		if (XN < XMin) {
		    XN = XMin;
		OutOfRange = true;
		}
		else
		    { if (XN > XMax) {
			    XN = XMax;
			    OutOfRange=true;
			}
			else {
			    OutOfRange=false;
			}
		    }       
		
		VAlpha = 240.0 - (XN - XMin) * (300.0 / (XMax - XMin));
		if (VAlpha < 0.0)  {
		    VAlpha = 360.0 + VAlpha;
		}

		// [GRAD] -> [RAD]
		VAlphaRad = (VAlpha * 2.0*Math.PI/360.0);

		// Calculate the positions x,y
		x = xc + (int) (Math.round((r2 - 2) * Math.cos(VAlphaRad))); 
		y = yc  - (int) (Math.round((r2 - 2) * Math.sin(VAlphaRad)));

		// draw the line
		//
		if (OutOfRange == true) {
		    big.setColor(ERRColor);
		}
		else {
		    big.setColor(AFGColor);
		}
		if (XNew <= 20){
		    int[] xpoints={xc - 5, xc + 5, x};
		    int[] ypoints={yc - 5, yc + 5, y};
		    big.fillPolygon(xpoints, ypoints, 3);
		}
		else {
		    if (XNew <= 35){
			int[] xpoints={xc, xc, x};
			int[] ypoints={yc - 5, yc + 5, y};
			big.fillPolygon(xpoints, ypoints, 3);
		    }
		    else{
			if (XNew <= 70){
			    int[] xpoints={xc - 6, xc + 6, x};
			    int[] ypoints={yc - 2, yc + 2, y};
			    big.fillPolygon(xpoints, ypoints, 3);
		    }

			else{
			    int[] xpoints={xc, xc, x};
			    int[] ypoints={yc + 6, yc - 6, y};
			    big.fillPolygon(xpoints, ypoints, 3);
			}
		    }
		}
		
		// End of draw value
		} // End of ANALOG
	    
	    g.drawImage(bimg, 0, 0, this);
        }

	/**
	 *  set the value of the meter
	 */
	public void SetMeter(double V) {
	    XNew = V; 
	    CHANGED = true;
	    repaint();    
	}  // END of setmeter


	/**
	 *  get the value of X
	 */
	public double GetX() {
	    boolean DB = Changed();
	    return XOld;
	}//End of GETX DOUBLE
    
    
	/**
	 *  check if the vlaue has changed
	 */
	public boolean Changed() {
	    boolean DB;
	    if (CHANGED == true) {
		XOld = XNew;
		CHANGED = false;
		DB = true;
	    }
	    else {
		DB = false;
	    }
	    return DB;
	}// End of CHANGED 
	

	/**
	 * this method is used to set parameters via JavaScript by
	 * using document.meter.SetParameter(list of parameter)
	 */
	public void SetParameter(String head,
				 String unit,
				 String digital,
				 String analog,
				 float xfrom,
				 float xto,
				 float x)
	{ 
	    Header = head;
	    if (Header.length()==0) {
		WHeader=false;
	    }
	    else {
		WHeader=true;
	    }
	    Unit = unit;
	    if (digital.equals("false") == true) {
		WDigital=false;
	    }
	    else {
		WDigital=true;
	    }

	    if (analog.equals("false")==true) {
		WAnalog=false;
	    }
	    else {
		WAnalog=true;
	    }
	    XMin = xfrom;
	    XMax = xto;
	    XNew = x;
	    XOld = XNew;
	    CHANGED=true; 
	    
	    // Recalculation
	    //
	    if (WHeader == true) {
		Hhe  = Mhe / 6;
	    }                       // height of the header
	    else {
		Hhe = 0;
	    }
	    if (WDigital == true) {
		Dhe  = Mhe / 6;
	    }                         // height of the digital instrument
	    else {
		Dhe = 0;
	    }
	    if (WAnalog == true) {
		Ahe  = Mhe - Hhe - Dhe;
	    }      // height of the analog instrument
	    else
		{ Ahe = 0;
		    if (WDigital == true) 
		    { if (WHeader == true){ 
			    Hhe = Mhe /3;
			    Dhe = Mhe - Hhe;
			} // DIGITAL with header
			else  {
			    Hhe = 0;
			    Dhe = Mhe;
			}// DIGITAL without header
		    }// WITHOUT ANA
		    else    { // only header (!!!
			Hhe = Mhe;
		    }
		}
	    // call paint method
	    //
	    repaint(); 
	} // End of SetParameter 
	
	
	/**
	 *  start the thread
	 */
        public void start() {
            thread = new Thread(this);
            thread.setPriority(Thread.MIN_PRIORITY);
            thread.setName("ConfidenceMonitor");
            thread.start();
        }

	/**
	 *  stop the thread
	 */
        public synchronized void stop() {
            thread = null;
            notify();
        }

	/**
	 *  run the thread
	 */
        public void run() {

            Thread me = Thread.currentThread();

            while (thread == me && !isShowing() || getSize().width == 0) {
                try {
                    thread.sleep(sleepAmount);
                } catch (InterruptedException e) { 
		    return; 
		}
            }

            while (thread == me && isShowing()) {
                Dimension d = getSize();
                if (d.width != w || d.height != h) {
                    w = d.width;
                    h = d.height;
                    bimg = (BufferedImage) createImage(w, h);
                    big = bimg.createGraphics();
                    big.setFont(font);
                    FontMetrics fm = big.getFontMetrics(font);
                    ascent = (int) fm.getAscent();
                    descent = (int) fm.getDescent();
                }
                repaint();
                try {
                    thread.sleep(sleepAmount);
                } catch (InterruptedException e) { break; }
                if (ConfidenceMonitor.dateStampCB.isSelected()) {
		    System.out.println(new Date().toString() + " " + usedStr);
                }
            }
            thread = null;
        }

	/**
	 * set the data
	 */
	public void setData(float[] temp, int size, int status) {
	
	    System.out.println("[Status in setdata]==================: "+status);
	    if (status == 2) {
		pts_received = 0;
		for (int i = 0; i <MAX_POINTS ; i++) {
		    pts_all[pts_received++] = (int)0;
		}
		ptNum = 0;
		flag = 1;
		return;
	    }

	    if (pts_received > MAX_POINTS - size) {
		pts_received = 0;
		ptNum = 0;
		for (int i = 0; i < MAX_POINTS; i++)
		    pts_all[i] = 100;
	    }

	    position = pts_received;
	    ptNum = pts_received;
	    for(int i = 0; i < size; i++) {
		pts_all[pts_received++] = (int)temp[i];
	    }
	    max_points = pts_received;
	}
    }
    
      /**
       *  the main method
       */
    public static void main(String s[]) {
        final ConfidenceMonitor demo3 = new ConfidenceMonitor();
        WindowListener l = new WindowAdapter() {
		public void windowClosing(WindowEvent e) {System.exit(0);}
		public void windowDeiconified(WindowEvent e) { demo3.surf.start(); }
		public void windowIconified(WindowEvent e) { demo3.surf.stop(); }
	    };
        JFrame f = new JFrame("ConfidenceMonitor");
        f.addWindowListener(l);
        f.getContentPane().add("Center", demo3);
        f.pack();
        f.setVisible(true);
        demo3.surf.start();
    }
}

















