//Trans_MultiGraphPanelC.java
//This is for frequency plots
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Trans_MultiGraphPanelC extends Panel implements MouseListener, ItemListener, AdjustmentListener, Runnable, ActionListener{
    //private static final Color bgcolor = new Color(216,216,191);
    //private  static final Color bgcolor = new Color(161,220,220);
    private static final Color tinta = new Color(236,236,236);
    private static final Color tinta2 = new Color(236,236,236);
    private  static final Color bgcolor = new Color(255,255,255);
    
    private static final Font titlefont = TheFonts.bold16;
    private static final Font watchfont = TheFonts.bold14;
    
    private static final Font labfont = TheFonts.sanSerif11;
    private static final Font labfont2 = TheFonts.bold11;
    private static final Font labfont3 = TheFonts.sanSerif16;
    private final Font labelfont=TheFonts.sanSerif12;
    
   
    public Label titlelabel, selectlabel, titulolabel, fast, slow, 
		 watch1, watch2, watch3, watch4, rise1, rise2, rise3, rise4, rise5;   
    private String titulo;
    public Choice c1;
    private Thread tron;
    private boolean IsTronRunning; 
    public boolean IsStepOn;
    private double temp;
    private int SleepTime = 100;
    private int RampIndex = 0;
    private int ScrollMax = 1000;
    //private int ScrollMax2 = 9993;
    private int ScrollMax2 = 4057;
    public double RiseTime =0.5;
    public Scrollbar slider1, slider2;
    public Trans_State t_state;
    public TransGraphCanvas tgc1;
    public TransGraphCanvas2 tgc3;
    public Panel ps3, ps4;
    
    // CHANGE ==============================================================
        public Button b2a, b2b;
    //======================================================================
	
        
    StartStopClock ssc;
    private int DynamicRunType = 1;
    private CheckboxGroup cbxgroup;
    private Checkbox cbx1, cbx2;
    public CheckboxGroup cbgroup;
    public Checkbox cb1, cb2;
    public Trans_MultiGraphPanelC(String titulo){
	super();
	this.titulo=titulo;
	setLayout(null);
	setBackground(bgcolor);
	
        setFont(TheFonts.sanSerif14);
	//t_state = new Trans_State();
	 
	c1 = new Choice();
        c1.addItem(" Select a Plot");
	c1.addItem(" Transient Voltage");
	c1.addItem(" Transient Current");
	c1.addItem(" Transient Power");
	
	c1.setBackground(Color.white);
	c1.setFont(TheFonts.sanSerif11);
	
	titlelabel = new Label(titulo,Label.CENTER);
	titlelabel.setFont(titlefont);
	
	titulolabel = new Label("Transient Response",Label.LEFT);
	titulolabel.setFont(titlefont);
	
	selectlabel = new Label("Click to select a plot",Label.RIGHT);
	selectlabel.setFont(labfont);
	selectlabel.setBackground(bgcolor);
	selectlabel.setForeground(Color.blue);
	fast = new Label("FAST >>",Label.RIGHT);
	slow = new Label("<< SLOW",Label.LEFT);
	fast.setFont(labfont2);
	slow.setFont(labfont2);
	watch1 = new Label("0.00",Label.LEFT);
	watch2 = new Label("T",Label.LEFT);
	watch1.setFont(watchfont);
	watch2.setFont(watchfont);
	
	watch3 = new Label("0.00",Label.LEFT);
	watch4 = new Label("s",Label.LEFT);
	
	watch3.setFont(watchfont);
	watch4.setFont(watchfont);
	watch1.setForeground(Color.red);
	watch2.setForeground(Color.red);
	watch3.setForeground(Color.blue);
	watch4.setForeground(Color.blue);
	
	rise1 = new Label("0.5",Label.RIGHT);
	rise2 = new Label("fs",Label.LEFT);
	rise3 = new Label("Pulse Width",Label.RIGHT);
	rise4 = new Label("r",Label.CENTER);
	rise5 = new Label(" = ",Label.CENTER);
	rise1.setFont(labfont3);
	rise2.setFont(labfont3);
	rise3.setFont(labfont3);
	rise4.setFont(labfont2);
	rise4.setFont(labfont3);
	rise1.setForeground(Color.blue);
	rise2.setForeground(Color.blue);
	rise3.setForeground(Color.blue);
	rise4.setForeground(Color.blue);
	rise5.setForeground(Color.blue);
	tgc3 = new TransGraphCanvas2();
	ssc  = new StartStopClock();
	
	//Checkboxes
	cbxgroup = new CheckboxGroup();
	cbx1 = new Checkbox("Trace On",cbxgroup,false);
	cbx2 = new Checkbox("Trace Off",cbxgroup,true);
	cbx1.setBackground(tinta.darker());
	cbx2.setBackground(tinta.darker());
	
	cbgroup = new CheckboxGroup();
	cb1 = new Checkbox("Step",cbgroup,true);
	cb2 = new Checkbox("Pulse",cbgroup,false);
	cb1.setForeground(Color.red);
	cb2.setForeground(Color.blue);
	
        // CHANGE  =====================================================
        b2a = new Button("<");
        b2a.setBackground(bgcolor);        
        b2b = new Button(">");
        b2b.setBackground(bgcolor);
        b2a.setFont(labelfont);
        b2b.setFont(labelfont);
                
        add(b2a); add(b2b);
        //==============================================================
		
        
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax);//changes sleep time
        slider1.setBackground(Color.white);
	slider1.setValue(ScrollMax-SleepTime);
	add(slider1);
	slider1.setBounds(265,222,345,15);
	
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax2);//changes pulse time
        slider2.setBackground(Color.white);
	slider2.setValue(RampIndex);
	add(slider2);
	slider2.setBounds(265,33,345,15);
	slider2.setVisible(false);
        
        // CHANGE  =====================================================
        b2a.setBounds(265,33,14,15);
        b2b.setBounds(596,33,14,15);
        b2a.setVisible(false);
        b2b.setVisible(false);
        //==============================================================
        
	//Panel ps1 = new Panel();
	    //ps1.setBackground(Color.cyan);
	    //add(ps1);
	    //ps1.setBounds(264,221,347,22);
	Panel ps2 = new Panel();
	    ps2.setBackground(Color.black);
	    add(ps2);
	    //ps2.setBounds(263,220,349,24);
            ps2.setBounds(264,221,347,17);
	ps3 = new Panel();
	    //ps3.setBackground(Color.cyan);
	    //add(ps3);
	    //ps3.setBounds(264,32,347,22);
	ps4 = new Panel();
	    ps4.setBackground(Color.black);
	    add(ps4);
	    //ps4.setBounds(263,31,349,24);
            ps4.setBounds(264,32,347,17);
	
	ps3.setVisible(false);
        ps4.setVisible(false);
        ps2.setVisible(true);
        
        rise1.setVisible(false);
        rise2.setVisible(false);
        rise3.setVisible(false);
        rise5.setVisible(false);
        
        //add(titlelabel);
	add(titulolabel);
	//add(selectlabel);
	add(fast);
	add(slow);
	add(watch1);
	add(watch2);
	add(watch3);
	add(watch4);
	add(rise1);
	add(rise2);
	add(rise3);
	//add(rise4);
	//add(rise5);
	add(c1);
	
	add(tgc3);
	add(ssc);
	
	//add(cbx1);
	//add(cbx2);
	add(cb1);
	add(cb2);
	
	//titlelabel.setBounds(5,5,240,25);
	//c1.setBounds(305,30,195,25);
	//selectlabel.setBounds(200,30,100,25);
	slow.setBounds(265,242,80,12);  // width was 60
	//fast.setBounds(550,242,80,12);  //      "
	fast.setBounds(530,242,80,12);  //      "
	watch1.setBounds(340,258,50,18);
	watch2.setBounds(391,258,15,18);
	watch3.setBounds(460,258,60,18);
	watch4.setBounds(521,258,20,18);
	rise1.setBounds(470,10,50,20);
	rise2.setBounds(530,10,30,20);
	//rise3.setBounds(320,10,90,20);
	rise3.setBounds(320,10,110,20);
	rise4.setBounds(440,15,10,15);
	rise5.setBounds(460,10,10,15);
	Panel pwatch1 = new Panel();
	    pwatch1.setBackground(Color.white);
	    add(pwatch1);
	    pwatch1.setBounds(329,259,87,20);
	Panel pwatch2 = new Panel();
	    pwatch2.setBackground(Color.black);
	    //add(pwatch2);
	    pwatch2.setBounds(328,258,89,22);
	Panel pwatch3 = new Panel();
	    pwatch3.setBackground(Color.white);
	    add(pwatch3);
	    pwatch3.setBounds(449,259,103,20);
	Panel pwatch4 = new Panel();
	    pwatch4.setBackground(Color.black);
	    add(pwatch4);
	    //pwatch4.setBounds(448,258,105,22);
	Panel pwatch1b = new Panel();
	    pwatch1b.setBackground(Color.white);
	    add(pwatch1b);
	    pwatch1b.setBounds(327,257,91,24);
	Panel pwatch2b = new Panel();
	    pwatch2b.setBackground(Color.black);
	    add(pwatch2b);
	    pwatch2b.setBounds(326,256,93,26);
	Panel pwatch3b = new Panel();
	    pwatch3b.setBackground(Color.white);
	    add(pwatch3b);
	    pwatch3b.setBounds(447,257,107,24);
	Panel pwatch4b = new Panel();
	    pwatch4b.setBackground(Color.black);
	    add(pwatch4b);
	    pwatch4b.setBounds(446,256,109,26);
	    
        //titulolabel.setBounds(15,5,180,25);
        titulolabel.setBounds(10,5,165,25);
	c1.setBounds(15,30,150,25);
	selectlabel.setBounds(170,35,100,25);
	
	tgc3.setBounds(6,61,605,153);
		
	ssc.setBounds(6,221,250,58);
	cbx1.setBounds(216,226,78,24);
	cbx2.setBounds(216,250,78,24);
	cb1.setBounds(185,7,70,20);
	cb2.setBounds(185,33,70,20);
	
	
	Panel pgraph1 = new Panel();
	    pgraph1.setBackground(tinta2);
	    //pgraph1.setBackground(bgcolor);
	    add(pgraph1);
	    pgraph1.setBounds(6,61,605,153);
	
	Panel pgraph2 = new Panel();
	    pgraph2.setBackground(Color.black);
	    add(pgraph2);
	    pgraph2.setBounds(5,60,607,155);
	
	Panel pgraph3 = new Panel();
	    pgraph3.setBackground(Color.black);
	    //add(pgraph3);
	    pgraph3.setBounds(5,220,252,60);
	
	Panel pgraph4 = new Panel();
	    pgraph4.setBackground(Color.black);
	    //add(pgraph4);
	    pgraph4.setBounds(215,225,80,50);
	
	IsTronRunning = false;
	IsStepOn = true;
	
    //Listeners
	ssc.button1.addMouseListener(this);
	ssc.button2.addMouseListener(this);
	ssc.button3.addMouseListener(this);
	
	//cbx1.addItemListener(this);
	//cbx2.addItemListener(this);
	cb1.addItemListener(this);
	cb2.addItemListener(this);
	
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
                
        // CHANGE ======================================================
        b2a.addActionListener(this);
        b2b.addActionListener(this);
        //==============================================================
    }			
    
    
    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){;}
    
    public synchronized void mouseHandler(MouseEvent evt){
	if(evt.getSource() == ssc.button1){
	    cb1.setEnabled(false);
	    cb2.setEnabled(false);
	    slider2.setEnabled(false);
	    ssc.button2.setEnabled(true);
	    IsTronRunning = true;
	    if(tron == null){
		start();
	    }
	    else {
		//tron.resume();
	    }
		t_state.SwitchIsOpen = false;
		t_state.ignition();
	}
	if(evt.getSource() == ssc.button2){
	    IsTronRunning = false;
	    //tron.suspend();
	    tgc3.repaint();
	    cb1.setEnabled(false);
	    cb2.setEnabled(false);
	    slider2.setEnabled(false);
	}
	if(evt.getSource() == ssc.button3){
	    IsTronRunning = false;
	    //tron.suspend();
	    ssc.clockcanvas.setStatus(0,t_state.NTime,t_state.dtime);
	    ssc.clockcanvas.reset();
	    t_state.ctime=0;
	    t_state.ignition();
	    tgc3.plot(t_state.x,t_state.yclean);
	    tgc3.repaint();
	    watch1.setText("0.00");
	    watch3.setText("0.00");
	    cb1.setEnabled(true);
	    cb2.setEnabled(true);
	    slider2.setEnabled(true);
	    ssc.button2.setEnabled(false);
	    
    	}
	
    }
    
    public void actionPerformed(ActionEvent evt){
            if(evt.getSource()==b2a){
                slider2.setValue(slider2.getValue()-1);
                  if(IsStepOn){
                    //RiseTime=0.0;
                    //rise1.setText("0.0");
                    //rise2.setText("s");
                    //slider2.setValue(0);
                  }
                  else{  	    
                    if(slider2.getValue() == 0){
                        RiseTime=0.5e-15;
                        rise1.setText("0.5");
                        rise2.setText("fs");

                    }
                    if(slider2.getValue() < 2000 && slider2.getValue()>0.0){
                        RiseTime=((double)((slider2.getValue())/2.0))*1.0e-15;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e15,2));
                        rise2.setText("fs");

                    }
                    if(slider2.getValue() == 2000){
                        RiseTime=1.0e-12;
                        rise1.setText("1.0");
                        rise2.setText("ps");

                    }
                    if(slider2.getValue() > 2000 && slider2.getValue() < 3998 ){
                        RiseTime=((double)((slider2.getValue()-2000)/2.0)+1)*1.0e-12;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e12,2));
                        rise2.setText("ps");

                    }
                    if(slider2.getValue() == 3998){
                        RiseTime=1.0e-9;
                        rise1.setText("1.0");
                        rise2.setText("ns");

                    }
                    if(slider2.getValue() > 3998 && slider2.getValue() < 5996 ){
                        RiseTime=((double)((slider2.getValue()-3998)/2.0)+1)*1.0e-9;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e9,2));
                        rise2.setText("ns");

                    }
                    if(slider2.getValue() ==5996){
                        RiseTime=1.0e-6;
                        rise1.setText("1.0");
                        rise2.setText("\u00b5s");

                    }
                    if(slider2.getValue() > 5996 && slider2.getValue() < 7994 ){
                        RiseTime=((double)((slider2.getValue()-5996)/2.0)+1)*1.0e-6;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e6,2));
                        rise2.setText("\u00b5s");

                    }
                    if(slider2.getValue() == 7994){
                        RiseTime=1.0e-3;
                        rise1.setText("1.0");
                        rise2.setText("ms");

                    }
                    if(slider2.getValue() > 7994 && slider2.getValue() < 9992 ){
                        RiseTime=((double)((slider2.getValue()-7994)/2.0)+1)*1.0e-3;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e3,2));
                        rise2.setText("ms");

                    }
                    if(slider2.getValue() ==9992 ){
                        RiseTime=1.0;
                        rise1.setText("1.0");
                        rise2.setText("s");

                    }
                  }
            }
            else if(evt.getSource()==b2b){
                slider2.setValue(slider2.getValue()+1);
                if(IsStepOn){
                    //RiseTime=0.0;
                    //rise1.setText("0.0");
                    //rise2.setText("s");
                    //slider2.setValue(0);
                  }
                  else{  	    
                    if(slider2.getValue() == 0){
                        RiseTime=0.5e-15;
                        rise1.setText("0.5");
                        rise2.setText("fs");

                    }
                    if(slider2.getValue() < 2000 && slider2.getValue()>0.0){
                        RiseTime=((double)((slider2.getValue())/2.0))*1.0e-15;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e15,2));
                        rise2.setText("fs");

                    }
                    if(slider2.getValue() == 2000){
                        RiseTime=1.0e-12;
                        rise1.setText("1.0");
                        rise2.setText("ps");

                    }
                    if(slider2.getValue() > 2000 && slider2.getValue() < 3998 ){
                        RiseTime=((double)((slider2.getValue()-2000)/2.0)+1)*1.0e-12;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e12,2));
                        rise2.setText("ps");

                    }
                    if(slider2.getValue() == 3998){
                        RiseTime=1.0e-9;
                        rise1.setText("1.0");
                        rise2.setText("ns");

                    }
                    if(slider2.getValue() > 3998 && slider2.getValue() < 5996 ){
                        RiseTime=((double)((slider2.getValue()-3998)/2.0)+1)*1.0e-9;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e9,2));
                        rise2.setText("ns");

                    }
                    if(slider2.getValue() ==5996){
                        RiseTime=1.0e-6;
                        rise1.setText("1.0");
                        rise2.setText("\u00b5s");

                    }
                    if(slider2.getValue() > 5996 && slider2.getValue() < 7994 ){
                        RiseTime=((double)((slider2.getValue()-5996)/2.0)+1)*1.0e-6;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e6,2));
                        rise2.setText("\u00b5s");

                    }
                    if(slider2.getValue() == 7994){
                        RiseTime=1.0e-3;
                        rise1.setText("1.0");
                        rise2.setText("ms");

                    }
                    if(slider2.getValue() > 7994 && slider2.getValue() < 9992 ){
                        RiseTime=((double)((slider2.getValue()-7994)/2.0)+1)*1.0e-3;
                        rise1.setText(""+MaestroA.rounder(RiseTime*1.0e3,2));
                        rise2.setText("ms");

                    }
                    if(slider2.getValue() ==9992 ){
                        RiseTime=1.0;
                        rise1.setText("1.0");
                        rise2.setText("s");

                    }
                  }
            }
    }
	
    
    public void itemStateChanged(ItemEvent evt){
	if(evt.getSource() == cbx1){
	    tgc3.setTrace(true);
	}
	else if(evt.getSource() == cbx2){
	    tgc3.setTrace(false);
	}
	
	if(evt.getSource() == cb1){
            ps4.setVisible(false);
        }
        else if(evt.getSource() == cb2){
            ps4.setVisible(true);
        }
    }
    
    public synchronized void plotTransientVoltage(Trans_State state){
	//if(tron != null) tron.suspend();
	t_state = state;
	cbx2.setState(true);
	
	t_state.SwitchIsOpen = true;
	t_state.ignition();
	t_state.reset();
	
	ssc.clockcanvas.setStatus(0,state.NTime,state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 1;
	tgc3.reset();
	tgc3.setDynamics(false);
	t_state.ScanTransientVoltage();
		
	double test;
	test = state.getYRangeMax(1);
	
	
	if (test>=1.0 && test<1.0E3 || test==0.0){
	    tgc3.setTitle("   V(z,t)  [ V ] ");    
	}
	else if(test <1.0E6 && test >=1.0E3  ){
	    tgc3.setTitle("   V(z,t)  [ k V ] "); 
	   
	}
	else if(test>=1.0E6  ){
	    tgc3.setTitle("   V(z,t)  [ M V ] "); 
	   
	}
	else if(test<1.0 && test>=1.0E-3  ){
	    tgc3.setTitle("   V(z,t)  [ m V ] "); 
	   
	}
	else if(test<1.0E-3 && test>=1.0E-6  ){
	    tgc3.setTitle("   V(z,t)  [ \u00b5 V ] "); 
	    
	}
	else if(test<1.0E-6 && test>=1.0E-9  ){
	    tgc3.setTitle("   V(z,t)  [ n V ] "); 
	    
	}
	else if(test<1.0E-9 && test>=1.0E-12  ){
	    tgc3.setTitle("   V(z,t)  [ p V ] "); 
	    
	}
	else if(test<1.0E-12 && test>=1.0E-15 ){
	    tgc3.setTitle("   V(z,t)  [ f V ] "); 
	   
	}
	else if(test<1.0E-15 ){
	    tgc3.setTitle("   V(z,t)  [ a V ] "); 
	   
	}
		
	tgc3.setYRangeMax(t_state.getYRangeMax(1));
	tgc3.setValueMax(state.getYRangeMax(1));
	tgc3.setYRangeMin(-state.getYRangeMax(1));
	
	if(!IsStepOn){
	    tgc3.setPulse(true);
	}
	else{
	    tgc3.setPulse(false);
	}
	
	tgc3.plot(t_state.x,t_state.y1);
	tgc3.cleanUp();
	tgc3.repaint();
	
    } 
    
    public synchronized void plotTransientCurrent(Trans_State state){
	//if(tron != null) tron.suspend();
	t_state = state;
	cbx2.setState(true);
	t_state.reset();
	ssc.clockcanvas.setStatus(0,t_state.NTime,t_state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 2;
	
	t_state.SwitchIsOpen = true;
	t_state.ignition();
	tgc3.reset();
	tgc3.setDynamics(false);
	t_state.ScanTransientCurrent();
	
	//tgc3.setTitle("   I (x,t)   [ A ]"); 
	
	double test;
	test = state.getYRangeMax(2);
	
	
	if (test>=1.0 && test<1.0E3 || test==0.0){
	    tgc3.setTitle("   I(z,t)  [ A ] ");    
	}
	else if(test <1.0E6 && test >=1.0E3  ){
	    tgc3.setTitle("   I(z,t)  [ k A ] "); 
	   
	}
	else if(test>=1.0E6  ){
	    tgc3.setTitle("   I(z,t)  [ M A ] "); 
	   
	}
	else if(test<1.0 && test>=1.0E-3  ){
	    tgc3.setTitle("   I(z,t)  [ m A ] "); 
	   
	}
	else if(test<1.0E-3 && test>=1.0E-6  ){
	    tgc3.setTitle("   I(z,t)  [ \u00b5 A ] "); 
	    
	}
	else if(test<1.0E-6 && test>=1.0E-9  ){
	    tgc3.setTitle("   I(z,t)  [ n A ] "); 
	    
	}
	else if(test<1.0E-9 && test>=1.0E-12  ){
	    tgc3.setTitle("   I(z,t)  [ p A ] "); 
	    
	}
	else if(test<1.0E-12 && test>=1.0E-15 ){
	    tgc3.setTitle("   I(z,t)  [ f A ] "); 
	   
	}
	else if(test<1.0E-15 ){
	    tgc3.setTitle("   I(z,t)  [ a A ] "); 
	   
	} 
	      
	tgc3.setYRangeMax(t_state.getYRangeMax(2));
	tgc3.setValueMax(state.getYRangeMax(2));
	tgc3.setYRangeMin(-state.getYRangeMax(2));
	
	if(!IsStepOn){
	    tgc3.setPulse(true);
	}
	else{
	    tgc3.setPulse(false);
	}
	
	tgc3.plot(t_state.x,t_state.y1);
	tgc3.cleanUp();
    }	
    
    public synchronized void plotTransientPower(Trans_State state){
	//if(tron != null) tron.suspend();
	t_state = state;
	cbx2.setState(true);
	
	t_state.SwitchIsOpen = true;
	t_state.ignition();
	t_state.reset();
	
	ssc.clockcanvas.setStatus(0,t_state.NTime,t_state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 3;
	tgc3.reset();
	tgc3.setDynamics(false);
	t_state.ScanTransientPower();
		
	//tgc3.setTitle("   P(x,t)   [ W ]"); 
	
	double test;
	test = state.getYRangeMax(3);
	
	
	if (test>=1.0 && test<1.0E3 || test==0.0){
	    tgc3.setTitle("   P(z,t)  [ W ] ");    
	}
	else if(test <1.0E6 && test >=1.0E3  ){
	    tgc3.setTitle("   P(z,t)  [ k W ] "); 
	   
	}
	else if(test>=1.0E6  ){
	    tgc3.setTitle("   P(z,t)  [ M W ] "); 
	   
	}
	else if(test<1.0 && test>=1.0E-3  ){
	    tgc3.setTitle("   P(z,t)  [ m W ] "); 
	   
	}
	else if(test<1.0E-3 && test>=1.0E-6  ){
	    tgc3.setTitle("   P(z,t)  [ \u00b5 W ] "); 
	    
	}
	else if(test<1.0E-6 && test>=1.0E-9  ){
	    tgc3.setTitle("   P(z,t)  [ n W ] "); 
	    
	}
	else if(test<1.0E-9 && test>=1.0E-12  ){
	    tgc3.setTitle("   P(z,t)  [ p W ] "); 
	    
	}
	else if(test<1.0E-12 && test>=1.0E-15 ){
	    tgc3.setTitle("   P(z,t)  [ f W ] "); 
	   
	}
	else if(test<1.0E-15 ){
	    tgc3.setTitle("   P(z,t)  [ a W ] "); 
	   
	}    
	
	
	tgc3.setYRangeMax(t_state.getYRangeMax(3));
	tgc3.setValueMax(t_state.getYRangeMax(3));
	
	tgc3.setYRangeMin(-t_state.getYRangeMax(3));
	
	if(!IsStepOn){
	    tgc3.setPulse(true);
	}
	else{
	    tgc3.setPulse(false);
	}
	
	tgc3.plot(t_state.x,t_state.y1);
	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);
	    }
	    catch(InterruptedException e){e.printStackTrace();}
	}
    }
    
    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.y1);
	    break;
		
	    case 1:
		t_state.ScanTransientVoltage();
		tgc3.plot(t_state.x,t_state.y1);
		tgc3.setDynamics(true);
		
		watch1.setText(""+MaestroA.rounder((double)(t_state.ctime)/360,2));
		
		temp = (double)(t_state.ctime)/360*t_state.T_period;
		if(temp >= 1.0){
		    watch3.setText(""+MaestroA.rounder(temp,4));
		    watch4.setText("s");
		}
		else if(temp < 1.0 && temp >= 1.0E-3){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E3,2));
		    watch4.setText("ms");
		}
		else if(temp < 1.0E-3 && temp >= 1.0E-6){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E6,2));
		    watch4.setText("\u00b5s");
		}
		else if(temp < 1.0E-6 && temp >= 1.0E-9){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E9,2));		
		    watch4.setText("ns");
		}
		else if(temp < 1.0E-9 && temp >= 1.0E-12){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E12,2));
		    watch4.setText("ps");
		}
		else if(temp < 1.0E-12 ){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E15,4));
		    watch4.setText("fs");
		}
		    
	    break;
	    
	    case 2:
		t_state.ScanTransientCurrent();
		tgc3.plot(t_state.x,t_state.y1);
		tgc3.setDynamics(true);
		
		watch1.setText(""+MaestroA.rounder((double)(t_state.ctime)/360,2));
		
		temp = (double)(t_state.ctime)/360*t_state.T_period;
		if(temp >= 1.0){
		    watch3.setText(""+MaestroA.rounder(temp,4));
		    watch4.setText("s");
		}
		else if(temp < 1.0 && temp >= 1.0E-3){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E3,2));
		    watch4.setText("ms");
		}
		else if(temp < 1.0E-3 && temp >= 1.0E-6){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E6,2));
		    watch4.setText("\u00b5s");
		}
		else if(temp < 1.0E-6 && temp >= 1.0E-9){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E9,2));		
		    watch4.setText("ns");
		}
		else if(temp < 1.0E-9 && temp >= 1.0E-12){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E12,2));
		    watch4.setText("ps");
		}
		else if(temp < 1.0E-12 ){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E15,4));
		    watch4.setText("fs");
		}
		
		
	    break;
	    
	    case 3:
		t_state.ScanTransientPower();
		tgc3.plot(t_state.x,t_state.y1);
		tgc3.setDynamics(true);
		
		watch1.setText(""+MaestroA.rounder((double)(t_state.ctime)/360,2));
		
		temp = (double)(t_state.ctime)/360*t_state.T_period;
		if(temp >= 1.0){
		    watch3.setText(""+MaestroA.rounder(temp,4));
		    watch4.setText("s");
		}
		else if(temp < 1.0 && temp >= 1.0E-3){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E3,2));
		    watch4.setText("ms");
		}
		else if(temp < 1.0E-3 && temp >= 1.0E-6){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E6,2));
		    watch4.setText("\u00b5s");
		}
		else if(temp < 1.0E-6 && temp >= 1.0E-9){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E9,2));		
		    watch4.setText("ns");
		}
		else if(temp < 1.0E-9 && temp >= 1.0E-12){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E12,2));
		    watch4.setText("ps");
		}
		else if(temp < 1.0E-12 ){
		    watch3.setText(""+MaestroA.rounder(temp*1.0E15,4));
		    watch4.setText("fs");
		}
		
		
	    break; 
	}
    }
    
    public synchronized void setSleepTime(int SleepTime){
	this.SleepTime = SleepTime;
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){//NEED HERE
	
	if(evt.getSource()==slider1){
	    SleepTime  = ScrollMax-slider1.getValue();  
	}
	
	if(evt.getSource()==slider2){
	  if(IsStepOn){
		//RiseTime=0.0;
		//rise1.setText("0.0");
		//rise2.setText("s");
		//slider2.setValue(0);
	  }
	  else{  	    
	    if(slider2.getValue() == 0){
		RiseTime=0.5e-15;
		rise1.setText("0.5");
		rise2.setText("fs");
		
	    }
	    if(slider2.getValue() < 2000 && slider2.getValue()>0.0){
		RiseTime=((double)((slider2.getValue())/2.0))*1.0e-15;
		rise1.setText(""+MaestroA.rounder(RiseTime*1.0e15,2));
		rise2.setText("fs");
		
	    }
	    if(slider2.getValue() == 2000){
		RiseTime=1.0e-12;
		rise1.setText("1.0");
		rise2.setText("ps");
		
	    }
	    if(slider2.getValue() > 2000 && slider2.getValue() < 3998 ){
		RiseTime=((double)((slider2.getValue()-2000)/2.0)+1)*1.0e-12;
		rise1.setText(""+MaestroA.rounder(RiseTime*1.0e12,2));
		rise2.setText("ps");
		
	    }
	    if(slider2.getValue() == 3998){
		RiseTime=1.0e-9;
		rise1.setText("1.0");
		rise2.setText("ns");
		
	    }
	    if(slider2.getValue() > 3998 && slider2.getValue() < 5996 ){
		RiseTime=((double)((slider2.getValue()-3998)/2.0)+1)*1.0e-9;
		rise1.setText(""+MaestroA.rounder(RiseTime*1.0e9,2));
		rise2.setText("ns");
		
	    }
	    if(slider2.getValue() ==5996){
		RiseTime=1.0e-6;
		rise1.setText("1.0");
		rise2.setText("\u00b5s");
		
	    }
	    if(slider2.getValue() > 5996 && slider2.getValue() < 7994 ){
		RiseTime=((double)((slider2.getValue()-5996)/2.0)+1)*1.0e-6;
		rise1.setText(""+MaestroA.rounder(RiseTime*1.0e6,2));
		rise2.setText("\u00b5s");
		
	    }
	    if(slider2.getValue() == 7994){
		RiseTime=1.0e-3;
		rise1.setText("1.0");
		rise2.setText("ms");
		
	    }
	    if(slider2.getValue() > 7994 && slider2.getValue() < 9992 ){
		RiseTime=((double)((slider2.getValue()-7994)/2.0)+1)*1.0e-3;
		rise1.setText(""+MaestroA.rounder(RiseTime*1.0e3,2));
		rise2.setText("ms");
		
	    }
	    if(slider2.getValue() ==9992 ){
		RiseTime=1.0;
		rise1.setText("1.0");
		rise2.setText("s");
		
	    }
	  }
	    
	}
    }
    
    public synchronized void setRiseTime(double RiseTime){
	this.RiseTime = RiseTime;
    }
    
}
