//QuarterLambda.java

import java.awt.*;
import java.awt.event.*;
//import java.applet.*;
import java.lang.*;
import java.io.*;
import java.awt.font.*;
import java.net.URL;
import java.text.*;
import java.util.Map;
import java.util.Hashtable;
import java.util.jar.Attributes;


public class QuarterLambda extends Frame implements ActionListener, ItemListener, AdjustmentListener, WindowListener{
    Instructions instructions;
    Panel instrFrame, instrFrame1, instrFrame2;
    //About about;
    TitlePanel titlepanel;
    ControlPanel controlpanel;
    InputPanel inputpanel;
    SmithPanel smithpanel;
    SmithPanel2 smithpanel2;
    NewGraphCanvas newgraph;
    NewGraphCanvasB newgraphB;

    CircuitCanvas circuitcanvas;
    StateVars state;
    StepCanvas stepcanvas;
    Panel psmith, psmith2;
    Image image;
    line line;
    PhiControls phicontrols;
    public Label labtitle;
    Font ttfFont, phiFont, italicFont;
    final Font labfonte = new Font("Sanserif",Font.BOLD,15);
    
    private static final boolean useOldFonts = true;    
    
    /**
     * Initializes the applet.  You never need to call this directly; it is
     * called automatically by the system once the applet is created.
     */
    
    public static void main(String[] args){
        int xmove = 20;
        int ymove = 41;
        QuarterLambda f = new QuarterLambda();
        f.setSize(783+xmove,600+ymove);
        f.setVisible(true);
        //f.setLayout(new FlowLayout());
        f.setLayout(null);
    } 
    
    public QuarterLambda() {
	setLayout(null);
        String lcOSName = System.getProperty("os.name").toLowerCase();
        boolean MAC_OS_X = lcOSName.startsWith("mac os x");
        //System.out.println(lcOSName);
        int xmove = 10;
        int ymove = 34;
        
        if (!useOldFonts) {
            //getUlabyFonts();
            state = new StateVars();
            state.ttfFont = ttfFont;
            state.sanSerifFont = ttfFont;
            state.serifFont = ttfFont;
            state.symbolFont = phiFont;
            state.italicFont = phiFont;
            MaestroG.serifFont = ttfFont;
            MaestroG.sanSerifFont = ttfFont;
            MaestroG.italicFont = italicFont;
            MaestroG.useBiggerFont = true;
            TheFonts.setNewFonts(ttfFont, phiFont, italicFont);
        } else {
            state = new StateVars();
            state.ttfFont = new Font("SanSerif",Font.PLAIN,12);
            state.sanSerifFont = new Font("SanSerif",Font.PLAIN,12);
            state.serifFont = new Font("Serif",Font.PLAIN,12);
            state.symbolFont = new Font("Symbol",Font.PLAIN,12);
            state.italicFont = new Font("Serif",Font.ITALIC,12);
            MaestroG.serifFont = state.serifFont;
            MaestroG.sanSerifFont = state.sanSerifFont;
            MaestroG.italicFont = state.italicFont;
            MaestroG.useBiggerFont = false;
            TheFonts.setOldFonts();
        }
	
        //titlepanel = new TitlePanel(subtitleFont,"Module 2.7","Quarter-Wavelength Transformer");
        titlepanel = new TitlePanel(state.ttfFont.deriveFont(Font.BOLD,13f),
                                    "Module 2.7 Tutorial","Quarter-Wavelength Transformer",
                                    true);  // was MAC_OS_X
	
	titlepanel.setBounds(10+xmove,10+ymove,355,40);
	//add(titlepanel);
	
	controlpanel = new ControlPanel();
	controlpanel.setBounds(11+xmove,280+ymove,353,37);
	//add(controlpanel);
	controlpanel.b1.setEnabled(false);
	controlpanel.b3.setEnabled(false);
	
	inputpanel = new InputPanel(state);
	inputpanel.setBounds(11+xmove,324+ymove,353,262);
	//add(inputpanel);
	
	smithpanel = new SmithPanel();
	
	smithpanel.setZin(false);
	smithpanel.setYin(false);
	smithpanel.setZL(false);
	smithpanel.setYL(false);
	smithpanel.set_swr_circle(false);
	smithpanel.set_swr_line(false);
	smithpanel.setZL(state.getZL(),state.getZ0());
	smithpanel.setZin(state.getZin(),state.getZ0());
	smithpanel.setBounds(371+xmove,11+ymove,401,575);
	//add(smithpanel);
        
        smithpanel.setVisible(false);   // NEW ----------------------
	
	smithpanel2 = new SmithPanel2();
	
	smithpanel2.setZin(false);
	smithpanel2.setYin(false);
	smithpanel2.setZL(false);
	smithpanel2.setYL(false);
	smithpanel2.set_swr_circle(false);
	smithpanel2.set_swr_line(false);
	smithpanel2.setZL(state.getZL(),state.getZ0());
	smithpanel2.setZin(state.getZin(),state.getZ0());
	//smithpanel2.setBounds(321,11,568,655);
        smithpanel2.setBounds(321+xmove,11+ymove,568,673);
	smithpanel2.setVisible(false);
	//add(smithpanel2);
	
	newgraph = new NewGraphCanvas(state);
	newgraph.setBounds(371+xmove,11+ymove,401,276);
	newgraph.setVisible(false);
	//add(newgraph);
	
	newgraphB = new NewGraphCanvasB(state);
	newgraphB.setBounds(371+xmove,310+ymove,401,276);
	newgraphB.setVisible(false);
	//add(newgraphB);
	
	phicontrols = new PhiControls(state);
	phicontrols.setBounds(370+xmove,288+ymove,403,21);
	phicontrols.setVisible(false);
	//add(phicontrols);

	
	circuitcanvas = new CircuitCanvas(state);
	circuitcanvas.setBounds(10+xmove,55+ymove,355,219);
	//add(circuitcanvas);
	
	stepcanvas = new StepCanvas(state);
	stepcanvas.setBounds(11+xmove,324+ymove,353,262);
	//add(stepcanvas);
	stepcanvas.setVisible(false);
	
        //about = new About();
	//about.setBounds(9,9,765,579);
	//add(about);
        //about.setVisible(false);

        instructions = new Instructions(state);
	//instructions.setBounds(9,9,765,579);
        instructions.setBounds(11+xmove,56+ymove,761,531);
        //instructions.setVisible(false);
        instrFrame = new Panel();
        instrFrame.setBackground(Color.black);
        instrFrame.setBounds(10+xmove,55+ymove,763,533);

        labtitle = new Label("Design Procedure",Label.LEFT);
        labtitle.setFont(labfonte);
	labtitle.setForeground(Color.red);
        labtitle.setBackground(new Color(255,255,245));
        labtitle.setBounds(401+xmove,21+ymove,200,30);
        labtitle.setVisible(true);
        
        instrFrame1 = new Panel();
        instrFrame1.setBackground(new Color(255,255,245));
        instrFrame1.setBounds(371+xmove,11+ymove,401,56);
        
	Panel pgraph = new Panel();
	    pgraph.setBackground(Color.black);
	    //add(pgraph);
	    pgraph.setBounds(10+xmove,323+ymove,355,264);
	
	Panel pgraphb = new Panel();
	    pgraphb.setBackground(Color.black);
	    //add(pgraphb);
	    pgraphb.setBounds(10+xmove,279+ymove,355,39);
    
	psmith = new Panel();
	    psmith.setBackground(Color.black);
	    //add(psmith);
	    psmith.setBounds(370+xmove,10+ymove,403,577);
	    
	psmith2 = new Panel();
	    psmith2.setBackground(Color.black);
	    //add(psmith2);
	    psmith2.setBounds(320+xmove,10+ymove,570,675);
	    psmith2.setVisible(false);
	    
	Panel plot1 = new Panel();
	    plot1.setBackground(Color.black);
	    //add(plot1);
	    plot1.setBounds(370+xmove,10+ymove,403,278);
	    plot1.setVisible(true);
	    
	Panel plot2 = new Panel();
	    plot2.setBackground(Color.black);
	    //add(plot2);
	    plot2.setBounds(370+xmove,309+ymove,403,278);
	    plot2.setVisible(true);

	
	Panel ptitle = new Panel();
	    ptitle.setBackground(Color.black);
	    //add(ptitle);
	    ptitle.setBounds(10+xmove,10+ymove,355,40);
	
	Panel p0 = new Panel();
	    p0.setBackground(Color.cyan);
	    //add(p0);
	    p0.setBounds(3+xmove,3+ymove,777,591);
	    
	Panel p00 = new Panel();
	    p00.setBackground(Color.black);
	    //add(p00);
	    p00.setBounds(xmove,ymove,783,597);
	
	Panel p000 = new Panel();
	    p000.setBackground(Color.white);
	    //add(p000);
	    p000.setBounds(xmove,ymove,890,667);
	    
            //if(state.this_year > line.magicyear || (state.this_year == line.magicyear && state.this_month > line.magicmonth)){
            //    state.LicenseExpired = true;
            //}
            //else
            //{   
            add(labtitle);
            add(instructions);
            add(instrFrame1);
            add(instrFrame);
            
	    add(titlepanel);
	    add(controlpanel);
	    add(inputpanel);
	    add(smithpanel);
	    // CHANGE - DO NOT ADD - MAC DOES NOT HANDLE WELL setVisible =======
            //add(smithpanel2);
            //==================================================================
	    add(newgraph);
	    add(newgraphB);
	    add(circuitcanvas);
	    add(stepcanvas);
	    add(pgraph);
	    add(pgraphb);
	    add(phicontrols);
	    add(pgraph);
	    add(pgraphb);
	    add(psmith);
	    add(psmith2);
	    add(plot1);
	    add(plot2);
	    add(ptitle);
	    add(p0);
	    add(p00);
	    state.LicenseExpired = false;
            //}
		
	//Listeners
        this.addWindowListener(this);
        instructions.bupdate.addActionListener(this);
        //about.bupdate.addActionListener(this);
	inputpanel.b1.addActionListener(this);
	controlpanel.b1.addActionListener(this);
	controlpanel.b2.addActionListener(this);
	controlpanel.b3.addActionListener(this);
	
    //smithpanel.small.addItemListener(this);
    //smithpanel2.large.addItemListener(this);
    //--------------------------------------------- NEW
    smithpanel.small.addActionListener(this);
    smithpanel.colore.addActionListener(this);
    smithpanel2.colore.addActionListener(this); 
    smithpanel2.large.addActionListener(this);
   //--------------------------------------------------
    smithpanel.scon.imp.addItemListener(this);
    smithpanel.scon.adm.addItemListener(this);
    
    smithpanel2.scon.imp.addItemListener(this);
    smithpanel2.scon.adm.addItemListener(this);
    phicontrols.slider1.addAdjustmentListener(this);
    inputpanel.slider.addAdjustmentListener(this);

    }

    /*
    public void paint(Graphics g){
	Font licensefont = new Font("SanSerif",Font.BOLD,18);
	g.setColor(Color.white);	
	g.fillRect(0,0,945,765);
	g.drawImage(image,5,545,this);	
	
	if(state.LicenseExpired){
	g.setFont(licensefont);
	
	g.setColor(Color.red);
	g.drawString("The software license has expired.", 50, 200);
	g.setColor(Color.black);
	g.drawString("Obtain updated files from www.amanogawa.com", 50, 230);
	}
    }
    */
    
    
    public void paint(Graphics g){
    
    Font licensefont = new Font("SanSerif",Font.BOLD,18); 
    Font fonto = new Font("SanSerif",Font.PLAIN,12);
    Font fontc = new Font("SanSerif",Font.BOLD,16);
    
	Graphics2D g2d = (Graphics2D)g;
        /*
        g.setColor(Color.white);	
	g.fillRect(0,0,890,687);  	
		
	if(state.LicenseExpired){
	    g.setFont(licensefont);
	
	    g.setColor(Color.red);
	    g.drawString("The software license has expired.", 50, 200);
	    g.setColor(Color.black);
	    g.drawString("Obtain updated files from www.amanogawa.com", 50, 230);
	} 
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        int annonuovo = 2006;
        int anno = 2006;
        
        if(anno > state.this_year){
            annonuovo = anno;
        }
        else{
            annonuovo = state.this_year;
        }
        g.setColor(Color.red.darker());
        g.setFont(fontc);
        g.drawString("\u00a9",10,562);
        g.setFont(fonto);
        g.drawString("Amanogawa, "+annonuovo+" - All Rights Reserved",30,560);
        */
    }

    /**
     * Called to start the applet.  You never need to call this directly; it
     * is called when the applet's document is visited.
     */
    public void start() {
    }

    /**
     * Called to stop the applet.  This is called when the applet's document is
     * no longer on the screen.  It is guaranteed to be called before destroy()
     * is called.  You never need to call this method directly
     */
    public void stop() {
    }

    /**
     * Cleans up whatever resources are being held.  If the applet is active
     * it is stopped.
     */
    public void destroy() {
    }
    
    public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
    }
    
    public void windowOpened(WindowEvent evt){}
    
    public void windowIconified(WindowEvent evt){}
    
    public void windowClosed(WindowEvent evt){}
    
    public void windowDeiconified(WindowEvent evt){}
    
    public void windowActivated(WindowEvent evt){}
    
    public void windowDeactivated(WindowEvent evt){}
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
            if(evt.getSource()==inputpanel.slider){
                inputpanel.repaint();
            }

	    if(evt.getSource()==phicontrols.slider1){
		newgraph.setLabelAxis(true);
		newgraph.setYRangeMax(1.0);
		newgraph.setYRangeMin(0.0);
		newgraph.plotZeroLine(false);
		newgraph.plotRefPoint(true);
		newgraph.setLabels("Solution # 1",""+state.frequency,"  f [ Hz ]","");
		newgraph.setBand(state.F1,state.F2,state.fpos1,state.fpos2,state.BigBand1);
		newgraph.setRefPoint(state.frequency_scan, phicontrols.slider1.getValue());
		
		newgraph.plot(state.x_freq,state.F_array);
		
		newgraphB.setLabelAxis(true);
		newgraphB.setYRangeMax(1.0);
		newgraphB.setYRangeMin(0.0);
		newgraphB.plotZeroLine(false);
		newgraphB.plotRefPoint(true);
		newgraphB.setLabels("Solution # 2",""+state.frequency,"  f [ Hz ]","");
		newgraphB.setBand(state.G1,state.G2,state.gpos1,state.gpos2,state.BigBand2);
		newgraphB.setRefPoint(state.frequency_scan, phicontrols.slider1.getValue());
		
		newgraphB.plot(state.x_freq,state.G_array);
		
		newgraph.repaint();
		newgraphB.repaint();
		
		state.scanpoint = phicontrols.slider1.getValue();
	    }
    }

    
    public void actionPerformed(ActionEvent evt){
	if(evt.getSource()==inputpanel.b1){
	    state.ignition2();
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    	    
	    circuitcanvas.repaint();
	    phicontrols.slider1.setValue(state.NPoints/2);
	    state.frequency_scan = state.frequency;
	    state.ignition2();
	    
	    newgraph.ignition();
		newgraphB.ignition();
		
		newgraph.setLabelAxis(true);
		newgraph.setYRangeMax(1.0);
		newgraph.setYRangeMin(0.0);
		newgraph.plotZeroLine(false);
		newgraph.plotRefPoint(true);
		newgraph.setLabels("Solution # 1",""+state.frequency,"  f [ Hz ]","");
		newgraph.setBand(state.F1,state.F2,state.fpos1,state.fpos2,state.BigBand1);
		newgraph.setRefPoint(state.frequency_scan, phicontrols.slider1.getValue());
		
		newgraph.plot(state.x_freq,state.F_array);
		
		newgraphB.setLabelAxis(true);
		newgraphB.setYRangeMax(1.0);
		newgraphB.setYRangeMin(0.0);
		newgraphB.plotZeroLine(false);
		newgraphB.plotRefPoint(true);
		newgraphB.setLabels("Solution # 2",""+state.frequency,"  f [ Hz ]","");
		newgraphB.setBand(state.G1,state.G2,state.gpos1,state.gpos2,state.BigBand2);
		newgraphB.setRefPoint(state.frequency_scan, phicontrols.slider1.getValue());
		
		newgraphB.plot(state.x_freq,state.G_array);
		
		newgraph.repaint();
		newgraphB.repaint();

	    
	    //smithpanel.sc.repaint();
	    
	}
	else if(evt.getSource()==controlpanel.b2){//Next Step
	    if(state.getStepNumber()==0 && inputpanel.isVisible()){
		state.ignition2();
		inputpanel.setVisible(false);
		stepcanvas.setVisible(true);
		stepcanvas.repaint();
		controlpanel.b1.setEnabled(true);
		controlpanel.b3.setEnabled(true);
		
		runStep(0);
	    }
	    else if(state.getStepNumber()==6){ 
		state.setZin(state.ZL);
                state.setxpos(0.0);
                smithpanel.setxpos(0.0);
                smithpanel.setxpos(0.0);
		state.ignition2();
		smithpanel.setZin(state.ZL,state.Z0);
		smithpanel2.setZin(state.ZL,state.Z0);
		
		smithpanel.sm.repaint();
		smithpanel2.sm.repaint();
                smithpanel.sc.repaint();
		smithpanel2.sc.repaint();
		state.StepIncrement();
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
            // CHANGE - ADD LINES BELOW ========================================
	    else if(state.getStepNumber()==13){
                controlpanel.b2.setEnabled(false);
            }
            //==================================================================
	    else{
		state.StepIncrement();
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
	}
	else if(evt.getSource()==controlpanel.b1){//Previous Step 
		
		state.setZin(state.ZL);
		state.ignition2();
		smithpanel.setZin(state.ZL,state.Z0);
		smithpanel2.setZin(state.ZL,state.Z0);
		
		smithpanel.sm.repaint();
		smithpanel2.sm.repaint();
		controlpanel.b2.setLabel("Next Step");
                // CHANGE - ADD LINE BELOW =====================================
                controlpanel.b2.setEnabled(true);
                //==============================================================
		
	    if(state.getStepNumber()==0 && !inputpanel.isVisible()){
		stepcanvas.setVisible(false);
		inputpanel.setVisible(true);
		smithpanel.setZL(false);
		smithpanel.setYL(false);
		smithpanel.set_swr_line(false);
		smithpanel.set_swr_circle(false);
		smithpanel.sc.repaint();
		
		smithpanel2.setZL(false);
		smithpanel2.setYL(false);
		smithpanel2.set_swr_line(false);
		smithpanel2.set_swr_circle(false);
		smithpanel2.sc.repaint();
		
		controlpanel.b1.setEnabled(false);
		controlpanel.b3.setEnabled(false);
	    }
	    else if(state.getStepNumber()==1 ){
		state.setStepNumber(0);
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(false);
		smithpanel.setZin(false);
		smithpanel.setYin(false);
		smithpanel.setZL(true);
		smithpanel.setYL(true);
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(false);
		smithpanel2.setZin(false);
		smithpanel2.setYin(false);
		smithpanel2.setZL(true);
		smithpanel2.setYL(true);
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		 
		
		runStep(state.getStepNumber());
	    }
	    else if(state.getStepNumber()==2 || state.getStepNumber()==3 ){
		state.setStepNumber(1);
		//state.setZ0(state.getZ0old());
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		//smithpanel.sc.OverRide=false;
		//smithpanel.sc.repaint();
		//smithpanel2.sc.OverRide=false;
		//smithpanel2.sc.repaint();
		
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		smithpanel.setZin(true);
		smithpanel.setYin(true);
		smithpanel.setZL(true);
		smithpanel.setYL(true);
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		smithpanel2.setZin(true);
		smithpanel2.setYin(true);
		smithpanel2.setZL(true);
		smithpanel2.setYL(true);
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		 
		
		runStep(state.getStepNumber());
	    }
	    else if(state.getStepNumber()==4 ){
		state.setStepNumber(1);
		//state.setZ0(state.getZ0old());
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		//smithpanel.sc.OverRide=false;
		//smithpanel.sc.repaint();
		//smithpanel2.sc.OverRide=false;
		//smithpanel2.sc.repaint();
		
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		smithpanel.setZin(true);
		smithpanel.setYin(true);
		smithpanel.setZL(true);
		smithpanel.setYL(true);
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		smithpanel2.setZin(true);
		smithpanel2.setYin(true);
		smithpanel2.setZL(true);
		smithpanel2.setYL(true);
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		 
		
		runStep(state.getStepNumber());
	    }
	    
	    else if(state.getStepNumber()==5 ){
		state.setStepNumber(1);
		//state.setZ0(state.getZ0old());
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		
		//smithpanel.sc.OverRide=false;
		//smithpanel.sc.repaint();
		
		//smithpanel2.sc.OverRide=false;
		//smithpanel2.sc.repaint();
		
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		smithpanel.setZin(true);
		smithpanel.setYin(true);
		smithpanel.setZL(true);
		smithpanel.setYL(true);
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		smithpanel2.setZin(true);
		smithpanel2.setYin(true);
		smithpanel2.setZL(true);
		smithpanel2.setYL(true);
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		 
		
		runStep(state.getStepNumber());
	    }
	    
	    else if(state.getStepNumber()==6 ){
		state.setStepNumber(1);
		//state.setZ0(state.getZ0old());
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		
		//smithpanel.sc.OverRide=false;
		//smithpanel.sc.repaint();
		//smithpanel2.sc.OverRide=false;
		//smithpanel2.sc.repaint();
		
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		smithpanel.setZin(true);
		smithpanel.setYin(true);
		smithpanel.setZL(true);
		smithpanel.setYL(true);
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		smithpanel2.setZin(true);
		smithpanel2.setYin(true);
		smithpanel2.setZL(true);
		smithpanel2.setYL(true);
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		 
		
		runStep(state.getStepNumber());
	    }
	    
	    else if(state.getStepNumber()==7){
		state.setStepNumber(1);
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		
		//smithpanel.sc.OverRide=false;
		//smithpanel.sc.repaint();
		//smithpanel2.sc.OverRide=false;
		//smithpanel2.sc.repaint();
		
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		smithpanel.setZin(true);
		smithpanel.setYin(true);
		smithpanel.setZL(true);
		smithpanel.setYL(true);
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		smithpanel2.setZin(true);
		smithpanel2.setYin(true);
		smithpanel2.setZL(true);
		smithpanel2.setYL(true);
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		 
		
		runStep(state.getStepNumber());
	    }
	    else if(state.getStepNumber()==8){
		state.setStepNumber(7);
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		
		runStep(state.getStepNumber());
	    }
	    else if(state.getStepNumber()==9){
		state.setStepNumber(7);
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		
		runStep(state.getStepNumber());
	    }
	    
	    else if(state.getStepNumber()==10 ){
		state.setStepNumber(7);
		//state.setZ0(state.getZ0old());
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		
		runStep(state.getStepNumber());
	    }
	    
	    else if(state.getStepNumber()==11){
		state.setStepNumber(7);
		//state.setZ0(state.getZ0old());
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		
		runStep(state.getStepNumber());
	    }
	    
	    else if(state.getStepNumber()==12 ){
		state.setStepNumber(7);
		//state.setZ0(state.getZ0old());
		state.setZin(state.getZL());
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		state.setxpos(0.0);
		smithpanel.setxpos(0.0);
		smithpanel2.setxpos(0.0);
		
		circuitcanvas.repaint(); 
		stepcanvas.repaint();
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		
		runStep(state.getStepNumber());
	    }
	    
	    else if(state.getStepNumber()==13){
		
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(false);
		smithpanel.setZin(true);
		smithpanel.setYin(true);
		smithpanel.setZL(true);
		smithpanel.setYL(true);
		smithpanel.sc.OverRide=false;
		smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(false);
		smithpanel2.setZin(true);
		smithpanel2.setYin(true);
		smithpanel2.setZL(true);
		smithpanel2.setYL(true);
		smithpanel2.sc.OverRide=false;
		smithpanel2.sc.repaint();
		 
		state.setStepNumber(7);
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
	    else{
		state.StepDecrement();
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
	}
	else if(evt.getSource()==controlpanel.b3){//Start Over 
	    
	    state.setZin(state.ZL);
	    state.ignition2();
	    smithpanel.setZin(state.ZL,state.Z0);
	    smithpanel2.setZin(state.ZL,state.Z0);
		
	    smithpanel.sm.repaint();
	    smithpanel2.sm.repaint();
		
	    controlpanel.setEnabled(true);
	    state.setStepNumber(0);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
	    inputpanel.setVisible(true);
	    //state.setZ0(state.getZ0old());
	    //smithpanel.sc.repaint();
	    smithpanel.setZL(false);
	    smithpanel.setYL(false);
	    smithpanel.setZin(false);
	    smithpanel.setYin(false);
	    smithpanel.set_swr_line(false);
	    smithpanel.set_swr_circle(false);
	    
	    //smithpanel2.sc.repaint();
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    
	    
	    state.setDrawTransformer1(false);
	    state.setDrawTransformer2(false);
	    state.setxpos(0.0);
	    smithpanel.setxpos(0.0);
	    smithpanel2.setxpos(0.0);
	    
	    circuitcanvas.repaint(); 
	    
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel.sc.repaint();
	    
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    smithpanel2.sc.repaint();
	    
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    
	    //state.ignition();
	    circuitcanvas.repaint();
	    smithpanel.sc.repaint();
	    smithpanel2.sc.repaint();
	    
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }

		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);
	}
        
        
        // controls for size and colors of Smith Charts
        
        //----------------------------------NEW
        if(evt.getSource()==smithpanel.small){
            //about.setVisible(true);
            instructions.setVisible(true);
            instrFrame.setVisible(true);
            instrFrame1.setVisible(true);
            
            smithpanel.setVisible(false);
            labtitle.setVisible(true);
        }
        
        if(evt.getSource()==instructions.bupdate){
            //about.setVisible(false);
            instructions.setVisible(false);
            instrFrame.setVisible(false);
            instrFrame1.setVisible(false);
            
            smithpanel.setVisible(true);
            labtitle.setVisible(false);
        }
        
        /*
        if(evt.getSource()==smithpanel.small){
            
            state.IsBig = true;
		smithpanel.setVisible(false);
		psmith.setVisible(false);
		smithpanel2.setVisible(true);
		psmith2.setVisible(true);
		//smithpanel2.large.setState(false);
		
		//---------------------
		
	    controlpanel.setEnabled(true);
	    state.setStepNumber(0);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
	    inputpanel.setVisible(true);
	    //state.setZ0(state.getZ0old());
	    //smithpanel.sc.repaint();
	    smithpanel.setZL(false);
	    smithpanel.setYL(false);
	    smithpanel.setZin(false);
	    smithpanel.setYin(false);
	    smithpanel.set_swr_line(false);
	    smithpanel.set_swr_circle(false);
	    
	    //smithpanel2.sc.repaint();
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    
	    
	    state.setDrawTransformer1(false);
	    state.setDrawTransformer2(false);
	    state.setxpos(0.0);
	    smithpanel.setxpos(0.0);
	    smithpanel2.setxpos(0.0);
	    
	    circuitcanvas.repaint(); 
	    
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel.sc.repaint();
	    
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    smithpanel2.sc.repaint();
	    
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    
	    //state.ignition();
	    circuitcanvas.repaint();
	    smithpanel.sc.repaint();
	    smithpanel2.sc.repaint();
	
	}
        */
        
//-------------------------------------------------NEW        
        if(evt.getSource()==smithpanel.colore){
            if(smithpanel.colore_dark){
                smithpanel.colore_dark=false;
                smithpanel.sc.setColoreDark(false);
                smithpanel2.colore_dark=false;
                smithpanel2.sc.setColoreDark(false);
            }
            else{
                smithpanel.colore_dark=true;
                smithpanel.sc.setColoreDark(true);
                smithpanel2.colore_dark=true;
                smithpanel2.sc.setColoreDark(true);
            }
        }
        
        
	if(evt.getSource()==smithpanel2.colore){
            if(smithpanel2.colore_dark){
                smithpanel2.colore_dark=false;
                smithpanel2.sc.setColoreDark(false);
                smithpanel.colore_dark=false;
                smithpanel.sc.setColoreDark(false);
            }
            else{
                smithpanel2.colore_dark=true;
                smithpanel2.sc.setColoreDark(true);
                smithpanel.colore_dark=true;
                smithpanel.sc.setColoreDark(true);
            }
        }
        
        if(evt.getSource()==smithpanel2.large){
            state.IsBig = false;
		smithpanel.setVisible(true);
		psmith.setVisible(true);
		smithpanel2.setVisible(false);
		psmith2.setVisible(false);
		//smithpanel.small.setState(false);
		
		
		//-------------------------
		controlpanel.setEnabled(true);
	    state.setStepNumber(0);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
	    inputpanel.setVisible(true);
	    //state.setZ0(state.getZ0old());
	    //smithpanel.sc.repaint();
	    smithpanel.setZL(false);
	    smithpanel.setYL(false);
	    smithpanel.setZin(false);
	    smithpanel.setYin(false);
	    smithpanel.set_swr_line(false);
	    smithpanel.set_swr_circle(false);
	    
	    //smithpanel2.sc.repaint();
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    
	    
	    state.setDrawTransformer1(false);
	    state.setDrawTransformer2(false);
	    state.setxpos(0.0);
	    smithpanel.setxpos(0.0);
	    smithpanel2.setxpos(0.0);
	    
	    circuitcanvas.repaint(); 
	    
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel.sc.repaint();
	    
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    smithpanel2.sc.repaint();
	    
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    
	    //state.ignition();
	    circuitcanvas.repaint();
	    smithpanel.sc.repaint();
	    smithpanel2.sc.repaint();
            
            
            
            
             
	    }
    
   //--------------------------------------------------------------------
   
    }
    
    //--------------------------------------------------------------------
   
    public void itemStateChanged(ItemEvent evt){
	    ItemSelectable ie = evt.getItemSelectable();
	    
	    if(evt.getSource()==smithpanel.small){
		state.IsBig = true;
		smithpanel.setVisible(false);
		psmith.setVisible(false);
		smithpanel2.setVisible(true);
		psmith2.setVisible(true);
		//smithpanel2.large.setState(false);
		
		//---------------------
		
	    controlpanel.setEnabled(true);
	    state.setStepNumber(0);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
	    inputpanel.setVisible(true);
	    //state.setZ0(state.getZ0old());
	    //smithpanel.sc.repaint();
	    smithpanel.setZL(false);
	    smithpanel.setYL(false);
	    smithpanel.setZin(false);
	    smithpanel.setYin(false);
	    smithpanel.set_swr_line(false);
	    smithpanel.set_swr_circle(false);
	    
	    //smithpanel2.sc.repaint();
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    
	    
	    state.setDrawTransformer1(false);
	    state.setDrawTransformer2(false);
	    state.setxpos(0.0);
	    smithpanel.setxpos(0.0);
	    smithpanel2.setxpos(0.0);
	    
	    circuitcanvas.repaint(); 
	    
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel.sc.repaint();
	    
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    smithpanel2.sc.repaint();
	    
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    
	    //state.ignition();
	    circuitcanvas.repaint();
	    smithpanel.sc.repaint();
	    smithpanel2.sc.repaint();
	
		
	    }
	    if(evt.getSource()==smithpanel2.large){
		state.IsBig = false;
		smithpanel.setVisible(true);
		psmith.setVisible(true);
		smithpanel2.setVisible(false);
		psmith2.setVisible(false);
		//smithpanel.small.setState(false);
		
		
		//-------------------------
		controlpanel.setEnabled(true);
	    state.setStepNumber(0);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
	    inputpanel.setVisible(true);
	    //state.setZ0(state.getZ0old());
	    //smithpanel.sc.repaint();
	    smithpanel.setZL(false);
	    smithpanel.setYL(false);
	    smithpanel.setZin(false);
	    smithpanel.setYin(false);
	    smithpanel.set_swr_line(false);
	    smithpanel.set_swr_circle(false);
	    
	    //smithpanel2.sc.repaint();
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    
	    
	    state.setDrawTransformer1(false);
	    state.setDrawTransformer2(false);
	    state.setxpos(0.0);
	    smithpanel.setxpos(0.0);
	    smithpanel2.setxpos(0.0);
	    
	    circuitcanvas.repaint(); 
	    
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel.sc.repaint();
	    
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    smithpanel2.sc.repaint();
	    
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    
	    //state.ignition();
	    circuitcanvas.repaint();
	    smithpanel.sc.repaint();
	    smithpanel2.sc.repaint();
	    }
	    
    }
     
   //-------------------------------------------------------------------- 
    

    
    public void runStep(int x){
	 switch(x){
	   case 0: 
		state.ignition();
		smithpanel.set_swr_circle(false);
		smithpanel.set_swr_line(false);
		smithpanel.setZin(false);
		smithpanel.setYin(false);
		
		smithpanel.setZL(true);
		smithpanel.setYL(false);
		smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(false);
		smithpanel2.set_swr_line(false);
		smithpanel2.setZin(false);
		smithpanel2.setYin(false);
		
		smithpanel2.setZL(true);
		smithpanel2.setYL(false);
		smithpanel2.sc.repaint();
		
		newgraph.ignition();
		newgraphB.ignition();
		
		newgraph.setLabelAxis(true);
		newgraph.setYRangeMax(1.0);
		newgraph.setYRangeMin(0.0);
		newgraph.plotZeroLine(false);
		newgraph.plotRefPoint(true);
		newgraph.setLabels("Solution # 1",""+state.frequency,"  f [ Hz ]","");
		newgraph.setBand(state.F1,state.F2,state.fpos1,state.fpos2,state.BigBand1);
		newgraph.setRefPoint(state.frequency_scan, phicontrols.slider1.getValue());
		
		newgraph.plot(state.x_freq,state.F_array);
		
		newgraphB.setLabelAxis(true);
		newgraphB.setYRangeMax(1.0);
		newgraphB.setYRangeMin(0.0);
		newgraphB.plotZeroLine(false);
		newgraphB.plotRefPoint(true);
		newgraphB.setLabels("Solution # 2",""+state.frequency,"  f [ Hz ]","");
		newgraphB.setBand(state.G1,state.G2,state.gpos1,state.gpos2,state.BigBand2);
		newgraphB.setRefPoint(state.frequency_scan, phicontrols.slider1.getValue());
		
		newgraphB.plot(state.x_freq,state.G_array);
		
                    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);


		
		if(!state.IsBig){
		    StirThread0_0 st0_0 = new StirThread0_0(state,smithpanel,circuitcanvas,controlpanel);
			st0_0.start();
		}
		else{
		    StirThread0_0B st0_0B = new StirThread0_0B(state,smithpanel2,circuitcanvas,controlpanel);
			st0_0B.start();
		}
	    break;
	    
	   case 1:
	   
	   break;
	   
	   case 2:
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		//circuitcanvas.repaint();
		state.setZin(state.getZL());
		state.setYin(state.getYL());
		smithpanel.setYin(state.getYin(),state.getZ0());
		smithpanel.setZin(state.getZin(),state.getZ0());
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		if(!state.getPaintY()){
		    smithpanel.setYin(false);
		}
		else{
		    smithpanel.setYin(true);
		}
		smithpanel.setZin(true);
		
		smithpanel2.setYin(state.getYin(),state.getZ0());
		smithpanel2.setZin(state.getZin(),state.getZ0());
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		if(!state.getPaintY()){
		    smithpanel2.setYin(false);
		}
		else{
		    smithpanel2.setYin(true);
		}
		smithpanel2.setZin(true);
		
		
		if(state.getZL().Real() == 0.0 || 
				 (state.getZL().Real() == state.getZ0() && state.getZL().Imaginary() == 0.0)){
		
		     controlpanel.b3.setEnabled(true);
		     controlpanel.b1.setEnabled(false);
		     controlpanel.b2.setEnabled(false);
		}
		else{
		
		    if(!state.IsBig){
			StirThread1_0 st1_0 = new StirThread1_0(state,smithpanel,circuitcanvas,controlpanel);
			st1_0.start();
		    }
		    else{
			StirThread1_0B st1_0B = new StirThread1_0B(state,smithpanel2,circuitcanvas,controlpanel);
			st1_0B.start();
		    }
		}
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

		//smithpanel.sc.repaint();
	    break;
	    
	    case 3:
		state.setDrawTransformer1(true);
		circuitcanvas.repaint();
		if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

	    break;
	    
	    case 4:
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		if(!state.getPaintY()){
		    smithpanel.setYin(false);
		}
		else{
		    smithpanel.setYin(true);
		}
		smithpanel.setZin(true);
		//smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		if(!state.getPaintY()){
		    smithpanel2.setYin(false);
		}
		else{
		    smithpanel2.setYin(true);
		}
		smithpanel2.setZin(true);
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

		
		//smithpanel2.sc.repaint();
		if(!state.IsBig){
		    StirThread1_1 st1_1 = new StirThread1_1(state,smithpanel,circuitcanvas,controlpanel);
			st1_1.start();
		}
		else{
		     StirThread1_1B st1_1B = new StirThread1_1B(state,smithpanel2,circuitcanvas,controlpanel);
			st1_1B.start();
		}
	    break;
	    
	    case 5:
		
		/*smithpanel.setYin(state.getYin(),state.getZ0());
		smithpanel.setZin(state.getZin(),state.getZ0());
		smithpanel.setYin(true);
		smithpanel.setZin(true);*/
		
		
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

		
		if(!state.IsBig){    
		    StirThread1_2 st1_2 = new StirThread1_2(state,smithpanel,circuitcanvas,controlpanel);
		    st1_2.start();
		}
		else{
		    StirThread1_2B st1_2B = new StirThread1_2B(state,smithpanel2,circuitcanvas,controlpanel);
		    st1_2B.start();
		}
	    break;
	    
	    case 6:
		    smithpanel.set_swr_line(true);
		    smithpanel2.set_swr_line(true);
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

		    
		    
		    if(!state.IsBig){
			StirThread1_3 st1_3 = new StirThread1_3(state,smithpanel,circuitcanvas,controlpanel);
			st1_3.start(); 
		    }
		    else{
			StirThread1_3B st1_3B = new StirThread1_3B(state,smithpanel2,circuitcanvas,controlpanel);
			st1_3B.start();
		    }
	    
	    break;
	    
	    case 7:
                    smithpanel.sc.OverRide=false;
		    smithpanel2.sc.OverRide=false;
		    
		    smithpanel.setZin(false);
		    smithpanel.setYin(false);
		
		    smithpanel2.setZin(false);
		    smithpanel2.setYin(false);
		
		    smithpanel.set_swr_line(true);
		    smithpanel2.set_swr_line(true);
		    
		    smithpanel.sc.repaint();
		    smithpanel2.sc.repaint();
		    
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

	    break;
	    	    
	    case 8:
		smithpanel.sc.OverRide=false;
		smithpanel.setZin(false);
		smithpanel.setYin(false);
		
		smithpanel2.sc.OverRide=false;
		smithpanel2.setZin(false);
		smithpanel2.setYin(false);
		
		state.setDrawTransformer1(false);
		state.setDrawTransformer2(false);
		state.setAtInput(false);
		circuitcanvas.repaint();
		//smithpanel.sc.cleanAll();
		//smithpanel.sc.cleanAll();
		smithpanel.sc.repaint();
		smithpanel2.sc.repaint();
		
		state.setZin(state.getZL());
		state.setYin(state.getYL());
		
		smithpanel.set_swr_line(true);
		smithpanel.setYin(state.getYin(),state.getZ0());
		smithpanel.setZin(state.getZin(),state.getZ0());
		
		smithpanel2.set_swr_line(true);
		smithpanel2.setYin(state.getYin(),state.getZ0());
		smithpanel2.setZin(state.getZin(),state.getZ0());
		
		if(!state.getPaintY()){
		    smithpanel.setYin(false);
		    smithpanel2.setYin(false);
		}
		else{
		    smithpanel.setYin(true);
		    smithpanel2.setYin(true);
		}
		smithpanel.setZin(true);
		smithpanel2.setZin(true);
		
		//state.setDrawTransformer1(false);
		//state.setAtInput(false);
		//circuitcanvas.repaint();
		
		smithpanel.sc.repaint();
		smithpanel2.sc.repaint();
		
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

		
		if(!state.IsBig){ 
		    
		    StirThread2_0 st2_0 = new StirThread2_0(state,smithpanel,circuitcanvas,controlpanel);
		    st2_0.start();
		}
		else{
		    
		    StirThread2_0B st2_0B = new StirThread2_0B(state,smithpanel2,circuitcanvas,controlpanel);
		    st2_0B.start();
		}
		controlpanel.b2.setLabel("Next Step");
	    break;
	    
	    case 9:
	    
		if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

		state.setDrawTransformer2(true);
		circuitcanvas.repaint();
	    
	    break;
	    
	    case 10:
	    
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

		    
		state.setZin(state.getZL());
		state.setYin(state.getYL());
		smithpanel.setYin(state.getYin(),state.getZ0());
		smithpanel.setZin(state.getZin(),state.getZ0());
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		if(!state.getPaintY()){
		    smithpanel.setYin(false);
		}
		else{
		    smithpanel.setYin(true);
		}
		
		smithpanel.setZin(true);
		
		smithpanel2.setYin(state.getYin(),state.getZ0());
		smithpanel2.setZin(state.getZin(),state.getZ0());
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		if(!state.getPaintY()){
		    smithpanel2.setYin(false);
		}
		else{
		    smithpanel2.setYin(true);
		}
		
		smithpanel2.setZin(true);
		
		if(!state.IsBig){
		    StirThread2_1 st2_1 = new StirThread2_1(state,smithpanel,circuitcanvas,controlpanel);
		    st2_1.start();
		}
		else{
		    StirThread2_1B st2_1B = new StirThread2_1B(state,smithpanel2,circuitcanvas,controlpanel);
		    st2_1B.start();
		}
		//smithpanel.sc.repaint();
	    break;
	    
	    case 11:
	    
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		
		if(!state.getPaintY()){
		    smithpanel.setYin(false);
		}
		else{
		    smithpanel.setYin(true);
		}
		
		smithpanel.setZin(true);
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		
		if(!state.getPaintY()){
		    smithpanel2.setYin(false);
		}
		else{
		    smithpanel2.setYin(true);
		}
		
		smithpanel2.setZin(true);
		
		//smithpanel.sc.repaint();
		
		if(!state.IsBig){
		    StirThread2_2 st2_2 = new StirThread2_2(state,smithpanel,circuitcanvas,controlpanel);
		    st2_2.start();
		}
		else{
		    StirThread2_2B st2_2B = new StirThread2_2B(state,smithpanel2,circuitcanvas,controlpanel);
		    st2_2B.start();
		}
	    break;
	    
	    case 12:
	    
		    if(state.IsBig){
			smithpanel2.setVisible(true);
			smithpanel.setVisible(false);
			psmith2.setVisible(true);
			psmith.setVisible(false);
		    }
		    else{
			smithpanel.setVisible(true);
			smithpanel2.setVisible(false);
			psmith2.setVisible(false);
			psmith.setVisible(true);
		    }
		    newgraph.setVisible(false);
		    newgraphB.setVisible(false);
		    phicontrols.setVisible(false);

		    smithpanel.setYin(false);
		    smithpanel2.setYin(false);
		    if(!state.IsBig){
			StirThread2_3 st2_3 = new StirThread2_3(state,smithpanel,circuitcanvas,controlpanel);
			st2_3.start();
		    }
		    else{
			StirThread2_3B st2_3B = new StirThread2_3B(state,smithpanel2,circuitcanvas,controlpanel);
			st2_3B.start();
		    }
		    controlpanel.b2.setLabel("Summary");
	    break;
	    	      
	    case 13:
		    smithpanel.sc.OverRide=false;
		   
		    smithpanel.set_swr_circle(false);
		    smithpanel.set_swr_line(false);
		    smithpanel.setZin(true);
		    smithpanel.setYin(false);
		    smithpanel.setZL(false);
		    smithpanel.setYL(false);
		    
		    smithpanel2.sc.OverRide=false;
		   
		    smithpanel2.set_swr_circle(false);
		    smithpanel2.set_swr_line(false);
		    smithpanel2.setZin(true);
		    smithpanel2.setYin(false);
		    smithpanel2.setZL(false);
		    smithpanel2.setYL(false);
		    
		    state.setDrawTransformer1(false);
		    state.setDrawTransformer2(false);
		    state.setxpos(0.0);
		    smithpanel.setxpos(0.0);
		    smithpanel.sc.repaint();
		    smithpanel2.setxpos(0.0);
		    smithpanel2.sc.repaint();
		    
		    smithpanel.setVisible(false);
		    smithpanel2.setVisible(false);
		    psmith2.setVisible(false);
		    psmith.setVisible(false);
		    newgraph.setVisible(true);
		    newgraphB.setVisible(true);
		    phicontrols.setVisible(true);

		    
		    circuitcanvas.repaint(); 
	    break;
	 }
    }
}
class StirThread0_0 extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel smithpanel;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread0_0(StateVars state, SmithPanel smithpanel, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel = smithpanel;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  
	  try{
	  sleep(400);
	  
	  smithpanel.set_swr_circle(true);
	  smithpanel.set_swr_line(true);
	  //smithpanel.sc.repaint();
	  
	  smithpanel.setYL(true);
	  smithpanel.sc.repaint();
	  }
	  catch(Exception e){}
	  smithpanel.sc.OverRide=false;
	  
	  //smithpanel.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}


class StirThread1_0 extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel smithpanel;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread1_0(StateVars state, SmithPanel smithpanel, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel = smithpanel;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  int skip;
	  skip = (int)(state.d1*144);
	  if(skip < 1){	skip = 1;}
	  dx = (state.d1)/(float)skip;
	  //smithpanel.sc.OverRide=true;
	  
	  if(state.IsTraceOn){
	    smithpanel.sc.OverRide=true;
	  }
	  else{
	    smithpanel.sc.OverRide=false;
	  }
	  smithpanel.set_swr_line(true);
	  smithpanel.set_swr_circle(true);
	  if(state.getZL().Imaginary() == 0.0){
	    state.setYin(state.getYL());
	    state.setZin(state.getZL());
	    smithpanel.setYin(state.getYin(),state.getZ0());
            smithpanel.setxpos(0.0);
	  }
	  else{
	    while(i<=skip){
	     try{
	    	    state.setYin(EMF.computeYinAt(state.getYL(),state.getZ0(),i*dx,false));
		    state.setZin(EMF.computeZinAt(state.getZL(),state.getZ0(),i*dx,true));
		    smithpanel.setYin(state.getYin(),state.getZ0());
		    smithpanel.setxpos(i*dx);
		    state.setxpos(i*dx);
		    smithpanel.sc.repaint();
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    //if(i==1){smithpanel.set_swr_line(false);}
		    //if(i==skip) {smithpanel.set_swr_line(true);}
		    
		    if(state.IsTraceOn){
			if(i==1){smithpanel.set_swr_line(false);}
		    }
		    else{
			smithpanel.set_swr_line(true);
		    }
		    
		    if(i==skip) {smithpanel.set_swr_line(true);}

	     }
	     catch(Exception e){}
	    } smithpanel.sc.OverRide=false;
	  }
	  //smithpanel.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread1_1 extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel smithpanel;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread1_1(StateVars state, SmithPanel smithpanel, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel = smithpanel;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  int skip =0;
	  smithpanel.sc.OverRide=true;
	  smithpanel.set_swr_line(true);
	  smithpanel.set_swr_circle(true);
	  
	     try{   
		sleep(400);
		state.setZin(new Complex(state.Zin1.Real(),0.0));
		state.setYin(new Complex(EMF.Inv(state.Zin1).Real(),0.0));
		smithpanel.sc.repaint();
		//state.setZ0(state.Z_transformer1);
		smithpanel.set_swr_line(true);
		smithpanel.set_swr_circle(true);
		smithpanel.setYin(state.getYin(),state.Z_transformer1);
	        smithpanel.setZin(state.getZin(),state.Z_transformer1);
	        smithpanel.sc.repaint();
		}
	     catch(Exception e){}
	     //smithpanel.sc.OverRide=false;
	  
	  //smithpanel.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread1_2 extends Thread {
      int i=0;
      double dx;
      Complex startZin, startYin;
      StateVars state;
      SmithPanel smithpanel;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread1_2(StateVars state, SmithPanel smithpanel, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel = smithpanel;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  int skip;
	  skip = (int)(0.25*72);
	  dx = (0.25)/(float)skip;
	  //smithpanel.sc.OverRide=true;
	  
	  if(state.IsTraceOn){
	    smithpanel.sc.OverRide=true;
	  }
	  else{
	    smithpanel.sc.OverRide=false;
	  }
	  
	  smithpanel.set_swr_line(true);
	  smithpanel.set_swr_circle(true);
	  startZin = state.getZin();
	  startYin = state.getYin();
	  while(i<=skip){
	     try{
	    	    state.setYin(EMF.computeYinAt(startYin,state.Z_transformer1,i*dx,false));
		    state.setZin(EMF.computeZinAt(startZin,state.Z_transformer1,i*dx,true));
		    smithpanel.setYin(state.getYin(),state.Z_transformer1);
		    smithpanel.setxpos(state.d1+i*dx);
		    state.setxpos(state.d1+i*dx);
		    smithpanel.sc.repaint();
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    //if(i==1){smithpanel.set_swr_line(false);}
		    if(state.IsTraceOn){
			if(i==1){smithpanel.set_swr_line(false);}
		    }
		    else{
			smithpanel.set_swr_line(true);
		    }
		    
		    //if(i==skip) {smithpanel.set_swr_line(true);}

		    
		    if(i==skip) {smithpanel.set_swr_line(true);
				 state.setAtInput(true);}
				 
		
	     }
	     catch(Exception e){}
	     }//smithpanel.sc.OverRide=false;
	  
	  //smithpanel.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread1_3 extends Thread {
      int i=0;
      double dx;
      Complex startZin, startYin;
      StateVars state;
      SmithPanel smithpanel;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread1_3(StateVars state, SmithPanel smithpanel, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel = smithpanel;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  
	  smithpanel.sc.OverRide=true;
	  smithpanel.set_swr_line(true);
	  smithpanel.set_swr_circle(true);
	  startZin = state.getZin();
	  startYin = state.getYin();
	  {
	     try{
	    	sleep(400);
		//state.setZ0(state.getZ0old());
		circuitcanvas.repaint();
		state.setZin(new Complex(1.0*state.getZ0(),0.0));
		state.setYin(new Complex(1.0*state.getZ0(),0.0));
		smithpanel.set_swr_line(false);
		smithpanel.set_swr_circle(true);
		smithpanel.setYin(state.getYin(),state.getZ0());
	        smithpanel.setZin(state.getZin(),state.getZ0());
	        smithpanel.sc.repaint();
	     }
	     catch(Exception e){}
	  } 
	  //smithpanel.sc.OverRide=false;
	  
	  //smithpanel.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}


class StirThread2_0 extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel smithpanel;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread2_0(StateVars state, SmithPanel smithpanel, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel = smithpanel;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons 
	    
	  controlpanel.setEnabled(false);
	  int skip;
	  skip = (int)(state.d2*144);
	  if(skip < 1){skip =1;}
	  dx = (state.d2)/(float)skip;
	  smithpanel.sc.OverRide=false;
	  if(state.IsTraceOn){
	    smithpanel.sc.OverRide=true;
	  }
	  else{
	    smithpanel.sc.OverRide=false;
	  }
	  
	  smithpanel.set_swr_line(true);
	  smithpanel.set_swr_circle(true);
	  /*if(state.getZL().Imaginary() == 0.0){
	    state.setYin(state.getYL());
	    state.setZin(state.getZL());
	    smithpanel.setYin(state.getYin(),state.getZ0());
            smithpanel.setxpos(0.0);
	  }
	  else*/{
	    while(i<=skip){
	     try{   if(i==0){
			sleep(800);
			//smithpanel.repaint();
			//sleep(200);
		    }
	    	    state.setYin(EMF.computeYinAt(state.getYL(),state.getZ0(),i*dx,false));
		    state.setZin(EMF.computeZinAt(state.getZL(),state.getZ0(),i*dx,true));
		    smithpanel.setYin(state.getYin(),state.getZ0());
		    smithpanel.setxpos(i*dx);
		    state.setxpos(i*dx);
		    smithpanel.sc.repaint();
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    //if(i==1){smithpanel.set_swr_line(false);}
		    //if(i==skip) {smithpanel.set_swr_line(true);}
		    if(state.IsTraceOn){
			if(i==1){smithpanel.set_swr_line(false);}
		    }
		    else{
			smithpanel.set_swr_line(true);
		    }
		    
		    if(i==skip) {smithpanel.set_swr_line(true);}

		    
	     }
	     catch(Exception e){}
	    } smithpanel.sc.OverRide=false;
	  }
	  //smithpanel.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread2_1 extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel smithpanel;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread2_1(StateVars state, SmithPanel smithpanel, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel = smithpanel;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  int skip =0;
	  smithpanel.sc.OverRide=true;
	  smithpanel.set_swr_line(true);
	  smithpanel.set_swr_circle(true);
	  
	     try{   
		sleep(400);
		state.setZin(new Complex(state.Zin2.Real(),0.0));
		state.setYin(new Complex(EMF.Inv(state.Zin2).Real(),0.0));
		smithpanel.sc.repaint();
		//state.setZ0(state.Z_transformer2);
		smithpanel.set_swr_line(true);
		smithpanel.set_swr_circle(true);
		smithpanel.setYin(state.getYin(),state.Z_transformer2);
	        smithpanel.setZin(state.getZin(),state.Z_transformer2);
	        smithpanel.sc.repaint();
		}
	     catch(Exception e){}
	     //smithpanel.sc.OverRide=false;
	  
	  //smithpanel.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread2_2 extends Thread {
      int i=0;
      double dx;
      Complex startZin, startYin;
      StateVars state;
      SmithPanel smithpanel;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread2_2(StateVars state, SmithPanel smithpanel, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel = smithpanel;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  
	  int skip;
	  skip = (int)(0.25*72);
	  dx = (0.25)/(float)skip;
	  //smithpanel.sc.OverRide=true;
	  if(state.IsTraceOn){
	    smithpanel.sc.OverRide=true;
	  }
	  else{
	    smithpanel.sc.OverRide=false;
	  }
	  smithpanel.set_swr_line(true);
	  smithpanel.set_swr_circle(true);
	  startZin = state.getZin();
	  startYin = state.getYin();
	  while(i<=skip){
	     try{
	    	    state.setYin(EMF.computeYinAt(startYin,state.Z_transformer2,i*dx,false));
		    state.setZin(EMF.computeZinAt(startZin,state.Z_transformer2,i*dx,true));
		    smithpanel.setYin(state.getYin(),state.Z_transformer2);
		    smithpanel.setxpos(state.d2+i*dx);
		    state.setxpos(state.d2+i*dx);
		    smithpanel.sc.repaint();
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    //if(i==1){smithpanel.set_swr_line(false);}
		    if(state.IsTraceOn){
			if(i==1){smithpanel.set_swr_line(false);}
		    }
		    else{
			smithpanel.set_swr_line(true);
		    }
		    
		    //if(i==skip) {smithpanel.set_swr_line(true);}

		    
		    if(i==skip) {smithpanel.set_swr_line(true);
				 state.setAtInput(true);}
	     }
	     catch(Exception e){}
	     }//smithpanel.sc.OverRide=false;
	  
	  //smithpanel.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread2_3 extends Thread {
      int i=0;
      double dx;
      Complex startZin, startYin;
      StateVars state;
      SmithPanel smithpanel;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread2_3(StateVars state, SmithPanel smithpanel, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel = smithpanel;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  
	  smithpanel.sc.OverRide=true;
	  smithpanel.set_swr_line(true);
	  smithpanel.set_swr_circle(true);
	  startZin = state.getZin();
	  startYin = state.getYin();
	  {
	     try{
	    	sleep(400);
		//state.setZ0(state.getZ0old());
		state.setZin(new Complex(1.0*state.getZ0(),0.0));
		state.setYin(new Complex(1.0*state.getZ0(),0.0));
		smithpanel.set_swr_line(false);
		smithpanel.set_swr_circle(true);
		smithpanel.setYin(state.getYin(),state.getZ0());
	        smithpanel.setZin(state.getZin(),state.getZ0());
	        smithpanel.sc.repaint();
		circuitcanvas.repaint();
	     }
	     catch(Exception e){}
	  } 
	  //smithpanel.sc.OverRide=false;
	  
	  //smithpanel.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}


//  threads for smithpanel2

class StirThread0_0B extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel2 smithpanel2;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread0_0B(StateVars state, SmithPanel2 smithpanel2, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel2 = smithpanel2;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  
	  try{
	  sleep(400);
	  
	  smithpanel2.set_swr_circle(true);
	  smithpanel2.set_swr_line(true);
	  //smithpanel2.sc.repaint();
	  
	  smithpanel2.setYL(true);
	  smithpanel2.sc.repaint();
	  }
	  catch(Exception e){}
	  smithpanel2.sc.OverRide=false;
	  
	  //smithpanel2.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}


class StirThread1_0B extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel2 smithpanel2;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread1_0B(StateVars state, SmithPanel2 smithpanel2, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel2 = smithpanel2;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  int skip;
	  skip = (int)(state.d1*144);
	  if(skip < 1){	skip = 1;}
	  dx = (state.d1)/(float)skip;
	  //smithpanel2.sc.OverRide=true;
	  
	  if(state.IsTraceOn){
	    smithpanel2.sc.OverRide=true;
	  }
	  else{
	    smithpanel2.sc.OverRide=false;
	    
	  }
	  smithpanel2.set_swr_line(true);
	  smithpanel2.set_swr_circle(true);
	  if(state.getZL().Imaginary() == 0.0){
	    state.setYin(state.getYL());
	    state.setZin(state.getZL());
	    smithpanel2.setYin(state.getYin(),state.getZ0());
            smithpanel2.setxpos(0.0);
	  }
	  else{
	    while(i<=skip){
	     try{
	    	    state.setYin(EMF.computeYinAt(state.getYL(),state.getZ0(),i*dx,false));
		    state.setZin(EMF.computeZinAt(state.getZL(),state.getZ0(),i*dx,true));
		    smithpanel2.setYin(state.getYin(),state.getZ0());
		    smithpanel2.setxpos(i*dx);
		    state.setxpos(i*dx);
		    smithpanel2.sc.repaint();
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    //if(i==1){smithpanel2.set_swr_line(false);}
		    //if(i==skip) {smithpanel2.set_swr_line(true);}
		    
		    if(state.IsTraceOn){
			if(i==1){smithpanel2.set_swr_line(false);}
		    }
		    else{
			smithpanel2.set_swr_line(true);
		    }
		    
		    if(i==skip) {smithpanel2.set_swr_line(true);}

	     }
	     catch(Exception e){}
	    } smithpanel2.sc.OverRide=false;
	  }
	  //smithpanel2.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread1_1B extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel2 smithpanel2;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread1_1B(StateVars state, SmithPanel2 smithpanel2, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel2 = smithpanel2;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  int skip =0;
	  smithpanel2.sc.OverRide=true;
	  smithpanel2.set_swr_line(true);
	  smithpanel2.set_swr_circle(true);
	  
	     try{   
		sleep(400);
		state.setZin(new Complex(state.Zin1.Real(),0.0));
		state.setYin(new Complex(EMF.Inv(state.Zin1).Real(),0.0));
		smithpanel2.sc.repaint();
		//state.setZ0(state.Z_transformer1);
		smithpanel2.set_swr_line(true);
		smithpanel2.set_swr_circle(true);
		smithpanel2.setYin(state.getYin(),state.Z_transformer1);
	        smithpanel2.setZin(state.getZin(),state.Z_transformer1);
	        smithpanel2.sc.repaint();
		}
	     catch(Exception e){}
	     //smithpanel2.sc.OverRide=false;
	  
	  //smithpanel2.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread1_2B extends Thread {
      int i=0;
      double dx;
      Complex startZin, startYin;
      StateVars state;
      SmithPanel2 smithpanel2;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread1_2B(StateVars state, SmithPanel2 smithpanel2, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel2 = smithpanel2;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  int skip;
	  skip = (int)(0.25*72);
	  dx = (0.25)/(float)skip;
	  //smithpanel2.sc.OverRide=true;
	  
	  if(state.IsTraceOn){
	    smithpanel2.sc.OverRide=true;
	  }
	  else{
	    smithpanel2.sc.OverRide=false;
	  }
	  
	  smithpanel2.set_swr_line(true);
	  smithpanel2.set_swr_circle(true);
	  startZin = state.getZin();
	  startYin = state.getYin();
	  while(i<=skip){
	     try{
	    	    state.setYin(EMF.computeYinAt(startYin,state.Z_transformer1,i*dx,false));
		    state.setZin(EMF.computeZinAt(startZin,state.Z_transformer1,i*dx,true));
		    smithpanel2.setYin(state.getYin(),state.Z_transformer1);
		    smithpanel2.setxpos(state.d1+i*dx);
		    state.setxpos(state.d1+i*dx);
		    smithpanel2.sc.repaint();
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    //if(i==1){smithpanel2.set_swr_line(false);}
		    if(state.IsTraceOn){
			if(i==1){smithpanel2.set_swr_line(false);}
		    }
		    else{
			smithpanel2.set_swr_line(true);
		    }
		    
		    //if(i==skip) {smithpanel2.set_swr_line(true);}

		    
		    if(i==skip) {smithpanel2.set_swr_line(true);
				 state.setAtInput(true);}
				 
		
	     }
	     catch(Exception e){}
	     }//smithpanel2.sc.OverRide=false;
	  
	  //smithpanel2.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread1_3B extends Thread {
      int i=0;
      double dx;
      Complex startZin, startYin;
      StateVars state;
      SmithPanel2 smithpanel2;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread1_3B(StateVars state, SmithPanel2 smithpanel2, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel2 = smithpanel2;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  
	  smithpanel2.sc.OverRide=true;
	  smithpanel2.set_swr_line(true);
	  smithpanel2.set_swr_circle(true);
	  startZin = state.getZin();
	  startYin = state.getYin();
	  {
	     try{
	    	sleep(400);
		//state.setZ0(state.getZ0old());
		circuitcanvas.repaint();
		state.setZin(new Complex(1.0*state.getZ0(),0.0));
		state.setYin(new Complex(1.0*state.getZ0(),0.0));
		smithpanel2.set_swr_line(false);
		smithpanel2.set_swr_circle(true);
		smithpanel2.setYin(state.getYin(),state.getZ0());
	        smithpanel2.setZin(state.getZin(),state.getZ0());
	        smithpanel2.sc.repaint();
	     }
	     catch(Exception e){}
	  } 
	  //smithpanel2.sc.OverRide=false;
	  
	  //smithpanel2.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}


class StirThread2_0B extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel2 smithpanel2;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread2_0B(StateVars state, SmithPanel2 smithpanel2, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel2 = smithpanel2;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons 
	    
	  controlpanel.setEnabled(false);
	  int skip;
	  skip = (int)(state.d2*144);
	  if(skip < 1){skip =1;}
	  dx = (state.d2)/(float)skip;
	  //smithpanel2.sc.OverRide=false;
	  //------------
	  smithpanel2.sc.OverRide = false;
	    //smithpanel2.sc.cleanAll();
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    smithpanel2.sc.repaint();
	  //------------------------
	  
	  if(state.IsTraceOn){
	    smithpanel2.sc.OverRide=true;
	  }
	  else{
	    smithpanel2.sc.OverRide=false;
	  }
	  
	  smithpanel2.set_swr_line(true);
	  smithpanel2.set_swr_circle(true);
	  /*if(state.getZL().Imaginary() == 0.0){
	    state.setYin(state.getYL());
	    state.setZin(state.getZL());
	    smithpanel2.setYin(state.getYin(),state.getZ0());
            smithpanel2.setxpos(0.0);
	  }
	  else*/{
	    while(i<=skip){
	     try{   if(i==0){
			sleep(800);
			//smithpanel2.repaint();
			//sleep(200);
		    }
	    	    state.setYin(EMF.computeYinAt(state.getYL(),state.getZ0(),i*dx,false));
		    state.setZin(EMF.computeZinAt(state.getZL(),state.getZ0(),i*dx,true));
		    smithpanel2.setYin(state.getYin(),state.getZ0());
		    smithpanel2.setxpos(i*dx);
		    state.setxpos(i*dx);
		    smithpanel2.sc.repaint();
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    //if(i==1){smithpanel2.set_swr_line(false);}
		    //if(i==skip) {smithpanel2.set_swr_line(true);}
		    if(state.IsTraceOn){
			if(i==1){smithpanel2.set_swr_line(false);}
		    }
		    else{
			smithpanel2.set_swr_line(true);
		    }
		    
		    if(i==skip) {smithpanel2.set_swr_line(true);}

		    
	     }
	     catch(Exception e){}
	    } smithpanel2.sc.OverRide=false;
	  }
	  //smithpanel2.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread2_1B extends Thread {
      int i=0;
      double dx;
      StateVars state;
      SmithPanel2 smithpanel2;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread2_1B(StateVars state, SmithPanel2 smithpanel2, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel2 = smithpanel2;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  int skip =0;
	  
	  smithpanel2.sc.OverRide=true;
	  smithpanel2.sc.repaint();
	  smithpanel2.set_swr_line(true);
	  smithpanel2.set_swr_circle(true);
	  
	     try{   
		sleep(400);
		state.setZin(new Complex(state.Zin2.Real(),0.0));
		state.setYin(new Complex(EMF.Inv(state.Zin2).Real(),0.0));
		smithpanel2.sc.repaint();
		//state.setZ0(state.Z_transformer2);
		smithpanel2.set_swr_line(true);
		smithpanel2.set_swr_circle(true);
		smithpanel2.setYin(state.getYin(),state.Z_transformer2);
	        smithpanel2.setZin(state.getZin(),state.Z_transformer2);
	        smithpanel2.sc.repaint();
		}
	     catch(Exception e){}
	     //smithpanel2.sc.OverRide=false;
	  
	  //smithpanel2.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread2_2B extends Thread {
      int i=0;
      double dx;
      Complex startZin, startYin;
      StateVars state;
      SmithPanel2 smithpanel2;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread2_2B(StateVars state, SmithPanel2 smithpanel2, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel2 = smithpanel2;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  
	  int skip;
	  skip = (int)(0.25*72);
	  dx = (0.25)/(float)skip;
	  //smithpanel2.sc.OverRide=true;
	  if(state.IsTraceOn){
	    smithpanel2.sc.OverRide=true;
	  }
	  else{
	    smithpanel2.sc.OverRide=false;
	  }
	  smithpanel2.set_swr_line(true);
	  smithpanel2.set_swr_circle(true);
	  startZin = state.getZin();
	  startYin = state.getYin();
	  while(i<=skip){
	     try{
	    	    state.setYin(EMF.computeYinAt(startYin,state.Z_transformer2,i*dx,false));
		    state.setZin(EMF.computeZinAt(startZin,state.Z_transformer2,i*dx,true));
		    smithpanel2.setYin(state.getYin(),state.Z_transformer2);
		    smithpanel2.setxpos(state.d2+i*dx);
		    state.setxpos(state.d2+i*dx);
		    smithpanel2.sc.repaint();
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    //if(i==1){smithpanel2.set_swr_line(false);}
		    if(state.IsTraceOn){
			if(i==1){smithpanel2.set_swr_line(false);}
		    }
		    else{
			smithpanel2.set_swr_line(true);
		    }
		    
		    //if(i==skip) {smithpanel2.set_swr_line(true);}

		    
		    if(i==skip) {smithpanel2.set_swr_line(true);
				 state.setAtInput(true);}
	     }
	     catch(Exception e){}
	     }//smithpanel2.sc.OverRide=false;
	  
	  //smithpanel2.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}

class StirThread2_3B extends Thread {
      int i=0;
      double dx;
      Complex startZin, startYin;
      StateVars state;
      SmithPanel2 smithpanel2;
      CircuitCanvas circuitcanvas;
      ControlPanel controlpanel;
      StirThread2_3B(StateVars state, SmithPanel2 smithpanel2, CircuitCanvas circuitcanvas, ControlPanel controlpanel){
	 super();
	 this.state = state;
	 this.smithpanel2 = smithpanel2;
	 this.circuitcanvas = circuitcanvas;
	 this.controlpanel = controlpanel;
      }
      
      public void run(){
	  //Disable control buttons   
	  controlpanel.setEnabled(false);
	  
	  smithpanel2.sc.OverRide=true;
	  smithpanel2.set_swr_line(true);
	  smithpanel2.set_swr_circle(true);
	  startZin = state.getZin();
	  startYin = state.getYin();
	  {
	     try{
	    	sleep(400);
		//state.setZ0(state.getZ0old());
		state.setZin(new Complex(1.0*state.getZ0(),0.0));
		state.setYin(new Complex(1.0*state.getZ0(),0.0));
		smithpanel2.set_swr_line(false);
		smithpanel2.set_swr_circle(true);
		smithpanel2.setYin(state.getYin(),state.getZ0());
	        smithpanel2.setZin(state.getZin(),state.getZ0());
	        smithpanel2.sc.repaint();
		circuitcanvas.repaint();
	     }
	     catch(Exception e){}
	  } 
	  //smithpanel2.sc.OverRide=false;
	  
	  //smithpanel2.sc.repaint();
	  //Enable control buttons
	  controlpanel.setEnabled(true);
      }       
}
