//SingleStub.java
/*Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * June 2009 - All Rights Reserved
 */   


import java.awt.*;
import java.awt.event.*;
import java.util.*;
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 ShuntAdmittanceMatch extends Frame implements ActionListener, ItemListener, AdjustmentListener, WindowListener{
    Instructions instructions;
    Panel instrFrame, instrFrame1;
    //About about;
    TitlePanel titlepanel;
    ControlPanel controlpanel;
    InputPanel inputpanel;
    SmithPanel smithpanel;
    SmithPanel2 smithpanel2;
    NewGraphCanvas newgraph;
    NewGraphCanvasB newgraphB;
    CircuitCanvas circuitcanvas;
    StateVars state;
    StepCanvas stepcanvas;
    public Panel psmith, psmith2;
    Image image;
    line line;
    PhiControls phicontrols;
    public Label labtitle;
    final Font labfonte = new Font("Sanserif",Font.BOLD,15);
    public Panel plot1, plot2;
    
    // CHANGE ==================================================================
    public int xmove = 10;
    public int ymove = 34;
    //==========================================================================
    
    Font ttfFont, phiFont, italicFont;

    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 xxmove = 20;
        int yymove = 41;
        ShuntAdmittanceMatch f = new ShuntAdmittanceMatch();
        f.setSize(783+xxmove,600+yymove);
        f.setVisible(true);
        //f.setLayout(new FlowLayout());
        f.setLayout(null);
    }  
    
    public ShuntAdmittanceMatch() {
	setLayout(null);
        String lcOSName = System.getProperty("os.name").toLowerCase();
        boolean MAC_OS_X = lcOSName.startsWith("mac os x");
        //System.out.println(lcOSName);
        // CHANGE ==============================================================
        //int xmove = 10;
        //int ymove = 34;
        //======================================================================
        if (!useOldFonts) {
            
            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.8","Discrete Element Matching");
        titlepanel = new TitlePanel(state.ttfFont.deriveFont(Font.BOLD,13f),
                                    "Module 2.8 Tutorial","Discrete Element Matching");
	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);
	smithpanel.setVisible(true);
	//add(smithpanel);
	
	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+xmove,11+ymove,568,673);
	smithpanel2.setVisible(false);
	//add(smithpanel2);
	
	newgraph = new NewGraphCanvas(state);
	//newgraph.setBounds(371,11,401,276); 
        newgraph.setBounds(3710,1100,401,276); // paint out of the way for MAC - MAC sucks!!!
	newgraph.setVisible(false);
	//add(newgraph);
	
	newgraphB = new NewGraphCanvasB(state);
	//newgraphB.setBounds(371,310,401,276);
        newgraphB.setBounds(3710,3100,401,276); // paint out of the way for MAC - MAC sucks!!!
	newgraphB.setVisible(false);
	//add(newgraphB);
	
	phicontrols = new PhiControls(state);
	//phicontrols.setBounds(370,288,403,21);
        phicontrols.setBounds(3700,2880,403,21); // paint out of the way for MAC - MAC sucks!!!
	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,324,353,262);
        stepcanvas.setBounds(1100,3240,353,262); // paint out of the way for MAC - MAC sucks!!!
	//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(11+xmove,56+ymove,761,531);
        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);
	
	plot1 = new Panel();
	    plot1.setBackground(Color.black);
	    //add(plot1);
	    //plot1.setBounds(370+xmove,10+ymove,403,278);
            plot1.setBounds(3700,1000,403,278); // paint out of the way for MAC - MAC sucks!!!
	    plot1.setVisible(true);
	    
	plot2 = new Panel();
	    plot2.setBackground(Color.black);
	    //add(plot2);
	    //plot2.setBounds(370+xmove,309+ymove,403,278);
            plot2.setBounds(3700,3090,403,278); // paint out of the way for MAC - MAC sucks!!!
	    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(0,0,890,687);
	
            //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 - REMOVE EXPLICITLY - MAC DOES NOT HANDLE WELL setVisible
	    //add(smithpanel2);
            //==================================================================
	    add(newgraph);
	    add(newgraphB);
	    add(circuitcanvas);
	    add(stepcanvas);
	    add(pgraph);
	    add(pgraphb);
	    add(phicontrols);
	    add(psmith);
	    add(psmith2);
	    add(plot1);
	    add(plot2);
	    add(ptitle);
	    add(p0);
	    add(p00);
	    state.LicenseExpired = false;
            //}
	
	//Listeners
        this.addWindowListener(this);
	inputpanel.b1.addActionListener(this);
        instructions.bupdate.addActionListener(this);
	controlpanel.b1.addActionListener(this);
	controlpanel.b2.addActionListener(this);
	controlpanel.b3.addActionListener(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.drawLine(0,298,800,298);
        //g.drawRect(0,0,784,596);
        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.ignition();
	    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();
		
		//System.out.println(state.frequency_scan+"    "+phicontrols.slider1.getValue());
		
	    //smithpanel.sc.repaint();
	}
	else if(evt.getSource()==controlpanel.b2){//Next Step
	    if(state.getStepNumber()==1 && inputpanel.isVisible()){
		int xmove = 10;
                int ymove = 34;
                state.ignition2();
		inputpanel.setVisible(false);
		stepcanvas.setVisible(true);
                //-----------------------------------------------------------
                // put back for MAC - MAC sucks!!!
                phicontrols.setBounds(370+xmove,288+ymove,403,21);
                newgraph.setBounds(371+xmove,11+ymove,401,276);
                newgraphB.setBounds(371+xmove,310+ymove,401,276);
                plot1.setBounds(370+xmove,10+ymove,403,278);
                plot2.setBounds(370+xmove,309+ymove,403,278);
                stepcanvas.setBounds(11+xmove,324+ymove,353,262);
                //-----------------------------------------------------------

		stepcanvas.repaint();
		controlpanel.b1.setEnabled(true);
		controlpanel.b3.setEnabled(true);
                state.setZin(state.getZL());
		state.ignition2();
		smithpanel.setZin(state.getZL(),state.getZ0());
		smithpanel2.setZin(state.getZL(),state.getZ0());	
		runStep(1);
	    }
	    else if(state.getStepNumber()==5){ 
		state.setZin(state.getZL());
		state.ignition2();
		smithpanel.setZin(state.getZL(),state.getZ0());
		smithpanel2.setZin(state.getZL(),state.getZ0());
		state.StepIncrement();
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
            // CHANGE - ADD LINES BELOW ========================================
            else if(state.getStepNumber()==9){
                controlpanel.b2.setEnabled(false);
            }
            //==================================================================
	    else{
		state.StepIncrement();
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
	}
	else if(evt.getSource()==controlpanel.b1){//Previous Step 
		state.setZin(state.getZL());
		state.ignition2();
		smithpanel.setZin(state.getZL(),state.getZ0());
		smithpanel2.setZin(state.getZL(),state.getZ0());
		
		controlpanel.b2.setLabel("Next Step");
                // CHANGE - ADD LINE BELOW ===================================== 
                controlpanel.b2.setEnabled(true);
                //==============================================================
            
	    if(state.getStepNumber()==1 && !inputpanel.isVisible()){
		inputpanel.repaint();
                stepcanvas.setVisible(false);
		inputpanel.setVisible(true);
                inputpanel.repaint();
                //---------------------------------------------------------------------------------------
                stepcanvas.setBounds(1100,3240,353,262);  // print out of the way for MAC - MAC sucks!!!
                phicontrols.setBounds(3700,2880,403,21);
                newgraph.setBounds(3710,1100,401,276);
                newgraphB.setBounds(3710,3100,401,276);
                plot1.setBounds(3700,1000,403,278);
                plot2.setBounds(3700,3090,403,278);
                //-----------------------------------------------------------
		smithpanel.setZL(false);
		smithpanel.set_swr_line(false);
		smithpanel.sc.repaint();
		smithpanel2.setZL(false);
		smithpanel2.set_swr_line(false);
		smithpanel2.sc.repaint();
		controlpanel.b1.setEnabled(false);
		controlpanel.b3.setEnabled(false);
                
                
	    }
	    else if(state.getStepNumber()==5){
		state.setStepNumber(3);
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
	    else if(state.getStepNumber()==6){
		state.setStepNumber(3);
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
	    	    
	    else if(state.getStepNumber()==7){
		state.setStepNumber(6);
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
	    else if(state.getStepNumber()==8){
		state.setStepNumber(6);
		stepcanvas.repaint();
		runStep(state.getStepNumber());
	    }
	    
	    else if(state.getStepNumber()==9){
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(false);
		if(!state.getPaintZ()){
		    smithpanel.setZin(false);
		}
		else{
		    smithpanel.setZin(true);
		}
		//smithpanel.setZin(true);
		smithpanel.setYin(true);
		smithpanel.setZL(true);
		smithpanel.setYL(true);
		smithpanel.sc.repaint(); 
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(false);
		if(!state.getPaintZ()){
		    smithpanel2.setZin(false);
		}
		else{
		    smithpanel2.setZin(true);
		}
		//smithpanel2.setZin(true);
		smithpanel2.setYin(true);
		smithpanel2.setZL(true);
		smithpanel2.setYL(true);
		smithpanel2.sc.repaint(); 
		
		state.setStepNumber(6);
		stepcanvas.repaint();
		runStep(state.getStepNumber());
		
	    }
	    else{
		state.StepDecrement();
		stepcanvas.repaint();
		runStep(state.getStepNumber());
		
	    }
	    
	}
	else if(evt.getSource()==controlpanel.b3){//Start Over 
	    state.setZin(state.getZL());
	    state.ignition2();
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
            
	    state.setStepNumber(1);
	    controlpanel.b2.setEnabled(true);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
            inputpanel.repaint();
	    inputpanel.setVisible(true);
            inputpanel.repaint();
            //---------------------------------------------------------------------------------------
                stepcanvas.setBounds(1100,3240,353,262);  // print out of the way for MAC - MAC sucks!!!
                phicontrols.setBounds(3700,2880,403,21);
                newgraph.setBounds(3710,1100,401,276);
                newgraphB.setBounds(3710,3100,401,276);
                plot1.setBounds(3700,1000,403,278);
                plot2.setBounds(3700,3090,403,278);
            //-----------------------------------------------------------
	    //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);
	    smithpanel.setxpos(0.0);
	    
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    smithpanel2.setxpos(0.0);
	    
	    state.setxpos(0.0);
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    
	    //smithpanel.sc.repaint();
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    state.stub1.isEnable(false);
	    state.stub2.isEnable(false);
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    //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);
                    
                    inputpanel.repaint();
	}
        
        //----------------------------------NEW
        if(evt.getSource()==smithpanel.small){
            //about.setVisible(true);
            instructions.setVisible(true);
            instrFrame.setVisible(true);
            instrFrame1.setVisible(true);
            labtitle.setVisible(true);
        }
        
        if(evt.getSource()==instructions.bupdate){
            //about.setVisible(false);
            instructions.setVisible(false);
            instrFrame.setVisible(false);
            instrFrame1.setVisible(false);
            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);
	    //-----------------------------------------------start over
	    state.setStepNumber(1);
	    controlpanel.b2.setEnabled(true);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
	    inputpanel.setVisible(true);
	    //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);
	    smithpanel.setxpos(0.0);
	    
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    smithpanel2.setxpos(0.0);
	    
	    state.setxpos(0.0);
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    
	    //smithpanel.sc.repaint();
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    state.stub1.isEnable(false);
	    state.stub2.isEnable(false);
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    //state.ignition();
	    circuitcanvas.repaint();
	    smithpanel.sc.repaint();
	    smithpanel2.sc.repaint();
	    //-----------------------------------------------------------end of start over
		//------------------------------------------
		if(smithpanel.scon.imp.getState()){
		    smithpanel2.scon.imp.setState(true);
		    smithpanel2.scon.adm.setState(false);
                    
                    smithpanel2.set_impedance(true);
		    smithpanel2.set_admittance(false);
		    smithpanel2.sm.setMode(1);
                    smithpanel.sm.setMode(1);
                    smithpanel2.sc.setMode(1);
                    smithpanel.sc.setMode(1);
                    smithpanel2.sm.setZL(smithpanel2.sc.getZL());
		    smithpanel2.sm.setZin(smithpanel2.sc.getZin());
		}
		else if(smithpanel.scon.adm.getState()){
		    smithpanel2.scon.imp.setState(false);
		    smithpanel2.scon.adm.setState(true);
		    smithpanel2.set_impedance(false);
		    smithpanel2.set_admittance(true);
		    smithpanel2.sm.setMode(2);
                    smithpanel.sm.setMode(2);
                    smithpanel2.sc.setMode(2);
                    smithpanel.sc.setMode(2);
                    smithpanel2.sm.setZL(smithpanel2.sc.getZL());
		    smithpanel2.sm.setYin(smithpanel2.sc.getYin());
		}
		//-------------------------------------------
		if(smithpanel.scon.swr.getState()){
		    smithpanel2.scon.swr.setState(true);
		    smithpanel2.sc.set_swr_circle(true);
		}
		else{
		    smithpanel2.scon.swr.setState(false);
		    smithpanel2.set_swr_circle(false);
		}
		//-------------------------------------------
		if(smithpanel.scon.swrline.getState()){
		    smithpanel2.scon.swrline.setState(true);
		    smithpanel2.set_swr_line(true);
		}
		else{
		    smithpanel2.scon.swrline.setState(false);
		    smithpanel2.set_swr_line(false);
		}
		//-------------------------------------------
		smithpanel2.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);
                //------------------------------------REMOVE
		//smithpanel.small.setState(false);
	    //----------------------------------------------------start over
	    
            state.setStepNumber(1);
	    controlpanel.b2.setEnabled(true);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
	    inputpanel.setVisible(true);
	    //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);
	    smithpanel.setxpos(0.0);
	    
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    smithpanel2.setxpos(0.0);
	    
	    state.setxpos(0.0);
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    
	    //smithpanel.sc.repaint();
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    state.stub1.isEnable(false);
	    state.stub2.isEnable(false);
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    //state.ignition();
	    circuitcanvas.repaint();
	    smithpanel.sc.repaint();
	    smithpanel2.sc.repaint();
	    //----------------------------------------------------end of start over
		
		//------------------------------------------
		if(smithpanel2.scon.imp.getState()){
		    smithpanel.scon.imp.setState(true);
		    smithpanel.scon.adm.setState(false);
		    smithpanel.set_impedance(true);
		    smithpanel.set_admittance(false);
		    smithpanel.sm.setMode(1);
                    smithpanel2.sm.setMode(1);
                    smithpanel.sc.setMode(1);
                    smithpanel2.sc.setMode(1);
		    smithpanel.sm.setZL(smithpanel2.sc.getZL());
		    smithpanel.sm.setZin(smithpanel2.sc.getZin());
		}
		else if(smithpanel2.scon.adm.getState()){
		    smithpanel.scon.imp.setState(false);
		    smithpanel.scon.adm.setState(true);
		    smithpanel.set_impedance(false);
		    smithpanel.set_admittance(true);
                    smithpanel2.sm.setMode(2);
                    smithpanel.sm.setMode(2);
                    smithpanel2.sc.setMode(2);
                    smithpanel.sc.setMode(2);
		    smithpanel.sm.setZL(smithpanel2.sc.getZL());
		    smithpanel.sm.setYin(smithpanel2.sc.getYin());
		}
		//-------------------------------------------
		if(smithpanel2.scon.swr.getState()){
		    smithpanel.scon.swr.setState(true);
		    smithpanel.set_swr_circle(true);
		}
		else{
		    smithpanel.scon.swr.setState(false);
		    smithpanel.set_swr_circle(false);
		}
		//-------------------------------------------
		if(smithpanel2.scon.swrline.getState()){
		    smithpanel.scon.swrline.setState(true);
		    smithpanel.set_swr_line(true);
		}
		else{
		    smithpanel.scon.swrline.setState(false);
		    smithpanel.set_swr_line(false);
		}
		//-------------------------------------------
		smithpanel2.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);
	    //-----------------------------------------------start over
	    state.setStepNumber(1);
	    controlpanel.b2.setEnabled(true);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
	    inputpanel.setVisible(true);
	    //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);
	    smithpanel.setxpos(0.0);
	    
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    smithpanel2.setxpos(0.0);
	    
	    state.setxpos(0.0);
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    
	    //smithpanel.sc.repaint();
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    state.stub1.isEnable(false);
	    state.stub2.isEnable(false);
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    //state.ignition();
	    circuitcanvas.repaint();
	    smithpanel.sc.repaint();
	    smithpanel2.sc.repaint();
	    //-----------------------------------------------------------end of start over
		//------------------------------------------
		if(smithpanel.scon.imp.getState()){
		    smithpanel2.scon.imp.setState(true);
		    smithpanel2.scon.adm.setState(false);
		    smithpanel2.set_impedance(true);
		    smithpanel2.set_admittance(false);
		    smithpanel2.sm.setMode(1);
		    smithpanel2.sm.setZL(smithpanel2.sc.getZL());
		    smithpanel2.sm.setZin(smithpanel2.sc.getZin());
		}
		else{
		    smithpanel2.scon.imp.setState(false);
		    smithpanel2.scon.adm.setState(true);
		    smithpanel2.set_impedance(false);
		    smithpanel2.set_admittance(true);
		    smithpanel2.sm.setMode(2);
		    smithpanel2.sm.setZL(smithpanel2.sc.getZL());
		    smithpanel2.sm.setYin(smithpanel2.sc.getYin());
		}
		//-------------------------------------------
		if(smithpanel.scon.swr.getState()){
		    smithpanel2.scon.swr.setState(true);
		    smithpanel2.sc.set_swr_circle(true);
		}
		else{
		    smithpanel2.scon.swr.setState(false);
		    smithpanel2.set_swr_circle(false);
		}
		//-------------------------------------------
		if(smithpanel.scon.swrline.getState()){
		    smithpanel2.scon.swrline.setState(true);
		    smithpanel2.set_swr_line(true);
		}
		else{
		    smithpanel2.scon.swrline.setState(false);
		    smithpanel2.set_swr_line(false);
		}
		//-------------------------------------------
		smithpanel2.repaint();
	    }
	    if(evt.getSource()==smithpanel2.large){
		state.IsBig = false;
		smithpanel.setVisible(true);
		psmith.setVisible(true);
		smithpanel2.setVisible(false);
		psmith2.setVisible(false);
                //------------------------------------REMOVE
		//smithpanel.small.setState(false);
	    //----------------------------------------------------start over
	    
		state.setStepNumber(1);
	    controlpanel.b2.setEnabled(true);
	    controlpanel.b2.setLabel("Next Step");
	    stepcanvas.setVisible(false);
	    inputpanel.setVisible(true);
	    //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);
	    smithpanel.setxpos(0.0);
	    
	    smithpanel2.setZL(false);
	    smithpanel2.setYL(false);
	    smithpanel2.setZin(false);
	    smithpanel2.setYin(false);
	    smithpanel2.set_swr_line(false);
	    smithpanel2.set_swr_circle(false);
	    smithpanel2.setxpos(0.0);
	    
	    state.setxpos(0.0);
	    smithpanel.sc.OverRide = false;
	    smithpanel.sc.cleanAll();
	    smithpanel2.sc.OverRide = false;
	    smithpanel2.sc.cleanAll();
	    
	    //smithpanel.sc.repaint();
	    controlpanel.b1.setEnabled(false);
	    controlpanel.b3.setEnabled(false);
	    state.stub1.isEnable(false);
	    state.stub2.isEnable(false);
	    smithpanel.setZL(state.getZL(),state.getZ0());
	    smithpanel.setZin(state.getZL(),state.getZ0());
	    smithpanel2.setZL(state.getZL(),state.getZ0());
	    smithpanel2.setZin(state.getZL(),state.getZ0());
	    //state.ignition();
	    circuitcanvas.repaint();
	    smithpanel.sc.repaint();
	    smithpanel2.sc.repaint();
	    //----------------------------------------------------end of start over
		
		//------------------------------------------
		if(smithpanel2.scon.imp.getState()){
		    smithpanel.scon.imp.setState(true);
		    smithpanel.scon.adm.setState(false);
		    smithpanel.set_impedance(true);
		    smithpanel.set_admittance(false);
		    smithpanel.sm.setMode(1);
		    smithpanel.sm.setZL(smithpanel2.sc.getZL());
		    smithpanel.sm.setZin(smithpanel2.sc.getZin());
		}
		else{
		    smithpanel.scon.imp.setState(false);
		    smithpanel.scon.adm.setState(true);
		    smithpanel.set_impedance(false);
		    smithpanel.set_admittance(true);
		    smithpanel.sm.setMode(2);
		    smithpanel.sm.setZL(smithpanel2.sc.getZL());
		    smithpanel.sm.setYin(smithpanel2.sc.getYin());
		}
		//-------------------------------------------
		if(smithpanel2.scon.swr.getState()){
		    smithpanel.scon.swr.setState(true);
		    smithpanel.set_swr_circle(true);
		}
		else{
		    smithpanel.scon.swr.setState(false);
		    smithpanel.set_swr_circle(false);
		}
		//-------------------------------------------
		if(smithpanel2.scon.swrline.getState()){
		    smithpanel.scon.swrline.setState(true);
		    smithpanel.set_swr_line(true);
		}
		else{
		    smithpanel.scon.swrline.setState(false);
		    smithpanel.set_swr_line(false);
		}
		//-------------------------------------------
		smithpanel2.repaint();
	    }
            */
    }
     
   //-------------------------------------------------------------------- 
    
    public void runStep(int x){
	 switch(x){
	   case 1: //draw impedance
		state.ignition2();
		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);
		
		newgraph.repaint();
		newgraphB.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 2: //draw SWR circle and cursor lines
		smithpanel.set_swr_circle(true);
		smithpanel.set_swr_line(true);
		smithpanel.setYL(false);
		smithpanel.sc.repaint();
		
		smithpanel2.set_swr_circle(true);
		smithpanel2.set_swr_line(true);
		smithpanel2.setYL(false);
		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);
		    
		    //draw the admittance
		    
		    smithpanel.setYL(true);
		    smithpanel.setZin(false);
		    smithpanel.setYin(false);
		    state.stub1.isEnable(false);
		    state.stub2.isEnable(false);
		    smithpanel.sc.repaint();
		
		    smithpanel2.setYL(true);
		    smithpanel2.setZin(false);
		    smithpanel2.setYin(false);
		    state.stub1.isEnable(false);
		    state.stub2.isEnable(false);
		    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 3: //begin first solution
	   
		smithpanel.set_swr_line(true);
		smithpanel2.set_swr_line(true);
		smithpanel.setYL(true);
		smithpanel.setZin(false);
		smithpanel.setYin(false);
		state.stub1.isEnable(false);
		state.stub2.isEnable(false);
		smithpanel.sc.repaint();
		
		smithpanel2.setYL(true);
		smithpanel2.setZin(false);
		smithpanel2.setYin(false);
		state.stub1.isEnable(false);
		state.stub2.isEnable(false);
		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 4:  //move to stub 1 location
		state.setZin(state.getZL());
		state.setYin(state.getYL());
		smithpanel.setYin(state.getYin(),state.getZ0());
		smithpanel.setZin(state.getZin(),state.getZ0());
                smithpanel.setYin(true);
		if(!state.getPaintZ()){
		    smithpanel.setZin(false);
		}
		else{
		    smithpanel.setZin(true);
		}
		
		smithpanel2.setYin(state.getYin(),state.getZ0());
		smithpanel2.setZin(state.getZin(),state.getZ0());
                smithpanel2.sm.setYin(state.getYin(),state.getZ0());
		smithpanel2.sm.setZin(state.getZin(),state.getZ0());
                smithpanel2.setYin(true);
		if(!state.getPaintZ()){
		    smithpanel2.setZin(false);
		}
		else{
		    smithpanel2.setZin(true);
		}
		
		if(state.getZL().Real() == 0.0){
		controlpanel.b1.setEnabled(false);
		controlpanel.b2.setEnabled(false);
		controlpanel.b3.setEnabled(true);
		}
		else{
		//smithpanel.setZin(true);
		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();
		}
		//state.stub1.isEnable(true);
		state.stub1.isEnable(false);
		state.stub2.isEnable(false);
		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 5://add stub 1
		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();
		}
		state.stub1.isEnable(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);
	    break;
	    
	    case 6: //initialize 2nd solution
                    state.setxpos(0.0);
                    smithpanel.setxpos(0.0);
                    smithpanel2.setxpos(0.0);
                    
		    smithpanel.set_swr_line(true);
		    smithpanel2.set_swr_line(true);
		    
		    smithpanel.sc.OverRide=false;
		    smithpanel2.sc.OverRide=false;
		    
		    smithpanel.setZin(false);
		    smithpanel.setYin(false);
		
		    smithpanel2.setZin(false);
		    smithpanel2.setYin(false);
		
		    smithpanel.sc.repaint();
		    smithpanel2.sc.repaint();
		    
		    state.stub1.isEnable(false);
		    state.stub2.isEnable(false);
		    
		    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 7:  //move to stub 2 location
		//smithpanel.sc.OverRide=true;
		//smithpanel2.sc.OverRide=true;
		smithpanel.set_swr_line(true);
		smithpanel2.set_swr_line(true);
		state.setZin(state.getZL());
		state.setYin(state.getYL());
		smithpanel.setYin(state.getYin(),state.getZ0());
		smithpanel.setZin(state.getZin(),state.getZ0());
                smithpanel.setYin(true);
		if(!state.getPaintZ()){
		    smithpanel.setZin(false);
		}
		else{
		    smithpanel.setZin(true);
		}
		smithpanel.sc.repaint();
		
		smithpanel2.setYin(state.getYin(),state.getZ0());
		smithpanel2.setZin(state.getZin(),state.getZ0());
                smithpanel2.sm.setYin(state.getYin(),state.getZ0());
		smithpanel2.sm.setZin(state.getZin(),state.getZ0());
                
		smithpanel2.setYin(true);
		if(!state.getPaintZ()){
		    smithpanel2.setZin(false);
		}
		else{
		    smithpanel2.setZin(true);
		}
		smithpanel2.sc.repaint();
		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();
		}
		    
		state.stub1.isEnable(false);
		//state.stub2.isEnable(true);
		state.stub2.isEnable(false);
		controlpanel.b2.setLabel("Next Step");
		
		    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:  //add stub 2
		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();
		}    
		    controlpanel.b2.setLabel("Summary");
		    state.stub2.isEnable(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);
	    break;
	    
	    case 9:  //Summary of solutions
		    smithpanel.set_swr_circle(false);
		    smithpanel.set_swr_line(false);
		    smithpanel.setZin(false);
		    smithpanel.setYin(true);
		    smithpanel.setZL(false);
		    smithpanel.setYL(false);
		    smithpanel.setxpos(0.0);
		    smithpanel.sc.repaint(); 
		    
		    smithpanel2.set_swr_circle(false);
		    smithpanel2.set_swr_line(false);
		    smithpanel2.setZin(false);
		    smithpanel2.setYin(true);
		    smithpanel2.setZL(false);
		    smithpanel2.setYL(false);
		    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);
		    
		    state.stub2.isEnable(false);
		    state.setxpos(0.0);
		    circuitcanvas.repaint();
	    break;
	 }
    }
}

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;
	  skip = (int)(state.stub1.getPosition()*144);
	  
	  if(skip < 1){skip = 1;}
	  dx = (state.stub1.getPosition())/(float)skip;
	  
	  //smithpanel.sc.OverRide=true;
	  if(state.IsTraceOn){
	    smithpanel.sc.OverRide=true;
	  }
	  else{
	    smithpanel.sc.OverRide=false;
	  }
	  smithpanel.set_swr_line(true);
	  while(i<=skip){
	     try{
	    	    state.setYin(EMF.computeYinAt(state.getYL(),state.getZ0(),i*dx,false));
		    smithpanel.setYin(state.getYin(),state.getZ0());
                    smithpanel.setxpos(i*dx);
		    state.setxpos(i*dx);
		    smithpanel.sc.repaint();
		    state.stub1.setPosition(i*dx);
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    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_2 extends Thread {
      int i=0;
      double dx, dtmp;
      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;
	 dtmp = state.getYin().Imaginary();
      }
      
      public void run(){
	//Disable control panels
	controlpanel.setEnabled(false);
	if(state.IsTraceOn){
	    smithpanel.set_swr_line(false);
	}
	else{
	    smithpanel.set_swr_line(true);
	}
	
	double skip2=120;
	double skip3=14;
	
	if(dtmp>0.0){ //Yin >0
	  if(state.stub1.isOpen()){//Open Stub
	       dx = (0.5-state.stub1.getLength())/skip2;
	  }
	  else {//Short Stub
	       dx = (0.25-state.stub1.getLength())/skip2;
	  }
	}
	else { //Yin <= 0
	  if(state.stub1.isOpen()){//Open Stub
	       dx = (state.stub1.getLength())/skip2;
	  }
	  else {//Short Stub
	       dx = (state.stub1.getLength()-0.25)/skip2;
	  }
	}
	    int isum =0;
	  
	  //smithpanel.sc.OverRide=true;
	  if(state.IsTraceOn){
	    smithpanel.sc.OverRide=true;
	  }
	  else{
	    smithpanel.sc.OverRide=false;
	  }
	  
	  while(i<=skip3){
	     try{   isum=isum+(int)(skip3-i+1);
		  if(dtmp>0.0){//Yin >0
		    if(state.stub1.isOpen()){//Open Stub
			state.stub1.setLength(0.5-dx*isum);
		    }
		    else {//Short Stub
			state.stub1.setLength(0.25-dx*isum);
		    }
		  }
		  else{//Yin < 0
		    if(state.stub1.isOpen()){//Open Stub
			state.stub1.setLength(dx*isum);
		    }
		    else {//Short Stub
			state.stub1.setLength(dx*isum+0.25);
		    }
		  }
		  
		    state.setYin(Complex.Add(EMF.computeYinAt(state.getYL(),state.getZ0(),state.stub1.getPosition(),false),state.stub1.getYstub()));
		    smithpanel.setYin(state.getYin(),state.getZ0());
                    smithpanel.sc.repaint();
		    
		    if(i==skip3){
			    state.setYin(new Complex(1.0/state.getZ0(),0.0));
                            smithpanel.setYin(state.getYin(),state.getZ0());
                    }
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
	     }
	     catch(Exception e){}
	  } smithpanel.sc.OverRide=false;
	  //smithpanel.sc.repaint();
	  //Enable controlpanel buttons
	  controlpanel.setEnabled(true);
	  smithpanel.set_swr_line(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 controlpanel buttons
	  controlpanel.setEnabled(false);
	  //double skip3 = 35;
	  int skip3;
	  skip3 = (int)(state.stub2.getPosition()*144);
	  if(skip3 < 1){skip3 =1;}
	  dx = (state.stub2.getPosition())/skip3;
	  if(state.IsTraceOn){
	    smithpanel.sc.OverRide=true;
	  }
	  else{
	    smithpanel.sc.OverRide=false;
	  }
	  
	  smithpanel.set_swr_line(true);
	  while(i<=skip3){
	     try{
		    if(i<1){sleep(800);}
	    	    state.stub2.setPosition(i*dx);
		    state.setYin(EMF.computeYinAt(state.getYL(),state.getZ0(),i*dx,false));
		    smithpanel.setYin(state.getYin(),state.getZ0());
                    
		    smithpanel.setxpos(i*dx);
		    state.setxpos(i*dx);
		    smithpanel.sc.repaint();
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    if(state.IsTraceOn){
			if(i==1) { smithpanel.set_swr_line(false); }
		    }
		    else{
			smithpanel.set_swr_line(true);
		    }
		    if(i==skip3) { smithpanel.set_swr_line(true); }
	     }
	     catch(Exception e){}
	  } smithpanel.sc.OverRide=false;
	  //smithpanel.sc.repaint();
	  //Enable controlpanel buttons
	  controlpanel.setEnabled(true);
      }      
}

class StirThread2_2 extends Thread {
      int i=0;
      double dx;
      double dtmp;
      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;
	 dtmp = state.getYin().Imaginary();
      }
      
    public void run(){
      //Disable controlpanel buttons
      controlpanel.setEnabled(false);
      if(state.IsTraceOn){
	smithpanel.set_swr_line(false);
      }
      else{
	smithpanel.set_swr_line(true);
      }
      //+double skip4 = 20;
      double skip4=120;
      double skip5=14;
      if(dtmp>0.0){ //Yin >0
	  if(state.stub2.isOpen()){//Open Stub
	       dx = (0.5-state.stub2.getLength())/skip4;
	  }
	  else {//Short Stub
	       dx = (0.25-state.stub2.getLength())/skip4;
	  }
	}
	else { //Yin < 0
	  if(state.stub2.isOpen()){//Open Stub
	       dx = (state.stub2.getLength())/skip4;
	  }
	  else {//Short Stub
	       dx = (state.stub2.getLength()-0.25)/skip4;
	  }
	}
	  int isum = 0; 
	  if(state.IsTraceOn){
	    smithpanel.sc.OverRide=true;
	  }
	  else{
	    smithpanel.sc.OverRide=false;
	  }
	  //smithpanel.sc.OverRide=true;
	  
	  while(i<=skip5){
	     try{
		     isum=isum+(int)(skip5-i+1);
		    if(dtmp>0.0){//Yin >0
		      if(state.stub2.isOpen()){//Open Stub
			state.stub2.setLength(0.5-dx*isum);
		      }
		      else {//Short Stub
			state.stub2.setLength(0.25-dx*isum);
		      }
		    }
		    else{//Yin <= 0
		       if(state.stub2.isOpen()){//Open Stub
			  state.stub2.setLength(dx*isum);
		       }
		    else {//Short Stub
			state.stub2.setLength(dx*isum+0.25);
		    }
		  }
		    state.setYin(Complex.Add(EMF.computeYinAt(state.getYL(),state.getZ0(),state.stub2.getPosition(),false),state.stub2.getYstub()));
		    smithpanel.setYin(state.getYin(),state.getZ0());
		                        
                    if(i==skip5){
			    state.setYin(new Complex(1.0/state.getZ0(),0.0));
			    smithpanel.setYin(state.getYin(),state.getZ0());
		    }
		    
		    smithpanel.sc.repaint();
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
	     }
	     catch(Exception e){}
	  } smithpanel.sc.OverRide=false;
	  //smithpanel.sc.repaint();
	  controlpanel.setEnabled(true);
      }      
}

//-----------------------  threads for smithpanel2

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;
	  skip = (int)(state.stub1.getPosition()*144);
	  
	  if(skip < 1){skip = 1;}
	  dx = (state.stub1.getPosition())/(float)skip;
	  
	  //smithpanel2.sc.OverRide=true;
	  if(state.IsTraceOn){
	    smithpanel2.sc.OverRide=true;
	  }
	  else{
	    smithpanel2.sc.OverRide=false;
	  }
	  smithpanel2.set_swr_line(true);
	  while(i<=skip){
	     try{
	    	    state.setYin(EMF.computeYinAt(state.getYL(),state.getZ0(),i*dx,false));
		    smithpanel2.setYin(state.getYin(),state.getZ0());
		    smithpanel2.setxpos(i*dx);
		    state.setxpos(i*dx);
		    smithpanel2.sc.repaint();
		    state.stub1.setPosition(i*dx);
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    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_2B extends Thread {
      int i=0;
      double dx, dtmp;
      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;
	 dtmp = state.getYin().Imaginary();
      }
      
      public void run(){
	//Disable control panels
	controlpanel.setEnabled(false);
	if(state.IsTraceOn){
	    smithpanel2.set_swr_line(false);
	}
	else{
	    smithpanel2.set_swr_line(true);
	}
	
	double skip2=120;
	double skip3=14;
	
	if(dtmp>0.0){ //Yin >0
	  if(state.stub1.isOpen()){//Open Stub
	       dx = (0.5-state.stub1.getLength())/skip2;
	  }
	  else {//Short Stub
	       dx = (0.25-state.stub1.getLength())/skip2;
	  }
	}
	else { //Yin <= 0
	  if(state.stub1.isOpen()){//Open Stub
	       dx = (state.stub1.getLength())/skip2;
	  }
	  else {//Short Stub
	       dx = (state.stub1.getLength()-0.25)/skip2;
	  }
	}
	    int isum =0;
	  
	  //smithpanel2.sc.OverRide=true;
	  if(state.IsTraceOn){
	    smithpanel2.sc.OverRide=true;
	  }
	  else{
	    smithpanel2.sc.OverRide=false;
	  }
	  
	  while(i<=skip3){
	     try{   isum=isum+(int)(skip3-i+1);
		  if(dtmp>0.0){//Yin >0
		    if(state.stub1.isOpen()){//Open Stub
			state.stub1.setLength(0.5-dx*isum);
		    }
		    else {//Short Stub
			state.stub1.setLength(0.25-dx*isum);
		    }
		  }
		  else{//Yin < 0
		    if(state.stub1.isOpen()){//Open Stub
			state.stub1.setLength(dx*isum);
		    }
		    else {//Short Stub
			state.stub1.setLength(dx*isum+0.25);
		    }
		  }
		  
		    state.setYin(Complex.Add(EMF.computeYinAt(state.getYL(),state.getZ0(),state.stub1.getPosition(),false),state.stub1.getYstub()));
		    smithpanel2.setYin(state.getYin(),state.getZ0());
		    smithpanel2.sc.repaint();
		    
		    if(i==skip3){
			    state.setYin(new Complex(1.0/state.getZ0(),0.0));
			    smithpanel2.setYin(state.getYin(),state.getZ0());
		    }
		    
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
	     }
	     catch(Exception e){}
	  } smithpanel2.sc.OverRide=false;
	  //smithpanel2.sc.repaint();
	  //Enable controlpanel buttons
	  controlpanel.setEnabled(true);
	  smithpanel2.set_swr_line(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 controlpanel buttons
	  controlpanel.setEnabled(false);
	  //double skip3 = 35;
	  int skip3;
	  skip3 = (int)(state.stub2.getPosition()*144);
	  if(skip3 < 1){skip3 =1;}
	  dx = (state.stub2.getPosition())/skip3;
	  if(state.IsTraceOn){
	    smithpanel2.sc.OverRide=true;
	  }
	  else{
	    smithpanel2.sc.OverRide=false;
	  }
	  
	  smithpanel2.set_swr_line(true);
	  while(i<=skip3){
	     try{
		    if(i<1){sleep(800);}
	    	    state.stub2.setPosition(i*dx);
		    state.setYin(EMF.computeYinAt(state.getYL(),state.getZ0(),i*dx,false));
		    smithpanel2.setYin(state.getYin(),state.getZ0());
		    smithpanel2.setxpos(i*dx);
		    state.setxpos(i*dx);
		    smithpanel2.sc.repaint();
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
		    if(state.IsTraceOn){
			if(i==1) { smithpanel2.set_swr_line(false); }
		    }
		    else{
			smithpanel2.set_swr_line(true);
		    }
		    if(i==skip3) { smithpanel2.set_swr_line(true); }
	     }
	     catch(Exception e){}
	  } smithpanel2.sc.OverRide=false;
	  //smithpanel2.sc.repaint();
	  //Enable controlpanel buttons
	  controlpanel.setEnabled(true);
      }      
}

class StirThread2_2B extends Thread {
      int i=0;
      double dx;
      double dtmp;
      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;
	 dtmp = state.getYin().Imaginary();
      }
      
    public void run(){
      //Disable controlpanel buttons
      controlpanel.setEnabled(false);
      if(state.IsTraceOn){
	smithpanel2.set_swr_line(false);
      }
      else{
	smithpanel2.set_swr_line(true);
      }
      //+double skip4 = 20;
      double skip4=120;
      double skip5=14;
      if(dtmp>0.0){ //Yin >0
	  if(state.stub2.isOpen()){//Open Stub
	       dx = (0.5-state.stub2.getLength())/skip4;
	  }
	  else {//Short Stub
	       dx = (0.25-state.stub2.getLength())/skip4;
	  }
	}
	else { //Yin < 0
	  if(state.stub2.isOpen()){//Open Stub
	       dx = (state.stub2.getLength())/skip4;
	  }
	  else {//Short Stub
	       dx = (state.stub2.getLength()-0.25)/skip4;
	  }
	}
	  int isum = 0; 
	  if(state.IsTraceOn){
	    smithpanel2.sc.OverRide=true;
	  }
	  else{
	    smithpanel2.sc.OverRide=false;
	  }
	  //smithpanel2.sc.OverRide=true;
	  
	  while(i<=skip5){
	     try{
		     isum=isum+(int)(skip5-i+1);
		    if(dtmp>0.0){//Yin >0
		      if(state.stub2.isOpen()){//Open Stub
			state.stub2.setLength(0.5-dx*isum);
		      }
		      else {//Short Stub
			state.stub2.setLength(0.25-dx*isum);
		      }
		    }
		    else{//Yin <= 0
		       if(state.stub2.isOpen()){//Open Stub
			  state.stub2.setLength(dx*isum);
		       }
		    else {//Short Stub
			state.stub2.setLength(dx*isum+0.25);
		    }
		  }
		    state.setYin(Complex.Add(EMF.computeYinAt(state.getYL(),state.getZ0(),state.stub2.getPosition(),false),state.stub2.getYstub()));
		    smithpanel2.setYin(state.getYin(),state.getZ0());
		    if(i==skip5){
			    state.setYin(new Complex(1.0/state.getZ0(),0.0));
			    smithpanel2.setYin(state.getYin(),state.getZ0());
		    }
		    
		    smithpanel2.sc.repaint();
		    circuitcanvas.repaint();
		    sleep(state.sleeptime); 
		    i++;
	     }
	     catch(Exception e){}
	  } smithpanel2.sc.OverRide=false;
	  //smithpanel2.sc.repaint();
	  controlpanel.setEnabled(true);
      }      
}
