//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.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Trans_MultiGraphPanelB extends Panel implements MouseListener, ItemListener, Runnable, ChangeListener{
    public JSlider slider;
    private  static final Color bgcolor = new Color(255,255,255);
    private static final Color tinta = new Color(246,246,231); 
    private static final Color maroon = new Color(185,50,50);
    private static final Color tinta2 = new Color(236,236,236);
    private Label titlelabel, selectlabel, fast, slow;   
    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;
    Trans_State state;
    
    public Trans_MultiGraphPanelB(String titulo, Trans_State state){
	super();
        this.state = state;
	this.titulo=titulo;
	setLayout(null);
	setBackground(bgcolor);
	
	//t_state = new Trans_State();
	down = state.s144;
	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(new Font("SanSerif",Font.PLAIN,state.font10));
	fast.setBackground(bgcolor);
	fast.setForeground(maroon);
	add(fast);
	
	slow = new Label("slower",Label.LEFT);
	slow.setFont(new Font("SanSerif",Font.PLAIN,state.font10));
	slow.setBackground(bgcolor);
	slow.setForeground(Color.blue);
	add(slow);
	
	selectlabel.setBounds(state.s25,state.s223+down,state.s100,state.s15);
	 
	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(new Font("SanSerif",Font.ITALIC,state.font12));
	
        titlelabel = new Label(titulo,Label.CENTER);
	titlelabel.setFont(titlefont);
	
	tgc3 = new TransGraphCanvas2(state);
	ssc  = new StartStopClock(state);
	
        cbx1 = new Checkbox("Trace ON",true);
	cbx2 = new Checkbox("Envelope ON",true);
	cbx1.setFont(new Font("SanSerif",Font.ITALIC,state.font11));
	cbx2.setFont(new Font("SanSerif",Font.ITALIC,state.font11));
        IsTraceON = true;
        IsEnvelopeON = true;
        cbx1.setState(false);
        ssc.button1.setEnabled(true);
        ssc.button2.setEnabled(true);
	
	slider = new JSlider(SCROLLMIN,SCROLLMAX);	
	
        add(c1);
	add(tgc3);
	add(ssc);
	add(cbx1);
	add(cbx2);
	add(slider);
	
	titlelabel.setBounds(state.s5,state.s5,state.s300,state.s25);
	// CHANGE ==============================================================
        c1.setBounds(state.s10,state.s241+down,state.s220,state.s22);
        Panel pc1 = new Panel();
	    pc1.setBackground(Color.gray);
	    //add(pc1);
	    pc1.setBounds(state.s10-1,state.s241+down-1,state.s200+state.s20+2,state.s22+2);
        //======================================================================
	
	//panel for contour of plot choice - take out
	Panel pgraph5 = new Panel();
	    pgraph5.setBackground(Color.cyan);
	    //add(pgraph5);
	    pgraph5.setBounds(state.s18,state.s239+down,state.s199,state.s26);
	Panel pgraph6 = new Panel();
	    pgraph6.setBackground(Color.black);
	    //add(pgraph6);
	    pgraph6.setBounds(state.s17,state.s238+down,state.s201,state.s28);
	
	//plotting window
	tgc3.setBounds(state.s6,state.s6,state.s882,state.s211+down);
	
	//start stop clock
	ssc.setBounds(state.s466,state.s223+down,state.s170,state.s60);
	
	//choice for trace
	// CHANGE ==============================================================
        cbx1.setBounds(state.s650,state.s226+down,state.s105,state.s18);
        cbx2.setBounds(state.s760,state.s226+down,state.s120,state.s18);
        //======================================================================
	//slider to change animation speed
	//slider.setBounds(state.s643,state.s253+down,state.s240,state.s15);
	slider.setBackground(Color.white);
        slider.setBounds(state.s642+1,state.s253+down,state.s246-2,state.s15);
        
	fast.setBounds(state.s827,state.s272+down,state.s40,state.s10);
	slow.setBounds(state.s666,state.s272+down,state.s100,state.s10);
	
	
	//panels for background of plotting window
	Panel pgraph1 = new Panel();
	    pgraph1.setBackground(tinta2);
	    //pgraph1.setBackground(bgcolor);
	    add(pgraph1);
	    pgraph1.setBounds(state.s6,state.s6,state.s882,state.s211+down);
	
	Panel pgraph2 = new Panel();
	    pgraph2.setBackground(Color.black);
	    add(pgraph2);
	    pgraph2.setBounds(state.s6-1,state.s6-1,state.s882+2,state.s211+down+2);
	
	//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(state.s642,state.s224+down,state.s246,state.s22);
	Panel pgraph4 = new Panel();
	    pgraph4.setBackground(Color.black);
	    add(pgraph4);
	    pgraph4.setBounds(state.s642-1,state.s224+down-1,state.s246+2,state.s22+2);
	
	IsTronRunning = false;
        ThreadStarted = false;
	
	//Listeners
	ssc.button1.addMouseListener(this);
	ssc.button2.addMouseListener(this);
	cbx1.addItemListener(this);
	cbx2.addItemListener(this);
	slider.addChangeListener(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(state.s650,state.s278+down,8,g);
	    MaestroG.drawArrowtip(state.s658,state.s278+down,8,g);
	    
	    g.setColor(maroon);
	    MaestroG.drawArrowtip(state.s872,state.s278+down,7,g);
	    MaestroG.drawArrowtip(state.s880,state.s278+down,7,g);
            
            if(IsTronRunning){
                g.setFont(new Font("SanSerif",Font.PLAIN,state.font10));
                g.drawString("During   animation,  position",state.s237,state.s235+down);
                g.drawString("sliders and selection menus",state.s237,state.s249+down);
                g.drawString("are  disabled.   Click   STOP",state.s237,state.s263+down);
                g.drawString("to enable them again.",state.s237,state.s277+down);
            }
	    slider.requestFocusInWindow();
    }
    
    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));
            state.ignition();
            tempvalue = state.Power_X;
        }
        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 stateChanged(ChangeEvent 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];
        }
    }
}
