//Chart.java
//Interactive Transmission Line SmithChart
//author Umberto Ravaioli, 2005

import java.awt.*;
import java.awt.event.*;
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 Chart extends Frame implements AdjustmentListener, ActionListener, ItemListener, MouseListener, MouseMotionListener, WindowListener{
    Instructions instructions;
    Panel instrFrame;
    TitlePanel titlepanel;
    //About about;
    Auxiliary auxiliary;
    Auxiliary2 auxiliary2;
    Auxiliary3 auxiliary3;
    Auxiliary4 auxiliary4;
    Auxiliary5 auxiliary5;
    
    SmithPanel2 sp2;
    TransSlidePanel slidepanel;
    Trans_State state;
    TransInputPanel linepanel;
    TransLoadPanel loadpanel;
    Arc_red arcred;
    Arc_green arcgreen;
    line line;
   
    Trans_MultiGraphPanelA mgpa;
    Trans_MultiGraphPanelB mgpb;
    
    Panel psmith2;
    //Image image;
    //private Graphics buf;
    public Panel pplota, pplotb, paux5;        
    // CHANGE ==================================================================
    public int xmove = 10;
    public int ymove = 34;
    //==========================================================================
    
    Font ttfFont, phiFont, italicFont;

    private static final boolean useOldFonts = true;  
    
    public static void main(String[] args){
        Chart f = new Chart();
        int xxmove = 20;
        int yymove = 41;
        f.setSize(866+xxmove,621+yymove);
        //f.setSize(960,712);
        f.setVisible(true);
        f.setLayout(null);
    }

  public Chart(){
 
    setLayout(null);
    String lcOSName = System.getProperty("os.name").toLowerCase();
    boolean MAC_OS_X = lcOSName.startsWith("mac os x");
    //System.out.println(lcOSName);
    
    if (!useOldFonts) {
        //getUlabyFonts();
        state = new Trans_State();
        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 Trans_State();
        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();
    }
    
    // CHANGE ==================================================================
    //int xmove = 10;
    //int ymove = 34;
    //==========================================================================
    
   // image = getImage(getDocumentBase(),"copyright.gif");
    

    //titlepanel = new TitlePanel(subtitleFont,"Module 2.6","Interactive Smith Chart");
    titlepanel = new TitlePanel(state.ttfFont.deriveFont(Font.BOLD,15f),
                                "Module 2.6","Interactive Smith Chart");
    //add(titlepanel);
    //titlepanel.setBounds(10,9,294,41);
    titlepanel.setBounds(10+xmove,10+ymove,557,43);


    auxiliary = new Auxiliary(state);
    auxiliary.setBounds(600+20+xmove,302-179+ymove,150,15);
    auxiliary.setVisible(true);
    
    auxiliary2 = new Auxiliary2(state);
    auxiliary2.setBounds(600+20+xmove,337-179+ymove,150,15);
    auxiliary2.setVisible(true);
    
    auxiliary3 = new Auxiliary3(state);
    auxiliary3.setBounds(600+20+xmove,372-179+ymove,150,15);
    auxiliary3.setVisible(true);
    
    instructions = new Instructions(state);
    instructions.setBounds(7+xmove,7+ymove,565,604);
    instrFrame = new Panel();
    instrFrame.setBackground(Color.black);
    //instrFrame.setBounds(7,7,564,604);
    //instrFrame.setVisible(false);

    sp2 = new SmithPanel2(state);
    sp2.setBounds(8+xmove,8+ymove,850,602);
    sp2.setxpos(state.xpos);
    sp2.setZin(state.Zin,state.Z0);
    
    sp2.setZL(state.ZL,state.Z0);
   
    sp2.sm.setGL(state.GammaL);
    sp2.setVisible(true);
    
    slidepanel = new TransSlidePanel();
    slidepanel.setBounds(8+xmove,8+ymove,564,39);
    slidepanel.setLineLength(state.lineLength, state.lineLength_part1, state.lineLength_part2, state.linecounter1, state.linecounter2);
    
    {
     String[] str3 =  {"Z"};
     String[] str4 =  {"[ \u03a9 ]"};
     linepanel = new TransInputPanel(" ",str3,str4,state);
     linepanel.setValue(state.Z0,0);
     linepanel.setBounds(577+xmove,422+ymove,274,50);
    }
    
    {
      String[] str1 =  {"Z","Y"};
      String[] str2 =  {"[ \u03a9 ]","1"};
      loadpanel = new TransLoadPanel("Set Load",str1,str2,state);
      loadpanel.setValue(state.ZL,0);
      loadpanel.setBounds(577+xmove,479+ymove,274,126);
    }
    
    {
	loadpanel.setVisible(true);
	linepanel.setVisible(true);
    }
    
    arcred = new Arc_red();
    arcred.setBounds(577+15+xmove,195+143+ymove,18,10);
    
    arcgreen = new Arc_green();
    arcgreen.setBounds(577+15+xmove,225+143+ymove,18,10);
    
    mgpa = new Trans_MultiGraphPanelA("");
    //add(mgpa);
    // CHANGE -  KEEP INITIALLY AWAY DUE TO setVisible problem with MAC ========
    //mgpa.setBounds(575+xmove,276+ymove,278,283);
    mgpa.setBounds(575+xmove+10000,276+ymove,278,283);
    //==========================================================================
    
    mgpb = new Trans_MultiGraphPanelB("");
    //add(mgpb);
    // CHANGE -  KEEP INITIALLY AWAY DUE TO setVisible problem with MAC ========
    //mgpb.setBounds(575+xmove,12+ymove,278,268);
    mgpb.setBounds(575+xmove+10000,12+ymove,278,268);
    //==========================================================================
    
    auxiliary4 = new Auxiliary4(state);
    auxiliary4.setBounds(575+xmove,534+ymove,278,25);
    auxiliary4.setVisible(true);
    
    auxiliary5 = new Auxiliary5(state);
    auxiliary5.setBounds(575+xmove,560+ymove,278,45);
    auxiliary5.setVisible(true);
    
    paux5 = new Panel();
	    paux5.setBackground(Color.black);
	    paux5.setBounds(574+xmove,559+ymove,280,47);
    
    pplota = new Panel();
	    pplota.setBackground(Color.black);
	    pplota.setBounds(574+xmove,11+ymove,280,269);
    
    pplotb = new Panel();
	    pplotb.setBackground(Color.black);
	    pplotb.setBounds(574+xmove,276+ymove,280,284);
        
    Panel pgraph = new Panel();
	    pgraph.setBackground(Color.black);
	    pgraph.setBounds(10+xmove,235+ymove,900,288);
    	    
    psmith2 = new Panel();
	    psmith2.setBackground(Color.black);
	    psmith2.setBounds(7+xmove,7+ymove,852,604);
	    psmith2.setVisible(true);
    
    Panel pload = new Panel();
	    pload.setBackground(Color.black);
	    pload.setBounds(576+xmove,478+ymove,276,128);
	    
    Panel pline = new Panel();
	    pline.setBackground(Color.black);
	    pline.setBounds(576+xmove,421+ymove,276,52);
    
    Panel p0 = new Panel();
	    p0.setBackground(Color.cyan);
	    p0.setBounds(3+xmove,3+ymove,860,612);
	    
    Panel p00 = new Panel();
	    p00.setBackground(Color.black);
	    p00.setBounds(xmove,ymove,866,618);
	
            auxiliary4.setVisible(false);
            auxiliary5.setVisible(false);
            mgpb.setVisible(false);
            mgpa.setVisible(false);

            pplota.setVisible(false);
            pplotb.setVisible(false);
            paux5.setVisible(false);

            //if(state.this_year > line.magicyear || (state.this_year == line.magicyear && state.this_month > line.magicmonth)){
	    //state.LicenseExpired = true;
            //}
            //else
            //{   
            add(titlepanel);  // LATER .....................
            add(instructions);
            add(instrFrame);
            //add(about);
            add(auxiliary4);
            add(auxiliary5);
            add(mgpb);
            add(mgpa);
            
            add(pplota);
            add(pplotb);
            add(paux5);
            
	    add(auxiliary);
	    add(auxiliary2);
	    add(auxiliary3);
            
	    add(slidepanel); 	
	    //add(pslide);
	    add(loadpanel);
	    add(pload);
	    add(linepanel);
	    
            add(pline);
	    add(arcred);
	    add(arcgreen);          
            add(sp2); 	 	    
	    
            add(psmith2);    
            add(p0); 	    
            add(p00);
	    
	    state.LicenseExpired = false; 	
            //}
    
    //Listeners     
    this.addWindowListener(this);
    instructions.bupdate.addActionListener(this);        
    slidepanel.slider.addAdjustmentListener(this);
    slidepanel.slider2.addAdjustmentListener(this);
    // CHANGE ==================================================================
    slidepanel.b2a.addActionListener(this);
    slidepanel.b2b.addActionListener(this);
    slidepanel.b3a.addActionListener(this);
    slidepanel.b3b.addActionListener(this);
    //==========================================================================
    linepanel.b1.addActionListener(this);
    loadpanel.b1.addActionListener(this);
    //loadpanel.b2a.addActionListener(this);
    //loadpanel.b2b.addActionListener(this);
    //loadpanel.b3a.addActionListener(this);
    //loadpanel.b3b.addActionListener(this);
    //loadpanel.c1.addItemListener(this);
    //loadpanel.c2.addItemListener(this);
    //loadpanel.c3.addItemListener(this);
    sp2.large.addItemListener(this);
    //sp2.scon.imp.addItemListener(this);
    //sp2.scon.adm.addItemListener(this);
    sp2.scon.swr.addItemListener(this);
    sp2.scon.swrline.addItemListener(this);
    sp2.sc.addMouseListener(this);
    sp2.sc.addMouseMotionListener(this);
    loadpanel.slider1.addAdjustmentListener(this);
    loadpanel.slider2.addAdjustmentListener(this);
    loadpanel.slider3.addAdjustmentListener(this);
    loadpanel.slider4.addAdjustmentListener(this);
    sp2.colore.addActionListener(this);
    sp2.about.addActionListener(this);

    //inizialization 
        
	doupdate();
	state.ignition();
        //state.ScanVoltageCurrent();
        mgpa.plotVoltageCurrent(state);
        mgpa.tgc3.repaint();
        mgpa.tgc4.repaint();
        
        mgpa.tgc3.setVisible(true);
	mgpa.tgc4.setVisible(true);
        mgpa.repaint();

        if(state.use_Zchart){
            mgpb.plotImpedance(state);
        }
        else{
            mgpb.plotAdmittance(state);
        }
        mgpb.tgc1.repaint();
        mgpb.tgc2.repaint();
        
        mgpb.tgc1.setVisible(true);
	mgpb.tgc2.setVisible(true);
        mgpb.repaint();
    
}//End of init()


/*
public void paint(Graphics g){
    
    g.setColor(Color.white);	
    g.fillRect(0,0,945,765);
    g.drawImage(image,5,535,this);	
}
*/


/*
 public void paint(Graphics g){
    
    Font licensefont = new Font("SanSerif",Font.BOLD,18); 	
	g.setColor(Color.white);	 	
	g.fillRect(0,0,796,606); 	
	//g.drawImage(image,5,535,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);
	} 
}//End of paint
*/
  
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,886,630);  	
	/*	
	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,622);
        //g.setFont(fonto);
        //g.drawString("Amanogawa, "+annonuovo+" - All Rights Reserved",30,620);
        */
}



public void start(){}


public void stop(){}

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){
	
        Font symbolfont  = new Font("Symbol",Font.PLAIN,12);
	Font labfont     = new Font("SanSerif",Font.PLAIN,12);
	
	String alpha, Ohm, lambda, infinity, Gamma;
	    setFont(symbolfont);
	    alpha="\u03b1";
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    infinity="\u221e";
	    Gamma="\uu0393";
	    setFont(labfont);
    
    if(evt.getSource()==loadpanel.slider1 || evt.getSource()==loadpanel.slider2){
	    
	    if(loadpanel.c1.getState()){//Impedance choice is on
		
		double temp1, temp2;
		Complex tempc;
		
		tempc = new Complex(Double.valueOf(loadpanel.text1[0].getText()).doubleValue(),
		                     Double.valueOf(loadpanel.text2[0].getText()).doubleValue());
		temp1 = Complex.Real(tempc);
		temp2 = Complex.Imaginary(tempc);
		
		//avoid input of negative value				
		if(temp1 < 0.0 && (temp2 != 0.0 || temp2 < -1.0E130)){ //Negative R - transform Z to imaginary impedance
		    state.ZL = new Complex(0.0,temp2);
		    state.YL = new Complex(0.0,-1.0/temp2);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else if(temp1 <= 0.0 && temp2 == 0.0){// Negative R and zero X, transform to short
		    state.ZL = new Complex(0.0,0.0);
		    state.YL = new Complex(0.0,-1.0E135);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = true; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 0.0 && temp2 < -1.0E130){// Open
		    state.YL = new Complex(0.0,-1.0E135);
		    state.ZL = new Complex(0.0,0.0);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 0.0 && temp2!=0.0){// Purely Imaginary Impedance
		    state.ZL = new Complex(0.0,temp2);
		    state.YL = new Complex(0.0,-1.0/temp2);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else{// Regular Impedance
		    state.ZL = new Complex(temp1,temp2);
		    state.YL = EMF.Inv(state.ZL);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
		}
		state.is_Load_Ztype = true;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
	    }
	    else if(loadpanel.c2.getState()){
		double temp1, temp2;
		Complex tempc;
		
		tempc = new Complex(Double.valueOf(loadpanel.text1[0].getText()).doubleValue(),
		                     Double.valueOf(loadpanel.text2[0].getText()).doubleValue());
		temp1 = Complex.Real(tempc);
		temp2 = Complex.Imaginary(tempc);
		
	    //avoid input of negative value				
		if(temp1 < 0.0 && temp2 != 0.0){ //Negative R - transform Z to imaginary impedance
		    state.YL = new Complex(0.0,temp2);
		    state.ZL = new Complex(0.0,-1.0/temp2);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else if(temp1 <= 0.0 && temp2 == 0.0){// Negative R and zero X, transform to open
		    state.YL = new Complex(0.0,0.0);
		    state.ZL = new Complex(0.0,-1.0E135);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 0.0 && temp2 < -1.0E130){// Short
		    state.YL = new Complex(0.0,-1.0E135);
		    state.ZL = new Complex(0.0,0.0);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = true; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 0.0 && temp2 !=0.0){// Purely Imaginary Impedance
		    state.YL = new Complex(0.0,temp2);
		    state.ZL = new Complex(0.0,-1.0/temp2);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else{// Regular Admittance
		    state.YL = new Complex(temp1,temp2);
		    state.ZL = EMF.Inv(state.YL);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
		}
		state.is_Load_Ztype = false;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
	    }
	    
	    state.ignition();
            
            //state.ScanVoltageCurrent();
            auxiliary5.repaint();
            
            mgpa.plotVoltageCurrent(state);
            mgpa.tgc3.setXpos(state.xpos);
            mgpa.tgc4.setXpos(state.xpos);

            mgpa.tgc3.repaint();
            mgpa.tgc4.repaint();

            mgpa.tgc3.setVisible(true);
            mgpa.tgc4.setVisible(true);
            mgpa.repaint();
            
            if(state.use_Zchart){
                mgpb.plotImpedance(state);
            }
            else{
                mgpb.plotAdmittance(state);
            }
            mgpb.tgc1.setXpos(state.xpos);
            mgpb.tgc2.setXpos(state.xpos);

            mgpb.tgc1.repaint();
            mgpb.tgc2.repaint();

            mgpb.tgc1.setVisible(true);
            mgpb.tgc2.setVisible(true);
            mgpb.repaint();

	    sp2.setGammaFlag(state.IsLargerThanOne);
	    
	    sp2.setxpos(state.xpos);
	    if(loadpanel.c1.getState()){//Impedance choice is on
		sp2.setZin(state.Zin,state.Z0);
		sp2.setZL(state.ZL,state.Z0);
		sp2.sm.setGL(state.GammaL);
		sp2.setYin(state.Yin,state.Z0);
		state.is_Load_Ztype = true;
	    }
	    else if(loadpanel.c2.getState()){//Admittance choice is on
		sp2.setYin(state.Yin,state.Z0);
		sp2.setYL(state.YL,state.Z0);
		state.is_Load_Ztype = false;
	    }
	    else if(loadpanel.c3.getState()){//Reflection Coefficient choice is on
		sp2.setZin(state.Zin,state.Z0);
		sp2.setZL(state.ZL,state.Z0);
		sp2.sm.setGL(state.GammaL);
		sp2.setYin(state.Yin,state.Z0);
		state.is_Load_Ztype = true;
	    }
	    auxiliary.repaint();
	    auxiliary2.repaint();
	    auxiliary3.repaint();
    }
    else if(evt.getSource()==loadpanel.slider3 || evt.getSource()==loadpanel.slider4 ){
    
	    if(loadpanel.c3.getState()){//Gamma choice is on
		double temp1, temp2;
		temp1 = Double.valueOf(loadpanel.text1[1].getText()).doubleValue();
		temp2 = Double.valueOf(loadpanel.text2[1].getText()).doubleValue();
		
		//avoid input of negative value				
		if(temp1 < 0.0 ){ //Negative R - transform Z to imaginary impedance
		    state.GammaL = new Complex(0.0,0.0);
		    state.ZL = new Complex(state.Z0.Real(),0.0);
		    state.YL = new Complex(1.0/state.Z0.Real(),0.0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
		}
		else if(temp1 == 1.0 && temp2 == 0.0){//  open
		    state.YL = new Complex(0.0,0.0);
		    state.ZL = new Complex(0.0,-1.0E135);
		    //loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 1.0 && (temp2 > 179.9999 || temp2 < -179.9999)){// short
		    state.YL = new Complex(0.0,-1.0E135);
		    state.ZL = new Complex(0.0,0.0);
		    //loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = true; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 1.0 && (temp2!=0.0 || (temp2 > -179.9999 && temp2 < 179.9999))){// Purely Imaginary Impedance
		    //loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else{// Regular Impedance
		    //state.ZL = new Complex(temp1,temp2);
		    //state.YL = EMF.Inv(state.ZL);
		    //loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
		}
		state.is_Load_Ztype = true;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
	    }
	    
	    
	    sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
	    state.ignition();
	    sp2.setGammaFlag(state.IsLargerThanOne);
	    
	    sp2.setxpos(state.xpos);
	    sp2.setZin(state.Zin,state.Z0);
	    sp2.setZL(state.ZL,state.Z0);
		
	    sp2.sm.setGL(state.GammaL);
	    sp2.setYin(state.Yin,state.Z0);

            state.ignition();
            
            auxiliary5.repaint();
            
            //state.ScanVoltageCurrent();
            mgpa.plotVoltageCurrent(state);
            mgpa.tgc3.setXpos(state.xpos);
            mgpa.tgc4.setXpos(state.xpos);

            mgpa.tgc3.repaint();
            mgpa.tgc4.repaint();

            mgpa.tgc3.setVisible(true);
            mgpa.tgc4.setVisible(true);
            mgpa.repaint();
            
            if(state.use_Zchart){
                mgpb.plotImpedance(state);
            }
            else{
                mgpb.plotAdmittance(state);
            }
            mgpb.tgc1.setXpos(state.xpos);
            mgpb.tgc2.setXpos(state.xpos);

            mgpb.tgc1.repaint();
            mgpb.tgc2.repaint();

            mgpb.tgc1.setVisible(true);
            mgpb.tgc2.setVisible(true);
            mgpb.repaint();

	    auxiliary.repaint();
	    auxiliary2.repaint();
	    auxiliary3.repaint();
    }
    
    if(evt.getSource()==slidepanel.slider){
	slidepanel.slider2.setValue(slidepanel.slider2.getMaximum());
	state.xpos = slidepanel.getValue();
	if(state.xpos == state.lineLength){
	    slidepanel.slider2.setEnabled(false);  
	}
	else{
	    slidepanel.slider2.setEnabled(true);
	}
	send_shock_wave();
	sp2.setZin(state.Zin, state.Z0);
	sp2.setYin(state.Yin,state.Z0);
        
        state.ignition();
        auxiliary5.repaint();
            
        //state.ScanVoltageCurrent();
        mgpa.plotVoltageCurrent(state);
        mgpa.tgc3.setXpos(state.xpos);
        mgpa.tgc4.setXpos(state.xpos);

        mgpa.tgc3.repaint();
        mgpa.tgc4.repaint();
        
        mgpa.tgc3.setVisible(true);
	mgpa.tgc4.setVisible(true);
        mgpa.repaint();
        
        if(state.use_Zchart){
            mgpb.plotImpedance(state);
        }
        else{
            mgpb.plotAdmittance(state);
        }
        mgpb.tgc1.setXpos(state.xpos);
        mgpb.tgc2.setXpos(state.xpos);

        mgpb.tgc1.repaint();
        mgpb.tgc2.repaint();

        mgpb.tgc1.setVisible(true);
        mgpb.tgc2.setVisible(true);
        mgpb.repaint();

    }
    
    
    if(evt.getSource()==slidepanel.slider2){
	
	if(slidepanel.slider.getValue() == 0){
	    if(slidepanel.slider2.getValue() < (slidepanel.SCROLLMAX2 - state.linecounter2)){
		slidepanel.slider2.setValue(slidepanel.SCROLLMAX2 - state.linecounter2-1);
	    }
	}
	state.xpos = slidepanel.getValue()+slidepanel.getValue2();
	send_shock_wave();
	
	sp2.setZin(state.Zin, state.Z0);
	sp2.setYin(state.Yin,state.Z0);	
        
        state.ignition();
        auxiliary5.repaint();
            
        //state.ScanVoltageCurrent();
        mgpa.plotVoltageCurrent(state);
        mgpa.tgc3.setXpos(state.xpos);
        mgpa.tgc4.setXpos(state.xpos);

        mgpa.tgc3.repaint();
        mgpa.tgc4.repaint();
        
        mgpa.tgc3.setVisible(true);
	mgpa.tgc4.setVisible(true);
        mgpa.repaint();

        if(state.use_Zchart){
            mgpb.plotImpedance(state);
        }
        else{
            mgpb.plotAdmittance(state);
        }
        mgpb.tgc1.setXpos(state.xpos);
        mgpb.tgc2.setXpos(state.xpos);

        mgpb.tgc1.repaint();
        mgpb.tgc2.repaint();

        mgpb.tgc1.setVisible(true);
        mgpb.tgc2.setVisible(true);
        mgpb.repaint();

    }
    
}

public void actionPerformed(ActionEvent evt){
	
    //if(evt.getSource()==about.bupdate){
    //          about.setVisible(false);
    //  }
      
        if(evt.getSource()==slidepanel.b2a || evt.getSource()==slidepanel.b2b){
            System.out.println("HERE");
            slidepanel.slider2.setValue(slidepanel.slider2.getMaximum());
            state.xpos = slidepanel.getValue();
            if(state.xpos == state.lineLength){
                slidepanel.slider2.setEnabled(false);  
            }
            else{
                slidepanel.slider2.setEnabled(true);
            }
            send_shock_wave();
            sp2.setZin(state.Zin, state.Z0);
            sp2.setYin(state.Yin,state.Z0);

            state.ignition();
            auxiliary5.repaint();

            //state.ScanVoltageCurrent();
            mgpa.plotVoltageCurrent(state);
            mgpa.tgc3.setXpos(state.xpos);
            mgpa.tgc4.setXpos(state.xpos);

            mgpa.tgc3.repaint();
            mgpa.tgc4.repaint();

            mgpa.tgc3.setVisible(true);
            mgpa.tgc4.setVisible(true);
            mgpa.repaint();

            if(state.use_Zchart){
                mgpb.plotImpedance(state);
            }
            else{
                mgpb.plotAdmittance(state);
            }
            mgpb.tgc1.setXpos(state.xpos);
            mgpb.tgc2.setXpos(state.xpos);

            mgpb.tgc1.repaint();
            mgpb.tgc2.repaint();

            mgpb.tgc1.setVisible(true);
            mgpb.tgc2.setVisible(true);
            mgpb.repaint();
        }
            
        if(evt.getSource()==slidepanel.b3a || evt.getSource()==slidepanel.b3b){
            if(slidepanel.slider.getValue() == 0){
                if(slidepanel.slider2.getValue() < (slidepanel.SCROLLMAX2 - state.linecounter2)){
                    slidepanel.slider2.setValue(slidepanel.SCROLLMAX2 - state.linecounter2-1);
                }
            }
            state.xpos = slidepanel.getValue()+slidepanel.getValue2();
            send_shock_wave();

            sp2.setZin(state.Zin, state.Z0);
            sp2.setYin(state.Yin,state.Z0);	

            state.ignition();
            auxiliary5.repaint();

            //state.ScanVoltageCurrent();
            mgpa.plotVoltageCurrent(state);
            mgpa.tgc3.setXpos(state.xpos);
            mgpa.tgc4.setXpos(state.xpos);

            mgpa.tgc3.repaint();
            mgpa.tgc4.repaint();

            mgpa.tgc3.setVisible(true);
            mgpa.tgc4.setVisible(true);
            mgpa.repaint();

            if(state.use_Zchart){
                mgpb.plotImpedance(state);
            }
            else{
                mgpb.plotAdmittance(state);
            }
            mgpb.tgc1.setXpos(state.xpos);
            mgpb.tgc2.setXpos(state.xpos);

            mgpb.tgc1.repaint();
            mgpb.tgc2.repaint();

            mgpb.tgc1.setVisible(true);
            mgpb.tgc2.setVisible(true);
            mgpb.repaint();

        }
              
    
      if(evt.getSource()==instructions.bupdate){ 
          titlepanel.setVisible(false);
          instructions.setVisible(false);
          instrFrame.setVisible(false);
      }
        
      if(evt.getSource()==sp2.about){ 
          //about.setVisible(true);
          titlepanel.setVisible(true);
          instructions.setVisible(true);
          instrFrame.setVisible(true);
      }
        
	if(evt.getSource()==linepanel.b1){
	    state.IsLowLoss = true;
		double tempo1, tempo2;
		tempo1 = Complex.Real(linepanel.getValue(0));
		tempo2 = Complex.Imaginary(linepanel.getValue(0));
		
		//avoid input of negative resistance
		if(tempo1 < 0.0){
		    state.Z0 = new Complex(50.0,0.0);
		    linepanel.setValue(state.Z0,0);
		}
		else{
		    state.Z0 = linepanel.getValue(0);
		}
	    
	    state.xpos = 0.0;
	    state.ignition();
	    
			    
	    slidepanel.reset();
	    
	    sp2.setxpos(state.xpos);
	    sp2.setalpha(state.alpha);
	    if(state.is_Load_Ztype){
		sp2.setZL(state.ZL,state.Z0);
		
		sp2.sm.setGL(state.GammaL);
	    }
	    else{
		sp2.setYL(state.YL,state.Z0);
	    }
	    
	    sp2.setZin(state.Zin,state.Z0);
	   
	    sp2.setYin(state.Yin,state.Z0);
	   
	    auxiliary.repaint();
	    auxiliary2.repaint();
	    auxiliary3.repaint();
	    
	    doupdate();
            
            state.ignition();
            auxiliary5.repaint();
            
            //state.ScanVoltageCurrent();
            mgpa.plotVoltageCurrent(state);
            mgpa.tgc3.setXpos(state.xpos);
            mgpa.tgc4.setXpos(state.xpos);

            mgpa.tgc3.repaint();
            mgpa.tgc4.repaint();

            mgpa.tgc3.setVisible(true);
            mgpa.tgc4.setVisible(true);
            mgpa.repaint();

	    if(state.use_Zchart){
                mgpb.plotImpedance(state);
            }
            else{
                mgpb.plotAdmittance(state);
            }
            mgpb.tgc1.setXpos(state.xpos);
            mgpb.tgc2.setXpos(state.xpos);

            mgpb.tgc1.repaint();
            mgpb.tgc2.repaint();

            mgpb.tgc1.setVisible(true);
            mgpb.tgc2.setVisible(true);
            mgpb.repaint();

	}
          
	else if(evt.getSource()==loadpanel.b1  
                //|| evt.getSource()==loadpanel.b2a ||
                //evt.getSource()==loadpanel.b2b || evt.getSource()==loadpanel.b3a ||
                //evt.getSource()==loadpanel.b3b
                ){
	
	    doupdate();
            state.ignition();
            auxiliary5.repaint();
            
            //state.ScanVoltageCurrent();
            mgpa.plotVoltageCurrent(state);
            mgpa.tgc3.setXpos(state.xpos);
            mgpa.tgc4.setXpos(state.xpos);

            mgpa.tgc3.repaint();
            mgpa.tgc4.repaint();

            mgpa.tgc3.setVisible(true);
            mgpa.tgc4.setVisible(true);
            mgpa.repaint();
            
            if(state.use_Zchart){
                mgpb.plotImpedance(state);
            }
            else{
                mgpb.plotAdmittance(state);
            }
            mgpb.tgc1.setXpos(state.xpos);
            mgpb.tgc2.setXpos(state.xpos);

            mgpb.tgc1.repaint();
            mgpb.tgc2.repaint();

            mgpb.tgc1.setVisible(true);
            mgpb.tgc2.setVisible(true);
            mgpb.repaint();
	}
        
        if(evt.getSource()==sp2.colore){
            if(sp2.colore_dark){
                sp2.colore_dark=false;
                sp2.sc.setColoreDark(false);
            }
            else{
                sp2.colore_dark=true;
                sp2.sc.setColoreDark(true);
            }
        }
}

public void itemStateChanged(ItemEvent evt){
	    ItemSelectable ie = evt.getItemSelectable();
	    
            if(evt.getSource()==sp2.large){
		if(state.IsPlots){
                    state.IsPlots = false;
                    auxiliary4.setVisible(false);
                    auxiliary5.setVisible(false);
                    mgpb.setVisible(false);
                    mgpa.setVisible(false);
                    
                    // CHANGE ==================================================
                    mgpa.setBounds(575+xmove+10000,276+ymove,278,283);
                    mgpb.setBounds(575+xmove+10000,12+ymove,278,268);
                    //==========================================================
                    
                    pplota.setVisible(false);
                    pplotb.setVisible(false);
                    paux5.setVisible(false);
                }
                else{
                    state.IsPlots = true;
                    auxiliary4.setVisible(true);
                    auxiliary5.setVisible(true);
                    mgpb.setVisible(true);
                    mgpa.setVisible(true);
                    
                    // CHANGE ==================================================
                    mgpa.setBounds(575+xmove,276+ymove,278,283);
                    mgpb.setBounds(575+xmove,12+ymove,278,268);
                    //==========================================================
                    
                    pplota.setVisible(true);
                    pplotb.setVisible(true);
                    paux5.setVisible(true);
                }
	    }
            
	    if(evt.getSource()==loadpanel.c1){
		loadpanel.repaint();
	    }
	    
	    if(evt.getSource()==loadpanel.c2){
		loadpanel.repaint();
	    }
	    
	    if(evt.getSource()==loadpanel.c3){
		loadpanel.repaint();
	    }
            
            if(evt.getSource()==sp2.scon.imp){
		state.use_Zchart = true;
                state.ignition();
                auxiliary5.repaint();

                //state.ScanVoltageCurrent();
                mgpa.plotVoltageCurrent(state);
                mgpa.tgc3.setXpos(state.xpos);
                mgpa.tgc4.setXpos(state.xpos);

                mgpa.tgc3.repaint();
                mgpa.tgc4.repaint();

                mgpa.tgc3.setVisible(true);
                mgpa.tgc4.setVisible(true);
                mgpa.repaint();

                if(state.use_Zchart){
                    mgpb.plotImpedance(state);
                }
                else{
                    mgpb.plotAdmittance(state);
                }
                mgpb.tgc1.setXpos(state.xpos);
                mgpb.tgc2.setXpos(state.xpos);

                mgpb.tgc1.repaint();
                mgpb.tgc2.repaint();

                mgpb.tgc1.setVisible(true);
                mgpb.tgc2.setVisible(true);
                mgpb.repaint();
	    }
            
            //if(evt.getSource()==sp2.scon.adm){
            //	state.use_Zchart = false;
            //    state.ignition();
            //    auxiliary5.repaint();

            //    //state.ScanVoltageCurrent();
            //    mgpa.plotVoltageCurrent(state);
            //    mgpa.tgc3.setXpos(state.xpos);
            //    mgpa.tgc4.setXpos(state.xpos);

            //    mgpa.tgc3.repaint();
            //    mgpa.tgc4.repaint();

            //    mgpa.tgc3.setVisible(true);
            //    mgpa.tgc4.setVisible(true);
            //    mgpa.repaint();

            //    if(state.use_Zchart){
            //        mgpb.plotImpedance(state);
            //    }
            //    else{
            //        mgpb.plotAdmittance(state);
            //    }
            //    mgpb.tgc1.setXpos(state.xpos);
            //    mgpb.tgc2.setXpos(state.xpos);

            //    mgpb.tgc1.repaint();
            //    mgpb.tgc2.repaint();

            //    mgpb.tgc1.setVisible(true);
            //    mgpb.tgc2.setVisible(true);
            //    mgpb.repaint();
	    //}
}
//public void mouseDragged(MouseEvent evt){move_1();}
//public void mouseMoved(MouseEvent evt){;}
//public void mouseClicked(MouseEvent evt){move_1();}
//public void mouseEntered(MouseEvent evt){;}
//public void mouseExited(MouseEvent evt){;}
//public void mousePressed(MouseEvent evt){move_1();}
//public void mouseReleased(MouseEvent evt){move_1();}

public void mouseDragged(MouseEvent evt){move_1();}
public void mouseMoved(MouseEvent evt){;}
public void mouseClicked(MouseEvent evt){;}
public void mouseEntered(MouseEvent evt){;}
public void mouseExited(MouseEvent evt){;}
public void mousePressed(MouseEvent evt){move_1();}
public void mouseReleased(MouseEvent evt){;}

private void move_1(){
    String str1, str2;
    Complex ZLnorm, YLnorm;
    Complex YOpen = new Complex(0.0,0.0);
    
    sp2.sc.draw_ZL=true;
    sp2.sc.draw_Zin=true;
    sp2.sc.draw_Yin=true;
    ZLnorm = new Complex(0.0,0.0);
    
    // zero gamma = xProbe = 280; yProbe = 280
    // open  circuit       = 518         = 280
    // short circuit       = 42          = 280 
    
    state.GammaL = sp2.sc.getGammaProbe();
	
	if(sp2.sc.xProbe == 518 && sp2.sc.yProbe == 280){//open
		state.ZL = new Complex(0.0,-1.0E135);
		state.YL = new Complex(0.0,0.0);
		state.GammaL = new Complex(1.0,0.0);
		state.IsLoadOpen = true;
		state.IsLoadShort = false;
		state.IsLoadImaginary = false;
		state.IsLoadRegular = false;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
	}
	else if(sp2.sc.xProbe == 42 && sp2.sc.yProbe == 280){//short
		state.YL = new Complex(0.0,-1.0E135);
		state.ZL = new Complex(0.0,0.0);
		state.GammaL = new Complex(-1.0,0.0);		
		state.IsLoadOpen = false;
		state.IsLoadShort = true;
		state.IsLoadImaginary = false;
		state.IsLoadRegular = false;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
		state.Gammain = EMF.computeGammaAt(state.GammaL,state.xpos);
	}
	else if(sp2.sc.xProbe == 280 && sp2.sc.yProbe == 42){//imaginary 90 deg
		state.ZL = new Complex(0.0,state.Z0.Real());
		state.YL = new Complex(0.0,-1.0/state.Z0.Real());
		state.GammaL = new Complex(0.0,1.0);
		state.IsLoadOpen = false;
		state.IsLoadShort = false;
		state.IsLoadImaginary = true;
		state.IsLoadRegular = false;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
	}
	else if(sp2.sc.xProbe == 280 && sp2.sc.yProbe == 518){//imaginary -90 deg
		state.ZL = new Complex(0.0,-state.Z0.Real());
		state.YL = new Complex(0.0,1.0/state.Z0.Real());
		state.GammaL = new Complex(0.0,-1.0);
		state.IsLoadOpen = false;
		state.IsLoadShort = false;
		state.IsLoadImaginary = true;
		state.IsLoadRegular = false;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
	}
	else{
	    if(sp2.sc.IsInside){   
		if(loadpanel.c1.getState() || loadpanel.c3.getState()){//Impedance choice is on
		    ZLnorm = new Complex(sp2.sc.getZProbe().Real(),sp2.sc.getZProbe().Imaginary());
		    state.ZL = Complex.Multiply(ZLnorm,state.Z0);
		    state.YL =  Complex.Divide(state.one,state.ZL);
		    state.is_Load_Ztype = true;
		}
		else{ //Admittance choice is on
		    YLnorm = new Complex(sp2.sc.getYProbe().Real(),sp2.sc.getYProbe().Imaginary());
		    state.YL = Complex.Divide(YLnorm,state.Z0);
		    state.ZL =  Complex.Divide(state.one,state.YL);
		    state.is_Load_Ztype = false;
		}
		state.IsLoadOpen = false;
		state.IsLoadShort = false;
		state.IsLoadImaginary = false;
		state.IsLoadRegular = true;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
	    }
	}
		    
	state.ignition();
        auxiliary5.repaint();
            
        //state.ScanVoltageCurrent();
        mgpa.plotVoltageCurrent(state);
        mgpa.tgc3.setXpos(state.xpos);
        mgpa.tgc4.setXpos(state.xpos);

        mgpa.tgc3.repaint();
        mgpa.tgc4.repaint();
        
        mgpa.tgc3.setVisible(true);
	mgpa.tgc4.setVisible(true);
        mgpa.repaint();

	if(state.use_Zchart){
            mgpb.plotImpedance(state);
        }
        else{
            mgpb.plotAdmittance(state);
        }
        mgpb.tgc1.setXpos(state.xpos);
        mgpb.tgc2.setXpos(state.xpos);

        mgpb.tgc1.repaint();
        mgpb.tgc2.repaint();

        mgpb.tgc1.setVisible(true);
        mgpb.tgc2.setVisible(true);
        mgpb.repaint();
    
	    // Use this to force return to IMP or ADM choice after Open or Short are detected
	    //if(state.is_Load_Ztype && !state.IsLoadShort && !state.IWantImpedance){state.is_Load_Ztype = false;}
	    //if(!state.is_Load_Ztype && !state.IsLoadOpen && state.IWantImpedance){state.is_Load_Ztype = true;}
	  if(loadpanel.c1.getState()){//Impedance choice is on
		/*
		if(state.IsLoadOpen){
		    state.is_Load_Ztype = false;
		    loadpanel.c1.setState(false);
		    loadpanel.c2.setState(true);
		    loadpanel.c3.setState(false);
		    loadpanel.setValue(state.YL,0);
		    loadpanel.lab1[0].setText("Y");
		    loadpanel.lab3[0].setText("[ S ]");
		}
		else{*/
		
		    if(state.IsLoadOpen){
			loadpanel.b1.setEnabled(false);
			loadpanel.slider1.setEnabled(false);
			loadpanel.locked_open = true;
			loadpanel.locked_short = false;
			
		    }
		    else{
			loadpanel.b1.setEnabled(true);
			loadpanel.slider1.setEnabled(true);
			loadpanel.locked_open = false;
			loadpanel.locked_short = false;
		    }
		    
		    loadpanel.setValue(state.ZL,0);
		    state.is_Load_Ztype = true;
		    //loadpanel.c1.setState(true);
		    //loadpanel.c2.setState(false);
		    //loadpanel.c3.setState(false);
		    loadpanel.lab1[0].setText("Z");
		    loadpanel.lab3[0].setText("[ \u03a9 ]");
		//}
	  }
	  else if(loadpanel.c2.getState()){//Admittance choice is on
		/*
		if(state.IsLoadShort){
		    state.is_Load_Ztype = true;
		    loadpanel.c1.setState(true);
		    loadpanel.c2.setState(false);
		    loadpanel.c3.setState(false);
		    loadpanel.setValue(state.ZL,0);
		    loadpanel.lab1[0].setText("Z");
		    loadpanel.lab3[0].setText("[ \u03a9 ]");
		}
		else*/
		{
		    if(state.IsLoadShort){
			loadpanel.b1.setEnabled(false);
			loadpanel.slider1.setEnabled(false);
			loadpanel.locked_open = false;
			loadpanel.locked_short = true;
		    }
		    else{
			loadpanel.b1.setEnabled(true);
			loadpanel.slider1.setEnabled(true);
			loadpanel.locked_open = false;
			loadpanel.locked_short = false;
		    }
		    
		    loadpanel.setValue(state.YL,0);
		    state.is_Load_Ztype = false;
		    //loadpanel.c1.setState(false);
		    //loadpanel.c2.setState(true);
		    //loadpanel.c3.setState(false);
		    loadpanel.lab1[0].setText("Y");
		    loadpanel.lab3[0].setText("[ S ]");
		}
	    }
	    else if(loadpanel.c3.getState()){//Reflection Coefficient
		    loadpanel.setValue(state.GammaL,1);
		    state.is_Load_Ztype = true;
	    }
	    
	    
	    if(loadpanel.c1.getState() || loadpanel.c3.getState()){
		Complex znorm = Complex.Divide(state.ZL,state.Z0);
		if(state.IsLoadRegular || state.IsLoadImaginary){
		    //Real part slider
		    if(znorm.Real() >= 0.0 && znorm.Real() <= 1.0){
			loadpanel.slider1.setValue((int)(znorm.Real()*500));
		    }
		    else if(znorm.Real() > 1.0 && znorm.Real() <= 20.0){
			loadpanel.slider1.setValue(500+(int)((znorm.Real()-1)*500));
		    }
		    else if(znorm.Real() > 20.0 && znorm.Real() <= 500.0){
			loadpanel.slider1.setValue((int)(znorm.Real()-20)+10000);
		    }
		
		    //Imaginary part slider
		    if(znorm.Imaginary() > 0.0 && znorm.Imaginary() <= 20.0){
			loadpanel.slider2.setValue((int)(znorm.Imaginary()*250)+5481);
		    }
		    else if(znorm.Imaginary() > 20.0 && znorm.Imaginary() <= 500.0){
			loadpanel.slider2.setValue((int)(znorm.Imaginary()-20)+10481);
		    }
		    else if(znorm.Imaginary() < 0.0 && znorm.Imaginary() >= -20.0){
			loadpanel.slider2.setValue(5481+(int)(znorm.Imaginary()*250));
		    }
		    else if(znorm.Imaginary() < -20.0 && znorm.Imaginary() >= - 500.0){
			loadpanel.slider2.setValue(481+(int)(znorm.Imaginary()+20));
		    }
		    else if(znorm.Imaginary() == 0.0){
			loadpanel.slider2.setValue(5481);
		    }
		    else if(znorm.Imaginary() == 500.0){
			loadpanel.slider2.setValue(10963);
		    }
		    else if(znorm.Imaginary() == -500.0){
			loadpanel.slider2.setValue(0);
		    }
		    
		    
		    if(state.IsLoadRegular){
			loadpanel.slider3.setValue((int)(Complex.Magnitude(state.GammaL)*1000.0));
			loadpanel.slider4.setValue((int)(MaestroA.rounder(Complex.Arg2(state.GammaL,1),6)*10)+1800);
		    }
		    else if(state.IsLoadImaginary){
			loadpanel.slider3.setValue(loadpanel.SCROLLMAXG1-1);
			loadpanel.slider4.setValue((int)(MaestroA.rounder(Complex.Arg2(state.GammaL,1),6)*10)+1800);
		    }
		
		}
		else if(state.IsLoadShort){
		    loadpanel.slider1.setValue(0);
		    loadpanel.slider2.setValue(5481);
		    loadpanel.slider3.setValue(loadpanel.SCROLLMAXG1-1);
		    loadpanel.slider4.setValue(loadpanel.SCROLLMAXG2-1);
		}
		else if(state.IsLoadOpen){
		    loadpanel.slider1.setValue(0);
		    loadpanel.slider2.setValue(0);
		    loadpanel.slider3.setValue(loadpanel.SCROLLMAXG1-1);
		    loadpanel.slider4.setValue((loadpanel.SCROLLMAXG2-1)/2);
		}
	    
	    }
	    else if(loadpanel.c2.getState()){
		Complex znorm = Complex.Multiply(state.YL,state.Z0);
		if(state.IsLoadRegular || state.IsLoadImaginary){
		    //Real part slider
		    if(znorm.Real() >= 0.0 && znorm.Real() <= 1.0){
			loadpanel.slider1.setValue((int)(znorm.Real()*500));
		    }
		    else if(znorm.Real() > 1.0 && znorm.Real() <= 20.0){
			loadpanel.slider1.setValue(500+(int)((znorm.Real()-1)*500));
		    }
		    else if(znorm.Real() > 20.0 && znorm.Real() <= 500.0){
			loadpanel.slider1.setValue((int)(znorm.Real()-20)+10000);
		    }
		
		    //Imaginary part slider
		    if(znorm.Imaginary() > 0.0 && znorm.Imaginary() <= 20.0){
			loadpanel.slider2.setValue((int)(znorm.Imaginary()*250)+5481);
		    }
		    else if(znorm.Imaginary() > 20.0 && znorm.Imaginary() <= 500.0){
			loadpanel.slider2.setValue((int)(znorm.Imaginary()-20)+10481);
		    }
		    else if(znorm.Imaginary() < 0.0 && znorm.Imaginary() >= -20.0){
			loadpanel.slider2.setValue(5481+(int)(znorm.Imaginary()*250));
		    }
		    else if(znorm.Imaginary() < -20.0 && znorm.Imaginary() >= - 500.0){
			loadpanel.slider2.setValue(481+(int)(znorm.Imaginary()+20));
		    }
		    else if(znorm.Imaginary() == 0.0){
			loadpanel.slider2.setValue(5481);
		    }
		    else if(znorm.Imaginary() == 500.0){
			loadpanel.slider2.setValue(10963);
		    }
		    else if(znorm.Imaginary() == -500.0){
			loadpanel.slider2.setValue(0);
		    }
		
		}
		else if(state.IsLoadOpen){
		    loadpanel.slider1.setValue(0);
		    loadpanel.slider2.setValue(5481);
		}
		else if(state.IsLoadShort){
		    loadpanel.slider1.setValue(0);
		    loadpanel.slider2.setValue(0);
		}
		state.is_Load_Ztype = false;
	    }
	    loadpanel.repaint();
	    
	state.ignition();
        auxiliary5.repaint();
            
        //state.ScanVoltageCurrent();
        mgpa.plotVoltageCurrent(state);
        mgpa.tgc3.setXpos(state.xpos);
        mgpa.tgc4.setXpos(state.xpos);

        mgpa.tgc3.repaint();
        mgpa.tgc4.repaint();
        
        mgpa.tgc3.setVisible(true);
	mgpa.tgc4.setVisible(true);
        mgpa.repaint();

	if(state.use_Zchart){
            mgpb.plotImpedance(state);
        }
        else{
            mgpb.plotAdmittance(state);
        }
        mgpb.tgc1.setXpos(state.xpos);
        mgpb.tgc2.setXpos(state.xpos);

        mgpb.tgc1.repaint();
        mgpb.tgc2.repaint();

        mgpb.tgc1.setVisible(true);
        mgpb.tgc2.setVisible(true);
        mgpb.repaint();

	//sp2.setGammaFlag(state.IsLargerThanOne);
	    //sp2.sc.draw_swr_circle=true;
	    //sp2.sc.draw_swr_line=true;
	    sp2.setxpos(state.xpos);
	    
	    if(loadpanel.c1.getState()){//Impedance choice is on
		sp2.setZin(state.Zin,state.Z0);
		sp2.setZL(state.ZL,state.Z0);
		sp2.sm.setGL(state.GammaL);
		sp2.setYin(state.Yin,state.Z0);
	    }
	    else if(loadpanel.c2.getState()){//Admittance choice is on
		sp2.setYin(state.Yin,state.Z0);
		sp2.setYL(state.YL,state.Z0);
		sp2.setZin(state.Zin,state.Z0);
		sp2.sm.setGL(state.GammaL);
	    }
	    else if(loadpanel.c3.getState()){//Reflection Coefficient choice is on
		sp2.setZin(state.Zin,state.Z0);
		sp2.setZL(state.ZL,state.Z0);
		sp2.setYin(state.Yin,state.Z0);
		sp2.sm.setGL(state.GammaL);
	    }
	    
	    auxiliary.repaint();
	    auxiliary2.repaint();
	    auxiliary3.repaint();
}


private void doupdate(){

    if(loadpanel.c1.getState()){//Impedance choice is on
		
		double temp1, temp2;
		Complex tempc;
		
		tempc = new Complex(Double.valueOf(loadpanel.text1[0].getText()).doubleValue(),
		                     Double.valueOf(loadpanel.text2[0].getText()).doubleValue());
		temp1 = Complex.Real(tempc);
		temp2 = Complex.Imaginary(tempc);
		
		//avoid input of negative value				
		if(temp1 < 0.0 && temp2 != 0.0 && temp2 > -1.0E135){ //Negative R - transform Z to imaginary impedance
		    state.ZL = new Complex(0.0,temp2);
		    state.YL = new Complex(0.0,-1.0/temp2);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else if(temp1 <= 0.0 && temp2 == 0.0){// Negative R and zero X, transform to short  SHORT
		    state.ZL = new Complex(0.0,0.0);
		    state.YL = new Complex(0.0,-1.0E135);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = true; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 0.0 && temp2 <= -1.0E135){// Open
		    state.ZL = new Complex(0.0,-1.0E135);
		    state.YL = new Complex(0.0,0.0);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 0.0 && temp2!=0.0 && temp2 > -1.0E135){// Purely Imaginary Impedance
		    state.ZL = new Complex(0.0,temp2);
		    state.YL = new Complex(0.0,-1.0/temp2);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else{// Regular Impedance
		    state.ZL = new Complex(temp1,temp2);
		    state.YL = EMF.Inv(state.ZL);
		    loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
		}
		state.is_Load_Ztype = true;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
		
		Complex znorm = Complex.Divide(state.ZL,state.Z0);
		
		if(state.IsLoadRegular || state.IsLoadImaginary){
		    //Real part slider
		    if(znorm.Real() > 0.0 && znorm.Real() <= 1.0){
			loadpanel.slider1.setValue((int)(znorm.Real()*500));
		    }
		    else if(znorm.Real() > 1.0 && znorm.Real() <= 20.0){
			loadpanel.slider1.setValue(500+(int)((znorm.Real()-1)*500));
		    }
		    else if(znorm.Real() > 20.0 && znorm.Real() <= 500.0){
			loadpanel.slider1.setValue((int)(znorm.Real()-20)+10000);
		    }
		    else if(znorm.Real() == 0.0){
		    if(znorm.Imaginary() > -1.0E135){
			loadpanel.slider1.setValue(0);
		    }
		    else{
			loadpanel.slider1.setValue(0);
			loadpanel.slider2.setValue(0);
		    }
		}
		
		    //Imaginary part slider
		    if(znorm.Imaginary() > 0.0 && znorm.Imaginary() <= 20.0){
			loadpanel.slider2.setValue((int)(znorm.Imaginary()*250)+5481);
		    }
		    else if(znorm.Imaginary() > 20.0 && znorm.Imaginary() <= 500.0){
			loadpanel.slider2.setValue((int)(znorm.Imaginary()-20)+10481);
		    }
		    else if(znorm.Imaginary() < 0.0 && znorm.Imaginary() >= -20.0){
			loadpanel.slider2.setValue(5481+(int)(znorm.Imaginary()*250));
		    }
		    else if(znorm.Imaginary() < -20.0 && znorm.Imaginary() >= - 500.0){
			loadpanel.slider2.setValue(481+(int)(znorm.Imaginary()+20));
		    }
		    else if(znorm.Imaginary() == 0.0){
			loadpanel.slider2.setValue(5481);
		    }
		    else if(znorm.Imaginary() == 500.0){
			loadpanel.slider2.setValue(10963);
		    }
		    else if(znorm.Imaginary() == -500.0){
			loadpanel.slider2.setValue(0);
		    }
		}
		else if(state.IsLoadShort){
		    loadpanel.slider1.setValue(0);
		    loadpanel.slider2.setValue(5481);
		}
		else if(state.IsLoadOpen){
		    loadpanel.slider1.setValue(0);
		    loadpanel.slider2.setValue(0);
		}
    
	    }
	    else if(loadpanel.c2.getState()){ //Admittance choice is on
		
		double temp1, temp2;
		Complex tempc;
		
		tempc = new Complex(Double.valueOf(loadpanel.text1[0].getText()).doubleValue(),
		                     Double.valueOf(loadpanel.text2[0].getText()).doubleValue());
		temp1 = Complex.Real(tempc);
		temp2 = Complex.Imaginary(tempc);
		
	    //avoid input of negative value				
		if(temp1 < 0.0 && temp2 != 0.0 && temp2 > -1.0E135){ //Negative R - transform Z to imaginary impedance
		    state.YL = new Complex(0.0,temp2);
		    state.ZL = new Complex(0.0,-1.0/temp2);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else if(temp1 <= 0.0 && temp2 == 0.0){// Negative R and zero X, transform to open   OPEN
		    state.YL = new Complex(0.0,0.0);
		    state.ZL = new Complex(0.0,-1.0E135);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 0.0 && temp2 <= -1.0E135){// Short
		    state.YL = new Complex(0.0,-1.0E135);
		    state.ZL = new Complex(0.0,0.0);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = true; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 0.0 && temp2 !=0.0 && temp2 > -1.0E135){// Purely Imaginary Impedance
		    state.YL = new Complex(0.0,temp2);
		    state.ZL = new Complex(0.0,-1.0/temp2);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else{// Regular Admittance
		    state.YL = new Complex(temp1,temp2);
		    state.ZL = EMF.Inv(state.YL);
		    loadpanel.setValue(state.YL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
		}
		state.is_Load_Ztype = false;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
	    
		Complex znorm = Complex.Multiply(state.YL,state.Z0);
		if(state.IsLoadRegular || state.IsLoadImaginary){
		    //Real part slider
		    if(znorm.Real() > 0.0 && znorm.Real() <= 1.0){
			loadpanel.slider1.setValue((int)(znorm.Real()*500));
		    }
		    else if(znorm.Real() > 1.0 && znorm.Real() <= 20.0){
			loadpanel.slider1.setValue(500+(int)((znorm.Real()-1)*500));
		    }
		    else if(znorm.Real() > 20.0 && znorm.Real() <= 500.0){
			loadpanel.slider1.setValue((int)(znorm.Real()-20)+10000);
		    }
		    else if(znorm.Real() == 0.0){
			if(znorm.Imaginary() > -1.0E135){
			    loadpanel.slider1.setValue(0);
			}
			else{
			    loadpanel.slider1.setValue(0);
			    loadpanel.slider2.setValue(0);
			}
		    }
		
		    //Imaginary part slider
		    if(znorm.Imaginary() > 0.0 && znorm.Imaginary() <= 20.0){
			loadpanel.slider2.setValue((int)(znorm.Imaginary()*250)+5481);
		    }
		    else if(znorm.Imaginary() > 20.0 && znorm.Imaginary() <= 500.0){
			loadpanel.slider2.setValue((int)(znorm.Imaginary()-20)+10481);
		    }
		    else if(znorm.Imaginary() < 0.0 && znorm.Imaginary() >= -20.0){
			loadpanel.slider2.setValue(5481+(int)(znorm.Imaginary()*250));
		    }
		    else if(znorm.Imaginary() < -20.0 && znorm.Imaginary() >= - 500.0){
			loadpanel.slider2.setValue(481+(int)(znorm.Imaginary()+20));
		    }
		    else if(znorm.Imaginary() == 0.0){
			loadpanel.slider2.setValue(5481);
		    }
		    else if(znorm.Imaginary() == 500.0){
			loadpanel.slider2.setValue(10963);
		    }
		    else if(znorm.Imaginary() == -500.0){
			loadpanel.slider2.setValue(0);
		    }
		
		}
		else if(state.IsLoadOpen){
		    loadpanel.slider1.setValue(0);
		    loadpanel.slider2.setValue(5481);
		}
		else if(state.IsLoadShort){
		    loadpanel.slider1.setValue(0);
		    loadpanel.slider2.setValue(0);
		}
	    
	    }
	    
	    else if(loadpanel.c3.getState()){//Impedance choice is on
		double temp1, temp2;
		temp1 = Double.valueOf(loadpanel.text1[1].getText()).doubleValue();
		temp2 = Double.valueOf(loadpanel.text2[1].getText()).doubleValue();
		
		//avoid input of negative value				
		if(temp1 < 0.0 ){ //Negative R - transform Z to imaginary impedance
		    state.GammaL = new Complex(0.0,0.0);
		    state.ZL = new Complex(state.Z0.Real(),0.0);
		    state.YL = new Complex(1.0/state.Z0.Real(),0.0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
		}
		else if(temp1 == 1.0 && temp2 == 0.0){//  open
		    state.GammaL = new Complex(1.0,0.0);
		    state.YL = new Complex(0.0,0.0);
		    state.ZL = new Complex(0.0,-1.0E135);
		    //loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = true; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 1.0 && (temp2 > 179.9999 || temp2 < -179.9999)){// short
		    state.GammaL = new Complex(-1.0,0.0);
		    state.YL = new Complex(0.0,-1.0E135);
		    state.ZL = new Complex(0.0,0.0);
		    //loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = true; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = false;
		}
		else if(temp1 == 1.0 && (temp2!=0.0 || (temp2 > -179.9999 && temp2 < 179.9999))){// Purely Imaginary Impedance
		    state.GammaL = new Complex(Math.cos(temp2*Math.PI/180.0), Math.sin(temp2*Math.PI/180.0));
		    state.ZL = new Complex(0.0, EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true).Imaginary());
		    state.YL = new Complex(0.0, EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true).Imaginary());
		    //loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = true; state.IsLoadRegular = false;
		}
		else{// Regular Impedance
		    state.GammaL = state.GammaL = new Complex(temp1*Math.cos(temp2*Math.PI/180.0), temp1*Math.sin(temp2*Math.PI/180.0));
		    state.ZL = EMF.computeZinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
		    state.YL = EMF.computeYinAt(state.GammaL,Complex.Real(state.Z0),0.0,true);
		    //loadpanel.setValue(state.ZL,0);
		    state.IsLoadShort = false; state.IsLoadOpen = false; state.IsLoadImaginary = false; state.IsLoadRegular = true;
		}
		
		state.is_Load_Ztype = true;
		sp2.setLoadType(state.IsLoadOpen, state.IsLoadShort, state.IsLoadImaginary, state.IsLoadRegular);
		
		if(state.IsLoadRegular || state.IsLoadImaginary){
		    if(state.IsLoadImaginary){
			loadpanel.slider3.setValue(loadpanel.SCROLLMAXG1-1);
		    }
		    else{
			loadpanel.slider3.setValue((int)(Complex.Magnitude(state.GammaL)*1000.0));
		    }
		    loadpanel.slider4.setValue((int)(MaestroA.rounder(Complex.Arg2(state.GammaL,1),6)*10)+1800);
		}
		else if(state.IsLoadShort){
		    loadpanel.slider3.setValue(loadpanel.SCROLLMAXG1-1);
		    loadpanel.slider4.setValue(loadpanel.SCROLLMAXG2-1);
		}
		else if(state.IsLoadOpen){
		    loadpanel.slider3.setValue(loadpanel.SCROLLMAXG1-1);
		    loadpanel.slider4.setValue((loadpanel.SCROLLMAXG2-1)/2);
		}
	    }
	    state.ignition();
	    loadpanel.repaint();
	    
	    sp2.setGammaFlag(state.IsLargerThanOne);
	    
	    sp2.setxpos(state.xpos);
	    if(loadpanel.c1.getState()){//Impedance choice is on
		sp2.setZin(state.Zin,state.Z0);
		sp2.setZL(state.ZL,state.Z0);
		sp2.sm.setGL(state.GammaL);
		sp2.setYin(state.Yin,state.Z0);
		state.is_Load_Ztype = true;
	    }
	    else if(loadpanel.c2.getState()){//Admittance choice is on
		sp2.setYin(state.Yin,state.Z0);
		sp2.setYL(state.YL,state.Z0);
		state.is_Load_Ztype = false;
	    }
	    else if(loadpanel.c3.getState()){//Reflection Coefficient choice is on
		sp2.setZin(state.Zin,state.Z0);
		sp2.setZL(state.ZL,state.Z0);
		sp2.sm.setGL(state.GammaL);
		sp2.setYin(state.Yin,state.Z0);
		state.is_Load_Ztype = true;
	    }
	   
	    auxiliary.repaint();
	    auxiliary2.repaint();
	    auxiliary3.repaint();
}

private void send_shock_wave(){
	state.ignition();
		
	sp2.setxpos(state.xpos);
	sp2.setZin(state.Zin,state.Z0);
	
	state.ignition();
}    
     

}
