//Trans_MultiGraphPanelB.java
/* A Java class for
 * LossyWide.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - All Rights Reserved
 */ 
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Trans_MultiGraphPanelB extends Panel implements MouseListener, ItemListener, Runnable, AdjustmentListener{
    public Scrollbar slider;
    private  static final Color bgcolor = new Color(255,255,255);
    //private static final Color tinta2 = new Color(216,216,191); // kaki
    private static final Color tinta = new Color(246,246,231); 
    private static final Color maroon = new Color(185,50,50);
    //private  static final Color bgcolor = new Color(161,220,220);
    private static final Color tinta2 = new Color(236,236,236);
    private Label titlelabel, selectlabel, fast, slow;   
    //private static final Font labfont = new Font("SanSerif",Font.PLAIN,11);
    //private static final Font titlefont = new Font("SanSerif",Font.BOLD,16);    
    //private static final Font symbolfont = new Font("Symbol",Font.PLAIN,12);
    //private static final Font normalfont = new Font("SanSerif",Font.PLAIN,14);
    public static  Font labfont;
    public static  Font titlefont;    
    public static  Font symbolfont;
    public static  Font normalfont;
    
    private String titulo;
    public Choice c1;
    private Thread tron;
    public boolean IsTronRunning;
    public boolean ThreadStarted, IsTraceON, IsEnvelopeON;
    private int SleepTime = 50;
    private Trans_State t_state;
    
    public TransGraphCanvas2 tgc3;
    public StartStopClock ssc;
    
    public int DynamicRunType = 1;
    public Checkbox cbx1, cbx2;
    
    private double[] y5, y6;
    private int NPoints = 301;
    
    private int SCROLLMIN=0, SCROLLMAX=250;
    private int down = 144;
    
    public Trans_MultiGraphPanelB(String titulo){
	super();
	this.titulo=titulo;
	setLayout(null);
	setBackground(bgcolor);
	
	//t_state = new Trans_State();
	
	selectlabel = new Label("Click to select a plot",Label.RIGHT);
	selectlabel.setFont(labfont);
	selectlabel.setBackground(bgcolor);
	selectlabel.setForeground(Color.blue);
	//add(selectlabel);
	
	fast = new Label("faster",Label.RIGHT);
	fast.setFont(labfont);
	fast.setBackground(bgcolor);
	fast.setForeground(maroon);
	add(fast);
	
	slow = new Label("slower",Label.LEFT);
	slow.setFont(labfont);
	slow.setBackground(bgcolor);
	slow.setForeground(Color.blue);
	add(slow);
	
	selectlabel.setBounds(25,223+down,100,15);
	 
	c1 = new Choice();
        c1.addItem("  Select a Plot");
	c1.addItem("  Time Dependent Voltage");
	c1.addItem("  Time Dependent Current");
	c1.addItem("  Time Dependent Power");
	
	c1.setBackground(Color.white);
	c1.setFont(normalfont);
	
        titlelabel = new Label(titulo,Label.CENTER);
	titlelabel.setFont(titlefont);
	
	tgc3 = new TransGraphCanvas2();
	ssc  = new StartStopClock();
	
        cbx1 = new Checkbox("Trace OFF",false);
	cbx2 = new Checkbox("Envelope ON",true);
	cbx1.setFont(normalfont);
	cbx2.setFont(normalfont);
        IsTraceON = false;
        IsEnvelopeON = true;
        cbx1.setState(false);
        ssc.button1.setEnabled(true);
        ssc.button2.setEnabled(true);
	
	slider = new Scrollbar(Scrollbar.HORIZONTAL,200,1,SCROLLMIN,SCROLLMAX);	
        slider.setBackground(Color.white);
	
        add(c1);
	add(tgc3);
	add(ssc);
	add(cbx1);
	add(cbx2);
	add(slider);
	
	titlelabel.setBounds(5,5,300,25);
	// CHANGE ==============================================================
        //c1.setBounds(5,241+down,220,25);
        c1.setBounds(3,241+down,228,25);
        //======================================================================
	
	//panel for contour of plot choice - take out
	Panel pgraph5 = new Panel();
	    pgraph5.setBackground(Color.cyan);
	    //add(pgraph5);
	    pgraph5.setBounds(18,239+down,199,26);
	Panel pgraph6 = new Panel();
	    pgraph6.setBackground(Color.black);
	    //add(pgraph6);
	    pgraph6.setBounds(17,238+down,201,28);
	
	//plotting window
	tgc3.setBounds(6,6,888,211+down);
	
	//start stop clock
	ssc.setBounds(466,223+down,170,60);
	
	//choice for trace
	// CHANGE ==============================================================
        //cbx1.setBounds(650,226+down,100,18);
        cbx1.setBounds(645,226+down,105,18);
        //cbx2.setBounds(760,226+down,120,18);
	cbx2.setBounds(760,226+down,130,18);
        //======================================================================
	//slider to change animation speed
	slider.setBounds(643,253+down,250,15);
	fast.setBounds(832,271+down,40,10);
	slow.setBounds(666,271+down,100,10);
	
	//panels for slider
	Panel slide0 = new Panel();
	    slide0.setBackground(Color.cyan);
	    add(slide0);
	    slide0.setBounds(642,252+down,252,17);	
	Panel slide1 = new Panel();
	    slide1.setBackground(Color.black);
	    add(slide1);
	    slide1.setBounds(641,251+down,254,19);
	
	//panels for background of plotting window
	Panel pgraph1 = new Panel();
	    pgraph1.setBackground(tinta2);
	    //pgraph1.setBackground(bgcolor);
	    add(pgraph1);
	    pgraph1.setBounds(6,6,888,211+down);
	
	Panel pgraph2 = new Panel();
	    pgraph2.setBackground(Color.black);
	    add(pgraph2);
	    pgraph2.setBounds(5,5,890,213+down);
	
	//panel for contour of start stop clock
	Panel pgraph3 = new Panel();
	    pgraph3.setBackground(Color.black);
	    //add(pgraph3);
	    pgraph3.setBounds(455,223+down,172,58);
	
	//panel for contour of trace choice
	Panel pgraph4a = new Panel();
	    pgraph4a.setBackground(Color.white);
	    add(pgraph4a);
	    pgraph4a.setBounds(642,224+down,252,22);
	Panel pgraph4 = new Panel();
	    pgraph4.setBackground(Color.black);
	    add(pgraph4);
	    pgraph4.setBounds(641,223+down,254,24);
	
	IsTronRunning = false;
        ThreadStarted = false;
	
	//Listeners
	ssc.button1.addMouseListener(this);
	ssc.button2.addMouseListener(this);
	cbx1.addItemListener(this);
	cbx2.addItemListener(this);
	slider.addAdjustmentListener(this);
    }			
    
    public void paint(Graphics g){
            g.clearRect(0,0,getSize().width,getSize().height);
	    
	    g.setColor(Color.black);
	    g.drawRect(0,0,getSize().width-1,getSize().height-1);
	    
            g.setColor(Color.blue);
	    MaestroG.drawArrowtip(650,getSize().height-12,8,g);
	    MaestroG.drawArrowtip(658,getSize().height-12,8,g);
	    
	    g.setColor(maroon);
	    MaestroG.drawArrowtip(877,getSize().height-12,7,g);
	    MaestroG.drawArrowtip(885,getSize().height-12,7,g);
            
            if(IsTronRunning){
                g.setFont(labfont);
                g.drawString("During   animation,  position",235,235+down);
                g.drawString("sliders and selection menus",235,249+down);
                g.drawString("are  disabled.   Click   STOP",235,263+down);
                g.drawString("to enable them again.",235,277+down);
            }
            
    }
    
    public void mouseClicked(MouseEvent evt){mouseHandler(evt);}
    public void mouseEntered(MouseEvent evt){;}
    public void mouseExited(MouseEvent evt){;}
    public void mousePressed(MouseEvent evt){;}
    public void mouseReleased(MouseEvent evt){;}
    
    private synchronized void mouseHandler(MouseEvent evt){
	if(evt.getSource() == ssc.button1){
            IsTronRunning = true;
	    if(!ThreadStarted){
                start();
                ThreadStarted = true;
                ssc.button1.setEnabled(false);
                ssc.button2.setEnabled(true);           
            }
            else{
                ssc.button1.setEnabled(false);
                ssc.button2.setEnabled(true);
                notify();
            }
            //c1.setEnabled(false);
            repaint();
	}
	if(evt.getSource() == ssc.button2){
	    IsTronRunning = false;
            ssc.button2.setEnabled(false);
            ssc.button1.setEnabled(true);             
            //c1.setEnabled(true);
            repaint();
	}
    }
    
    public void itemStateChanged(ItemEvent evt){
	if(evt.getSource() == cbx1){
	    if(IsTraceON){
                tgc3.setTrace(false);
                IsTraceON = false;
                cbx1.setLabel("Trace OFF");
                cbx1.setState(false);
                tgc3.cleanUp();
            }
            else{
                tgc3.setTrace(true);
                IsTraceON = true;
                cbx1.setLabel("Trace ON");
                cbx1.setState(true);
                tgc3.cleanUp();
            }
	}
	else if(evt.getSource() == cbx2){
            if(IsEnvelopeON){
                tgc3.setEnvelope(false);
                IsEnvelopeON = false;
                cbx2.setLabel("Envelope OFF");
                cbx2.setState(false);
                tgc3.cleanUp();
                tgc3.repaint();
            }
            else{
                tgc3.setEnvelope(true);
                IsEnvelopeON = true;
                cbx2.setLabel("Envelope ON");
                cbx2.setState(true);
                tgc3.cleanUp();
                tgc3.repaint();
            }
	}
    }
	
	
    public synchronized void plotTimeDependentVoltage(Trans_State state){
	    String alpha, Ohm, lambda, infinity, Gamma;
	    setFont(symbolfont);
	    alpha="\u03b1";
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    infinity="\u221e";
	    Gamma="\uu0393";
	    setFont(labfont);
	
	state.IsTimePlot = 1;
	
	//if(tron != null) tron.suspend();
	t_state = state;
        if(IsTraceON){
            cbx1.setState(true);
        }
        
	t_state.reset();
	ssc.clockcanvas.setStatus(0,state.NTime,state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 1;
	tgc3.reset();
	tgc3.setDynamics(false);
        tgc3.setLabFlag(1); 
        
	t_state.ScanTimeDependentVoltage();
        
	//tgc3.setLabelY("2 |V+| exp ("+alpha+" l)");  JAN
	tgc3.setLabelY("2 |V+|");
	tgc3.setTitle("V (z, t)");
	if (state.getYRangeMax(1)>=1.0 && state.getYRangeMax(1)<1.0E3 || state.getYRangeMax(1)==0.0){
	    tgc3.setUnits("[ V ]");  
	}
	else if(state.getYRangeMax(1)<1.0E6 && state.getYRangeMax(1)>=1.0E3  ){
	    tgc3.setUnits("[ k V ]");
	}
	else if(state.getYRangeMax(1)>=1.0E6  ){
	    tgc3.setUnits("[ M V ]");
	}
	else if(state.getYRangeMax(1)<1.0 && state.getYRangeMax(1)>=1.0E-3  ){
	    tgc3.setUnits("[ m V ]");
	}
	else if(state.getYRangeMax(1)<1.0E-3 && state.getYRangeMax(1)>=1.0E-6  ){
	    tgc3.setUnits("[ \u00b5 V ]");
	}
	else if(state.getYRangeMax(1)<1.0E-6 && state.getYRangeMax(1)>=1.0E-9  ){ 
	    tgc3.setUnits("[ n V ]");
	}
	else if(state.getYRangeMax(1)<1.0E-9 && state.getYRangeMax(1)>=1.0E-12  ){
	    tgc3.setUnits("[ p V ]");
	}
	else if(state.getYRangeMax(1)<1.0E-12 && state.getYRangeMax(1)>=1.0E-15 ){
	    tgc3.setUnits("[ f V ]");
	}
	else if(state.getYRangeMax(1)<1.0E-15 ){
	    tgc3.setUnits("[ a V ]");
	}
	
	//--------------------------------------------------------------------------------------------------
	double tempvalue;
	
	// Time Dependent Voltage
      
      if(state.IsTimePlot == 1){
        tempvalue = 0.0;
       
	if(state.IsTotal){
            tempvalue = Complex.Magnitude(state.Vin);
        }
        else if(state.IsIncident){
            tempvalue = Complex.Magnitude(state.VPlus);
        }
        else if(state.IsReflected){
            tempvalue = Complex.Magnitude(Complex.Multiply(state.VPlus,state.GammaL));
        }
        
	if (tempvalue>=1.0 && tempvalue<1.0E3|| tempvalue==0.0){
	    tgc3.setValue(" Vmax  = "+MaestroA.rounder(tempvalue,3)+"  [ V ]");   
	}
	else if(tempvalue<1.0E6 && tempvalue>=1.0E3  ){
	    tgc3.setValue(" Vmax  = "+MaestroA.rounder(tempvalue/1.0E3,3)+"  [ k V ]");   
	}
	else if(tempvalue>=1.0E6  ){
	    tgc3.setValue(" Vmax  = "+MaestroA.rounder(tempvalue/1.0E6,3)+"  [ M V ]");   
	}
	else if(tempvalue<1.0 && tempvalue>=1.0E-3  ){
	    tgc3.setValue(" Vmax  = "+MaestroA.rounder(tempvalue*1.0E3,3)+"  [ m V ]");   
	}
	else if(tempvalue<1.0E-3 && tempvalue>=1.0E-6  ){
	    tgc3.setValue(" Vmax  = "+MaestroA.rounder(tempvalue*1.0E6,3)+"  [ \u00b5 V ]");   
	}
	else if(tempvalue<1.0E-6 && tempvalue>=1.0E-9  ){
	    tgc3.setValue(" Vmax  = "+MaestroA.rounder(tempvalue*1.0E9,3)+"  [ n V ]");   
	}
	else if(tempvalue<1.0E-9 && tempvalue>=1.0E-12  ){
	    tgc3.setValue(" Vmax  = "+MaestroA.rounder(tempvalue*1.0E12,3)+"  [ p V ]");   
	}
	else if(tempvalue<1.0E-12 &&  tempvalue>=1.0E-15){
	    tgc3.setValue(" Vmax  = "+MaestroA.rounder(tempvalue*1.0E15,3)+"  [ f V ]");   
	}
	else if(tempvalue<1.0E-15){
	    tgc3.setValue(" Vmax  = 0.0  [ V ]");   
	}
      }
	//--------------------------------------------------------------------------------------------------
	tgc3.setYRangeMax(t_state.getYRangeMax(1));
	tgc3.setYRangeMin(-t_state.getYRangeMax(1));
	tgc3.plot(t_state.x,t_state.y1T,t_state.y3,t_state.y3min);
	tgc3.cleanUp();
    } 
    
    public synchronized void plotTimeDependentCurrent(Trans_State state){
	String alpha, Ohm, lambda, infinity, Gamma;
	    setFont(symbolfont);
	    alpha="\u03b1";
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    infinity="\u221e";
	    Gamma="\uu0393";
	    setFont(labfont);
	
	state.IsTimePlot = 2;    
	
	//if(tron != null) tron.suspend();
	t_state = state;
	if(IsTraceON){
            cbx1.setState(true);
        }
	t_state.reset();
	ssc.clockcanvas.setStatus(0,t_state.NTime,t_state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 2;
	tgc3.reset();
	tgc3.setDynamics(false);
        tgc3.setLabFlag(2); 
        
	t_state.ScanTimeDependentCurrent();
	
        //tgc3.setLabelY("2 |V+| exp ("+alpha+" l)/ Zo");  JAN
        tgc3.setLabelY("2 |V+|/ Zo");
	tgc3.setTitle("I (z, t)");
	if (state.getYRangeMax(2)>=1.0 && state.getYRangeMax(2)<1.0E3 || state.getYRangeMax(2)==0.0){
	    tgc3.setUnits("[ A ]");   
	}
	else if(state.getYRangeMax(2)<1.0E6 && state.getYRangeMax(2)>=1.0E3  ){
	    tgc3.setUnits("[ k A ]"); 
	   
	}
	else if(state.getYRangeMax(2)>=1.0E6 ){
	    tgc3.setUnits("[ M A ]");  
	   
	}
	else if(state.getYRangeMax(2)<1.0 && state.getYRangeMax(2)>=1.0E-3  ){
	    tgc3.setUnits("[ m A ]");  
	   
	}
	else if(state.getYRangeMax(2)<1.0E-3 && state.getYRangeMax(2)>=1.0E-6  ){
	    tgc3.setUnits("[ \u00b5 A ]");  
	    
	}
	else if(state.getYRangeMax(2)<1.0E-6 && state.getYRangeMax(2)>=1.0E-9  ){
	    tgc3.setUnits("[ n A ]"); 
	    
	}
	else if(state.getYRangeMax(2)<1.0E-9 && state.getYRangeMax(2)>=1.0E-12  ){
	    tgc3.setUnits("[ p A ]");  
	    
	}
	else if(state.getYRangeMax(2)<1.0E-12 && state.getYRangeMax(2)>=1.0E-15  ){
	    tgc3.setUnits("[ f A ]");  
	   
	}
	else if(state.getYRangeMax(2)<1.0E-15  ){
	    tgc3.setUnits("[ a A ]");  
	   
	}
	
	//-----------------------------------------------------------------------------------
	double tempvalue;
	
	// Time Dependent Current
      if(state.IsTimePlot == 2){
      
	//tempvalue = Complex.Magnitude(state.Iin);
        
        tempvalue = 0.0;
       
	if(state.IsTotal){
            tempvalue = Complex.Magnitude(state.Iin);
        }
        else if(state.IsIncident){
            tempvalue = Complex.Magnitude(Complex.Divide(state.VPlus,state.Z0));
        }
        else if(state.IsReflected){
            tempvalue = Complex.Magnitude(Complex.Multiply(Complex.Divide(state.VPlus,state.Z0),state.GammaL));
        }
	
	if (tempvalue>=1.0 && tempvalue<1.0E3|| tempvalue==0.0){
	    tgc3.setValue(" Imax  = "+MaestroA.rounder(tempvalue,3)+"  [ A ]");   
	}
	else if(tempvalue<1.0E6 && tempvalue>=1.0E3  ){
	    tgc3.setValue(" Imax  = "+MaestroA.rounder(tempvalue/1.0E3,3)+"  [ k A ]");   
	}
	else if(tempvalue>=1.0E6  ){
	    tgc3.setValue(" Imax  = "+MaestroA.rounder(tempvalue/1.0E6,3)+"  [ M A ]");   
	}
	else if(tempvalue<1.0 && tempvalue>=1.0E-3  ){
	    tgc3.setValue(" Imax  = "+MaestroA.rounder(tempvalue*1.0E3,3)+"  [ m A ]");   
	}
	else if(tempvalue<1.0E-3 && tempvalue>=1.0E-6  ){
	    tgc3.setValue(" Imax  = "+MaestroA.rounder(tempvalue*1.0E6,3)+"  [ \u00b5 A ]");   
	}
	else if(tempvalue<1.0E-6 && tempvalue>=1.0E-9  ){
	    tgc3.setValue(" Imax  = "+MaestroA.rounder(tempvalue*1.0E9,3)+"  [ n A ]");   
	}
	else if(tempvalue<1.0E-9 && tempvalue>=1.0E-12  ){
	    tgc3.setValue(" Imax  = "+MaestroA.rounder(tempvalue*1.0E12,3)+"  [ p A ]");   
	}
	else if(tempvalue<1.0E-12 &&  tempvalue>=1.0E-15){
	    tgc3.setValue(" Imax  = "+MaestroA.rounder(tempvalue*1.0E15,3)+"  [ f A ]");   
	}
	else if(tempvalue<1.0E-15){
	    tgc3.setValue(" Imax  = 0.0  [ A ]");   
	}
      }
      
	//-----------------------------------------------------------------------------------
	
	tgc3.setYRangeMax(t_state.getYRangeMax(2));
	tgc3.setYRangeMin(-t_state.getYRangeMax(2));
	tgc3.plot(t_state.x,t_state.y1T,t_state.y4,t_state.y4min);
	tgc3.cleanUp();
    }	
    
    public synchronized void plotTimeDependentPower(Trans_State state){
	String alpha, Ohm, lambda, infinity, Gamma;
	    setFont(symbolfont);
	    alpha="\u03b1";
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    infinity="\u221e";
	    Gamma="\uu0393";
	    setFont(labfont);
	    
	state.IsTimePlot = 3;
        
	//if(tron != null) tron.suspend();
	t_state = state;
	if(IsTraceON){
            cbx1.setState(true);
        }
	t_state.reset();
	ssc.clockcanvas.setStatus(0,t_state.NTime,t_state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 3;
	tgc3.reset();
	tgc3.setDynamics(false);
        tgc3.setLabFlag(3); 
        
	t_state.ScanTimeDependentPower();
	
	//tgc3.setLabelY("|V+|\u00b2 exp (2 "+alpha+" l)/ Zo");  JAN
	tgc3.setLabelY("|V+|\u00b2 / Zo");
	tgc3.setTitle("P (z, t)");
	if (state.getYRangeMax(3)>=1.0 && state.getYRangeMax(3)<1.0E3 || state.getYRangeMax(3)==0.0){
	    tgc3.setUnits("[ W ]"); 
	}
	else if(state.getYRangeMax(3)<1.0E6 && state.getYRangeMax(3)>=1.0E3  ){
	    tgc3.setUnits("[ k W ]"); 
	   
	}
	else if(state.getYRangeMax(3)>=1.0E6  ){
	    tgc3.setUnits("[ M W ]"); 
	   
	}
	else if(state.getYRangeMax(3)<1.0 && state.getYRangeMax(3)>=1.0E-3  ){
	    tgc3.setUnits("[ m W ]");  
	   
	}
	else if(state.getYRangeMax(3)<1.0E-3 && state.getYRangeMax(3)>=1.0E-6  ){
	    tgc3.setUnits("[ \u00b5 W ]");  
	    
	}
	else if(state.getYRangeMax(3)<1.0E-6 && state.getYRangeMax(3)>=1.0E-9  ){
	    tgc3.setUnits("[ n W ]");  
	    
	}
	else if(state.getYRangeMax(3)<1.0E-9 && state.getYRangeMax(3)>=1.0E-12  ){
	    tgc3.setUnits("[ p W ]");  
	    
	}
	else if(state.getYRangeMax(3)<1.0E-12 && state.getYRangeMax(3)>=1.0E-15){
	    tgc3.setUnits("[ f W ]");  
	   
	}
	else if(state.getYRangeMax(3)<1.0E-15){
	    tgc3.setUnits("[ a W ]");  
	   
	}
	
	//--------------------------------------------------------------------------------
	
	double tempvalue;
	
      // Time Dependent Power
      if(state.IsTimePlot == 3){
      
	tempvalue = 0.0; 
        
        if(state.IsTotal){
            //tempvalue = Complex.Magnitude(state.Vin)*Complex.Magnitude(state.Iin);
            tempvalue = Complex.Magnitude(Complex.Multiply(state.Vin,state.Iin));
        }
        else if(state.IsIncident){
            tempvalue = Complex.Magnitude(Complex.Divide(Complex.Multiply(state.VPlus,state.VPlus),state.Z0));
        }
        else if(state.IsReflected){
            tempvalue = Complex.Magnitude(Complex.Multiply(
                                                            Complex.Multiply(state.VPlus,state.GammaL),
                                                            Complex.Multiply(Complex.Divide(state.VPlus,state.Z0),state.GammaL))
                                                            );
        }
        
        if (tempvalue>=1.0 && tempvalue<1.0E3|| tempvalue==0.0){
	    tgc3.setValue(" Pmax  = "+MaestroA.rounder(tempvalue,3)+"  [ W ]");   
	}
	else if(tempvalue<1.0E6 && tempvalue>=1.0E3  ){
	    tgc3.setValue(" Pmax  = "+MaestroA.rounder(tempvalue/1.0E3,3)+"  [ k W ]");   
	}
	else if(tempvalue>=1.0E6  ){
	    tgc3.setValue(" Pmax  = "+MaestroA.rounder(tempvalue/1.0E6,3)+"  [ M W ]");   
	}
	else if(tempvalue<1.0 && tempvalue>=1.0E-3  ){
	    tgc3.setValue(" Pmax  = "+MaestroA.rounder(tempvalue*1.0E3,3)+"  [ m W ]");   
	}
	else if(tempvalue<1.0E-3 && tempvalue>=1.0E-6  ){
	    tgc3.setValue(" Pmax  = "+MaestroA.rounder(tempvalue*1.0E6,3)+"  [ \u00b5 W ]");   
	}
	else if(tempvalue<1.0E-6 && tempvalue>=1.0E-9  ){
	    tgc3.setValue(" Pmax  = "+MaestroA.rounder(tempvalue*1.0E9,3)+"  [ n W ]");   
	}
	else if(tempvalue<1.0E-9 && tempvalue>=1.0E-12  ){
	    tgc3.setValue(" Pmax  = "+MaestroA.rounder(tempvalue*1.0E12,3)+"  [ p W ]");   
	}
	else if(tempvalue<1.0E-12 &&  tempvalue>=1.0E-15){
	    tgc3.setValue(" Pmax  = "+MaestroA.rounder(tempvalue*1.0E15,3)+"  [ f W ]");   
	}
	else if(tempvalue<1.0E-15){
	    tgc3.setValue(" Pmax  = 0.0  [ W ]");   
	}
      }
      	
	//--------------------------------------------------------------------------------
	
	tgc3.setYRangeMax(t_state.getYRangeMax(3));
	tgc3.setYRangeMin(-t_state.getYRangeMax(3));
	tgc3.plot(t_state.x,t_state.y1T,y5,y6);
	tgc3.cleanUp();
    }
    
    public void start(){
	if(tron == null){
	    tron = new Thread(this);
	    tron.start();
	}
    }
    
    //public void stop(){
	//if(tron != null) {
	  //  tron.stop();
	  //  tron = null;
	//}
    //}
    
    public void run(){
	
        while(true){
	    if(IsTronRunning){
		plotDynamicRun();
		ssc.clockcanvas.increment();
		t_state.increment();
	    }
	    try{
		Thread.sleep(SleepTime);
                synchronized(this){
                    while(!IsTronRunning){wait();}
                }
	    }
	    catch(InterruptedException e){e.printStackTrace();}
	}
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	  if(evt.getSource()==slider){
		SleepTime = SCROLLMAX - slider.getValue();
	  }
    }
    
    public synchronized void plotDynamicRun(){
	switch(DynamicRunType){
	    case 5:
		t_state.ScanRandom();
		tgc3.setTitle("Test");
		tgc3.reset();
		tgc3.setDynamics(true);
		tgc3.plot(t_state.x,t_state.y1T,t_state.y1,t_state.y1);
	    break;
		
	    case 1:
		t_state.ScanTimeDependentVoltage();
		tgc3.plot(t_state.x,t_state.y1T,t_state.y3,t_state.y3min);
		tgc3.setDynamics(true);
	    break;
	    
	    case 2:
		t_state.ScanTimeDependentCurrent();
		tgc3.setDynamics(true);
		tgc3.plot(t_state.x,t_state.y1T,t_state.y4,t_state.y4min);
	    break;
	    
	    case 3:
		t_state.ScanTimeDependentPower();
		tgc3.setDynamics(true);
		tgc3.plot(t_state.x,t_state.y1T,y5,y6);
	    break; 
	}
    }
    
     public void setPowerEnvelope(double ydata[], double ydatamin[]){
	NPoints = ydata.length;
	y5 = new double[NPoints];
        y6 = new double[NPoints];
        
	for(int i = 0; i < NPoints; i++){
	    y5[i] = ydata[i];
            y6[i] = ydatamin[i];
        }
    }
}
