//SmithPanel.java
/* A Java class for
 * 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.geom.*;
import java.awt.event.*;

public class SmithPanel extends Panel implements MouseListener, MouseMotionListener, ItemListener{
	public SmithCanvas sc;
	public SmithMan sm; 
	private SmithProbe sp;
	public SmithControls scon;
	private static final Color bgcolor = Color.white;
	
	//---------------------------------NEW
        public Button small;
        public Button colore;
        public boolean colore_dark;
        
        protected static final Font buttonfont = TheFonts.sanSerif11;
        
        //--------------------------------------
        StateVars state;
        
public SmithPanel(StateVars state){
	setLayout(null);
        this.state = state;
	
	//small = new Checkbox("Enlarge",null,false);
	//add(small);
	//small.setBackground(Color.white);
	//small.setBounds(5,2,68,20);
        
	//-------------------------------------NEW
        small = new Button("Instructions");
        add(small);
	small.setBackground(Color.white);
	small.setBounds(state.s8,state.s9,state.s85,state.s20);
        small.setFont(new Font("SanSerif",Font.PLAIN,state.font10));
        
        colore = new Button("Color");
	add(colore);
	colore.setBackground(Color.white);
	colore.setBounds(state.s345,state.s9,state.s50,state.s20);
        colore.setFont(new Font("SanSerif",Font.PLAIN,state.font10));
        
        colore_dark = false;
        //----------------------------------------
	setBackground(bgcolor);
	
	sc = new SmithCanvas(state);
	add(sc);
	sc.setBounds(state.s4,state.s5,state.s393,state.s393);
	
	//sm = new SmithMan(340,65);
	sm = new SmithMan(state.s335,state.s95,state);
	add(sm);
	sm.setBounds(state.s4+1,state.s404+1,state.s393-2,state.s95-2);

	sp = new SmithProbe(337,34,state);
	add(sp);
	sp.setBounds(state.s4+1,state.s505+1,state.s393-2,state.s38-2);

	scon = new SmithControls();
	add(scon);
	scon.setBounds(state.s4+1,state.s549+1,state.s393-2,state.s21-2);
        
	Panel pgraph1 = new Panel();
	    pgraph1.setBackground(Color.black);
	    add(pgraph1);
	    pgraph1.setBounds(state.s4,state.s404,state.s393,state.s95);
	    
	Panel pgraph2 = new Panel();
	    pgraph2.setBackground(Color.black);
	    add(pgraph2);
	    pgraph2.setBounds(state.s4,state.s505,state.s393,state.s38);
	      
	Panel pgraph4 = new Panel();
	    pgraph4.setBackground(Color.black);
	    add(pgraph4);
	    pgraph4.setBounds(state.s4,state.s549,state.s393,state.s21);
	
        //---------------------------------
	sc.addMouseListener(this);
	sc.addMouseMotionListener(this);
	scon.imp.addItemListener(this);
	scon.adm.addItemListener(this);
	scon.swr.addItemListener(this);
	scon.swrline.addItemListener(this);
        //----------------------------------
}

public void paint(Graphics g){
	//g.setColor(bgcolor.darker());
	//g.fillRect(0,getSize().height-2,getSize().width,2);
	//g.fillRect(getSize().width-2,0,2,getSize().height);
	//g.setColor(bgcolor.brighter());
	//g.fillRect(0,0,2,getSize().height-1);
	//g.fillRect(0,0,getSize().width-2,2);
}

public  synchronized void setZin(Complex Zin){
	sc.setZin(Zin);
	sm.setZin(Zin);
}

public synchronized void setYin(Complex Yin){
	sc.setYin(Yin);
	sm.setYin(Yin);
}

public synchronized void setZin(Complex Zin, double Z0){
	setZin(Complex.Divide(Zin,Z0));
}

public synchronized void setYin(Complex Yin, double Z0){
	setYin(Complex.Multiply(Yin,Z0));
}

public synchronized void setZin(Complex Zin, Complex Z0){
	setZin(Complex.Divide(Zin,Z0));
}

public synchronized void setYin(Complex Yin, Complex Z0){
	setYin(Complex.Multiply(Yin,Z0));
}

public synchronized void setZL(Complex ZL){
	sc.setZL(ZL);
	sm.setZL(ZL);
}

public synchronized void setZL(Complex ZL, double Z0){
	setZL(Complex.Divide(ZL,Z0));
}

public synchronized void setZL(Complex ZL, Complex Z0){
	setZL(Complex.Divide(ZL,Z0));
}

public synchronized void setYL(Complex YL){
	sc.setYL(YL);
	sm.setYL(YL);
}

public synchronized void setYL(Complex YL, double Z0){
	setYL(Complex.Multiply(YL,Z0));
}

public synchronized void setYL(Complex YL, Complex Z0){
	setYL(Complex.Multiply(YL,Z0));
}

public synchronized void set_swr_circle(boolean arg){
	sc.set_swr_circle(arg);
}

public synchronized void set_swr_line(boolean arg){
	sc.set_swr_line(arg);
}

public synchronized void set_impedance(boolean arg){
	sc.set_impedance(arg);
}

public synchronized void set_admittance(boolean arg){
	sc.set_admittance(arg);
}

public synchronized void setZL(boolean arg){
	sc.setZL(arg);
}

public synchronized void setYL(boolean arg){
	sc.setYL(arg);
}

public synchronized void setZin(boolean arg){
	sc.setZin(arg);	
}

public synchronized void setYin(boolean arg){
	sc.setYin(arg);
}

public synchronized void setxpos(double xpos){
	sc.setxpos(xpos);
	sp.setxpos(xpos);
	sp.setmode(1);
}

public void mouseDragged(MouseEvent evt){;}//cricket_1();}
public void mouseMoved(MouseEvent evt){;}
public void mouseClicked(MouseEvent evt){;}//cricket_1();}
public void mouseEntered(MouseEvent evt){;}
public void mouseExited(MouseEvent evt){;}
public void mousePressed(MouseEvent evt){;}//cricket_1();}
public void mouseReleased(MouseEvent evt){;}//cricket_1();}
public void itemStateChanged(ItemEvent evt){cricket_2(evt);}

private void cricket_1(){
    String str1, str2;
    str1="z = "+sc.getZProbe().toString(Complex.CARTESIAN,5)+
	 "       y = "+Complex.Divide(1.0,sc.getZProbe()).toString(Complex.CARTESIAN,5);
    str2="\u0393 = "+sc.getGammaProbe().toString(Complex.POLAR_DEGREE,5);
    sp.setmode(2);
    sp.set(str1,str2);
}

private void cricket_2(ItemEvent evt){
    if(evt.getSource()==scon.imp){
			sc.set_impedance(scon.imp.getState());
                        sc.setMode(1);
			sm.setMode(1);
			sm.setZL(sc.getZL());
			sm.setZin(sc.getZin());
    }
    else if(evt.getSource()==scon.adm){
			sc.set_admittance(scon.adm.getState());
                        sc.setMode(2);
			sm.setMode(2);
			sm.setZL(sc.getZL());
			sm.setYin(sc.getYin());
    }
    else if(evt.getSource()==scon.swr){
			sc.set_swr_circle(scon.swr.getState());
    }
    else if(evt.getSource()==scon.swrline){
			sc.set_swr_line(scon.swrline.getState());
    }
    sc.repaint();
    sm.repaint();
}

}//End of SmithPanel


//----------------------------------------------------------------------------
 
class SmithCanvas extends Canvas implements MouseListener, MouseMotionListener{
//private final Color bgcolor = new Color(216,216,191);
//------------------------------NEW
private final Color bgcolor = new Color(236,236,236);
public Color outer;
private boolean colore_dark;
//----------------------------------

private int xCenter, yCenter;
//------------------------------------------NEW
private int Radius1, Diam1, Radius2, Diam2, Radius3;
//-------------------------------------------------
private int width, height;
private boolean draw_impedance_chart;
private boolean draw_admittance_chart;
private boolean draw_swr_circle;
private boolean draw_swr_line;
private boolean draw_arcs;
private boolean draw_vline;
private boolean draw_ZL, draw_YL, draw_Zin, draw_Yin;
private Complex Zin, Yin, ZL, YL;
private Complex GammaZin, GammaYin, GammaZL, GammaYL;
private double xpos, Z0;
private Complex GammaProbe, ZProbe; 
private int xProbe, yProbe;
private static final Cursor cursorA = new Cursor(Cursor.DEFAULT_CURSOR);
private static final Cursor cursorB = new Cursor(Cursor.CROSSHAIR_CURSOR);
public int mode;
private Image im;
private Graphics buf;
public boolean OverRide = false;
StateVars state;

public SmithCanvas(StateVars state){
	super();
        this.state = state;
        
	setBackground(Color.white); 
	draw_impedance_chart=true;
	draw_admittance_chart=false;
	draw_swr_circle=true;
	draw_swr_line=true;
	draw_ZL=true;
	draw_YL=true;
	draw_Zin=true;
	draw_Yin=true;
	draw_arcs=true;
	draw_vline=true;
        
        mode = 1;
        
	xpos = 0.0;
	Z0 = 1.0;
	ZL = new Complex(2.0,1.6);
	YL = EMF.Inv(ZL);
	GammaZL = EMF.computeGamma(ZL);
	GammaYL = EMF.computeGamma(YL);
	Zin = new Complex(2.0,1.6);
	GammaZin = EMF.computeGamma(Zin);
	Yin = EMF.Inv(Zin);
	GammaYin = EMF.computeGamma(Yin);
	ZProbe = new Complex();
	GammaProbe = new Complex();
	xProbe=0;
	yProbe=0;
	
        //---------------------------- NEW
        colore_dark = false;
        //------------------------------------
        
	//Listeners
	this.addMouseListener(this);
	this.addMouseMotionListener(this);
}

public void paint(Graphics g){
	    if(im == null){
		im = createImage(getSize().width,getSize().height);
		buf = im.getGraphics();
		drawSmith(buf);
	    }
	    else{
		drawSmith(buf);
	    }
	    g.drawImage(im,0,0,null);
}

//Addition to reduce flicker new routine
public void update(Graphics g){		// added to avoid clearing
	paint(g);
}


public void drawSmith(Graphics g){
    
        //------------------------------------------------NEW
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        //--------------------------------------------------------------------------------------

	if(OverRide) {
	    drawPoints(g);
	    if(draw_swr_line) draw_swr_line(g);
	    if(draw_arcs) draw_arcs(g);
	    return;
	}
	int i;
	int Ncurves = 16;
	double[] rl={0.2,0.4,0.6,0.8,1.0,1.2,1.4,1.6,1.8,2.0,3.0,4.0,5.0,10.0,20.0,50.0};
	double[] xl={0.2,0.4,0.6,0.8,1.0,1.2,1.4,1.6,1.8,2.0,3.0,4.0,5.0,10.0,20.0,50.0};
	
	width=getSize().width;
	height=getSize().height;

	xCenter = width/2;
	yCenter = height/2;

	Radius1 = (width/2)*85/100;
	Radius2 = (width/2)*95/100;
        //--------------------------------NEW
        Radius3 = Radius1;
        //-----------------------------------
	Diam1 = 2*Radius1;
	Diam2 = 2*Radius2;
	
        //------------------------------------------REMOVE
	//Draw the black rectangle
	//g.setColor(Color.black);
	//g.fillRect(0,0,width,height);
	
	
        //Draw the background circle
	//------------------------------------NEW
        if(colore_dark){
            g.setColor(bgcolor.darker());
        }
        else{
            g.setColor(bgcolor);
        }
        //---------------------------------------
        
	fillCircle(g,xCenter,yCenter,Radius1);

	//Draw a horizontal line
        g.setColor(Color.lightGray);
	g.drawLine(xCenter-Radius1,yCenter,xCenter+Radius1,yCenter);
	
	//Draw a vertical line
	if(draw_vline){g.drawLine(xCenter,yCenter-Radius1,xCenter,yCenter+Radius1);}
	
        //-----------------------NEW
        Color plotchart = Color.lightGray;
        
        
	if(draw_impedance_chart){
	//Draw resistance and reactance circles
		for(i=0;i<Ncurves;i++){
			draw_resistance(rl[i],g,plotchart);
			draw_reactance(xl[i],g,plotchart);
		}
	}
	if(draw_admittance_chart){
	//Draw conductance and susceptance circles
		for(i=0;i<Ncurves;i++){
			draw_conductance(rl[i],g,plotchart);
			draw_susceptance(xl[i],g,plotchart);
		}
	}
        
        //--------------------------------------END NEW
        
        //-------------------------------------------------------------------NEW
        //draw background with circular hole for chart
        
        if(colore_dark){
            outer = new Color(236,236,236);
        }
        else{
            outer = Color.lightGray;
        }

        
        drawStencil(g, width, height, Radius1, xCenter, yCenter, outer);
	
        //draw black frame
        g.setColor(Color.black);
	g.drawRect(0,0,width-1,height-1);
	//----------------------------------------------------------------------

	//Draw tick-marks in the peripheries
	draw_tick_marks(g);

        //--------------------------------------NEW
        //Draw external and internal circles
	g.setColor(Color.black);
	drawCircle(g,xCenter,yCenter,Radius1);
	drawCircle(g,xCenter,yCenter,Radius2);



	//Draw rl=1 circle 
        //--------------------------------NEW
        Color r_one_color;
        
            if(colore_dark){
                r_one_color = Color.white;
            }
            else{
                r_one_color = Color.gray;
            }

	if(draw_impedance_chart){
		draw_resistance(1.0,g,r_one_color);
	}
	else{
		draw_conductance(1.0,g,r_one_color);
	}
        //------------------------------------------
        
        
	//Draw a small blue circle at the center
	g.setColor(Color.blue);
        //---------------------------------------------NEW
	drawCircle(g,xCenter,yCenter,(int)(Radius1*0.04));
	//------------------------------------------------

	//------------------------------------------REMOVE
        //Draw external and internal white circle
	//g.setColor(bgcolor);
	//drawCircle(g,xCenter,yCenter,Radius1);
	//drawCircle(g,xCenter,yCenter,Radius2);
        //------------------------------------------------
        
        //draw_arcs()
	if(draw_arcs){draw_arcs(g);}
        
	//draw swr_circle
	if(draw_swr_circle){draw_swr_circle(g);}

	//draw swr_line
	if(draw_swr_line){draw_swr_line(g);}

	//drawPoints();
	drawPoints(g);

}

public void cleanAll() {
	//this.getGraphics().clearRect(0,0,getSize().width,getSize().height);
	im = null;
}

private void drawStencil(Graphics g, int width, int height, int radius, int xcenter, int ycenter, Color stencilcolor){
	int rule; float alpha;
        rule = AlphaComposite.SRC_OVER;
        alpha = 1.0f;
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(stencilcolor);      
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
        Area a1, a2, a3;
        
	Rectangle2D newrect = new Rectangle2D.Double(0.0,0.0,(double)width,(double)height);
        a1 = new Area(newrect);
        Ellipse2D newellipse = new Ellipse2D.Double((double)(xcenter-radius),(double)(ycenter-radius),(double)(radius*2),(double)(radius*2));
        a2 = new Area(newellipse);
        a3 = new Area();
        a3.add(a1);
        a3.subtract(a2);
        g2d.fill(a3);
        
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
}

private void drawCircle(Graphics g, int xCenter, int yCenter, int Radius){
	g.drawOval(xCenter-Radius,yCenter-Radius,2*Radius,2*Radius);
}

private void fillCircle(Graphics g, int xCenter, int yCenter, int Radius){
	g.fillOval(xCenter-Radius,yCenter-Radius,2*Radius,2*Radius);
}

private void draw_resistance(double rl, Graphics g, Color color){
	double xcen, ycen, raio;
	//Microwave Engineering, David M. Pozar, Equation 3.56a, Page 86.
	xcen = (rl)/(1.0+rl);
	ycen = 0.0;
	raio = 1.0/(1.0+rl);

	g.setColor(color);
	drawCircle(g,xCenter+(int)(xcen*Radius1),xCenter+(int)(ycen*Radius1),(int)(raio*Radius1));
}

private void draw_conductance(double rl, Graphics g, Color color){
	double xcen, ycen, raio;
	//Microwave Engineering, David M. Pozar, Equation 3.56a, Page 86.
	xcen = -(rl)/(1.0+rl);
	ycen = 0.0;
	raio = 1.0/(1.0+rl);
	
	g.setColor(color);
	drawCircle(g,xCenter+(int)(xcen*Radius1),xCenter+(int)(ycen*Radius1),(int)(raio*Radius1));
}

private void draw_reactance(double xl, Graphics g, Color color){
	double xcen, ycen, raio;
	//Microwave Engineering, David M. Pozar, Equation 3.56b, Page 86.
	xcen = 1.0;
	ycen = 1.0/xl;
	raio = 1.0/xl;

	g.setColor(color);
	drawCircle(g,xCenter+(int)(xcen*Radius1),xCenter+(int)(ycen*Radius1),(int)(raio*Radius1));

	ycen = -1.0/xl;
	drawCircle(g,xCenter+(int)(xcen*Radius1),xCenter+(int)(ycen*Radius1),(int)(raio*Radius1));
}

private void draw_susceptance(double xl, Graphics g, Color color){
	double xcen, ycen, raio;
	//Microwave Engineering, David M. Pozar, Equation 3.56b, Page 86.
	xcen = -1.0;
	ycen = 1.0/xl;
	raio = 1.0/xl;

	g.setColor(color);
	drawCircle(g,xCenter+(int)(xcen*Radius1),xCenter+(int)(ycen*Radius1),(int)(raio*Radius1));

	ycen = -1.0/xl;
	drawCircle(g,xCenter+(int)(xcen*Radius1),xCenter+(int)(ycen*Radius1),(int)(raio*Radius1));
}

//--------------------------------------------- NEW
private void draw_tick_marks(Graphics g){
	int Nmarks=360;
	int i;
	int tmpx1, tmpy1;
	int tmpx2, tmpy2;
	double beta = 2.0*Math.PI;
	int r1, r2,r3,r4;
	
	r4=(int)(Radius3+(Radius2-Radius3)*0.3);
        r3=(int)(Radius3+(Radius2-Radius3)*0.5);
        r1=(int)(Radius3+(Radius2-Radius3)*0.7);
	r2=(int)(Radius2*0.98);

	//g.setColor(bgcolor);
	
	for(i=0;i<Nmarks;i++){
                g.setColor(Color.gray);
                
		tmpx1=(int)(xCenter+r1*Math.cos(beta*i/(Nmarks)));
		tmpy1=(int)(yCenter+r1*Math.sin(beta*i/(Nmarks)));
		if(i%5==0){
			tmpx1=(int)(xCenter+r3*Math.cos(beta*i/(Nmarks)));
			tmpy1=(int)(yCenter+r3*Math.sin(beta*i/(Nmarks)));
                        
		}
                if(i%10==0){
			tmpx1=(int)(xCenter+r4*Math.cos(beta*i/(Nmarks)));
			tmpy1=(int)(yCenter+r4*Math.sin(beta*i/(Nmarks)));
                        
                }
		tmpx2=(int)(xCenter+r2*Math.cos(beta*i/(Nmarks)));
		tmpy2=(int)(yCenter+r2*Math.sin(beta*i/(Nmarks)));
                if(i == 270){tmpx1 = tmpx1+1; tmpx2=tmpx1;}
                {
		g.drawLine(tmpx1,tmpy1,tmpx2,tmpy2);}
	}	
}

//-------------------------------------------------NEW
public void draw_arcs(Graphics g){

	double beginAngle, beginAngle2, Angle, Angle2;
	double raio, del;
	double txpos=xpos;
	while(txpos>0.5){txpos-=0.5;}
	raio = (double)(2.03*Radius3);	
        if(mode ==2){
            beginAngle=GammaZL.Arg2(Complex.POLAR_DEGREE) + 180;
        }
        else{
            beginAngle=GammaZL.Arg2(Complex.POLAR_DEGREE);
        }
        
	Angle=-720.0*xpos;
        while(Math.abs(beginAngle)>360){beginAngle = beginAngle+360.0;}
        while(Math.abs(Angle)>360){Angle = Angle+360.0;}
        
        //drawArcThick(g,(double)(xCenter-raio/2),(double)(yCenter-raio/2),raio,beginAngle,Angle,3,Color.red);
                
	if(mode==2){beginAngle2 = GammaZL.Arg2(Complex.POLAR_DEGREE) + Angle + 180;}
        else{beginAngle2 = GammaZL.Arg2(Complex.POLAR_DEGREE) + Angle;}
	Angle2=-(0.5-txpos)*720.0;
        if(Math.abs(beginAngle2)>360){beginAngle2 = beginAngle2+360.0;}
        if(Math.abs(Angle2)>360){Angle2 = Angle2+360.0;}
        
        drawArcThick(g,(double)(xCenter-raio/2),(double)(yCenter-raio/2),raio+1.0,beginAngle,360,4,outer);
        drawArcThick(g,(double)(xCenter-raio/2),(double)(yCenter-raio/2),raio+1.0,beginAngle2,Angle2,3,Color.green);   
        drawArcThick(g,(double)(xCenter-raio/2),(double)(yCenter-raio/2),raio+1.0,beginAngle,Angle,3,Color.red);
}

public synchronized void setMode(int mode){
		this.mode=mode;
}

private void drawArcThick(Graphics g, double xCenter, double yCenter, double Radius, double startangle, double endangle, int thick, Color color){
	
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(color);
        g2d.setStroke(new BasicStroke(thick));
        
        
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        Arc2D.Double arc_one = new Arc2D.Double(xCenter,yCenter,Radius,Radius,startangle,endangle,0);
        g2d.draw(arc_one);
  
        g2d.setStroke(new BasicStroke(1));
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
}
//----------------------------------------------------END NEW
public void drawPoints(Graphics g){
    
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	int radius=state.s3;
	int tmpx, tmpy;
	if(draw_ZL){
		//ZL
		if(draw_impedance_chart){
		    tmpx=xCenter+(int)(GammaZL.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaZL.Imaginary()*Radius1);
		    g.setColor(Color.red);
		    fillCircle(g,tmpx,tmpy,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpx,tmpy,radius);
		}
		if(draw_admittance_chart){
		    tmpx=xCenter+(int)(GammaYL.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaYL.Imaginary()*Radius1);
		    g.setColor(Color.red);
		    fillCircle(g,tmpx,tmpy,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpx,tmpy,radius);
		}
	}
	if(draw_YL){
		//YL
		if(draw_impedance_chart){
		    tmpx=xCenter+(int)(GammaYL.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaYL.Imaginary()*Radius1);
		    g.setColor(Color.yellow);
		    fillCircle(g,tmpx,tmpy,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpx,tmpy,radius);
		}
		if(draw_admittance_chart){
		    tmpx=xCenter+(int)(GammaZL.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaZL.Imaginary()*Radius1);
		    g.setColor(Color.yellow);
		    fillCircle(g,tmpx,tmpy,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpx,tmpy,radius);
    		}    
	}
	if(draw_Zin){
		if(draw_impedance_chart){
		//Zin
		    tmpx=xCenter+(int)(GammaZin.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaZin.Imaginary()*Radius1);
		    g.setColor(Color.green);
		    fillCircle(g,tmpx,tmpy,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpx,tmpy,radius);                   
		}
		if(draw_admittance_chart){
		    tmpx=xCenter+(int)(GammaYin.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaYin.Imaginary()*Radius1);
		    g.setColor(Color.green);
		    fillCircle(g,tmpx,tmpy,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpx,tmpy,radius);
		}
	}	
	if(draw_Yin){
		//Yin
		if(draw_impedance_chart){
                    int tmpxL, tmpyL;
                    tmpxL=xCenter+(int)(GammaYL.Real()*Radius1);
		    tmpyL=yCenter+(int)(-GammaYL.Imaginary()*Radius1);
		    
		    tmpx=xCenter+(int)(GammaYin.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaYin.Imaginary()*Radius1);
		    //g.setColor(Color.blue.brighter());
                    
                    Color bluebright = new Color(0,150,255);
                    g.setColor(bluebright);
		    fillCircle(g,tmpx,tmpy,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpx,tmpy,radius);
                    
                    /*  Experimental, to put an arc instead of dots.  Only good for admittance outside circle g = 1.0
                    double angle1, angle2, anglestart, angleend, radiusarc;              
                    angle1 = GammaYL.Arg2(Complex.POLAR_DEGREE);
                    
                    if (angle1 < 0.0){anglestart = 360.0+angle1;}
                    else{anglestart = angle1;}
                    
                    angle2 = GammaYin.Arg2(Complex.POLAR_DEGREE);
                    if(angle2 < 0.0 && Yin.Real() < 1.0){
                        angle2 = 360.0+angle2;
                        angleend = - (anglestart - angle2);
                    }
                    else if(angle2 < 0.0 && Yin.Real() >= 1.0){
                        angleend = - anglestart +angle2;
                    }
                    else{
                        angleend = angle2 - anglestart;
                    }
                    
                    
                    radiusarc = Radius1 * Complex.Magnitude(GammaYin);
                    //draw arc
                    MaestroG.drawArcThick(g,xCenter-radiusarc,yCenter-radiusarc,2.0*radiusarc,anglestart,angleend,2,bluebright);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

                    System.out.println(anglestart+"  "+angleend+"   "+angle2+"    "+GammaYL+"     "+GammaYin);
                    
                    //redraw admittance
                    g.setColor(Color.yellow);
                    fillCircle(g,tmpxL,tmpyL,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpxL,tmpyL,radius);
                    */
		}
		if(draw_admittance_chart){
		    tmpx=xCenter+(int)(GammaZin.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaZin.Imaginary()*Radius1);
		    //g.setColor(Color.blue.brighter());
                    g.setColor(new Color(0,150,255));
		    fillCircle(g,tmpx,tmpy,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpx,tmpy,radius);
		
		}    
	}
}

private void draw_swr_circle(Graphics g){
        //---------------------------------------NEW
        if(colore_dark){
            g.setColor(Color.cyan);
        }
        else{
            g.setColor(Color.magenta);
        }
        //--------------------------------------
	
	drawCircle(g,xCenter,yCenter,(int)(Radius1*GammaZL.Magnitude()));
}

private void draw_swr_line(Graphics g){
	int xcor, ycor, xcor2, ycor2, xxcor, yycor, xcor3, ycor3;
	double theta;
        
        if(draw_ZL){
		//For  ZL ...
		if(draw_impedance_chart){
		    theta=-GammaZL.Arg2();
		    g.setColor(new Color(204,50,50));
		    xcor=(int)(GammaZL.Real()*(Radius1+5))+xCenter;
		    ycor=(int)(-GammaZL.Imaginary()*(Radius1+5))+yCenter;
                    xcor3=(int)(GammaZL.Real()*(Radius1-5))+xCenter;
		    ycor3=(int)(-GammaZL.Imaginary()*(Radius1-5))+yCenter;
                    
		    g.drawLine(xCenter,yCenter,xcor3,ycor3);
		    xxcor=(int)(Math.cos(theta)*Radius2)+xCenter;
		    yycor=(int)(Math.sin(theta)*Radius2)+yCenter;
		    g.setColor(Color.red);
		    g.drawLine(xcor,ycor,xxcor,yycor);
		}
		if(draw_admittance_chart){
		    theta=-GammaYL.Arg2();
		    g.setColor(new Color(204,50,50));
		    xcor=(int)(GammaYL.Real()*(Radius1+5))+xCenter;
		    ycor=(int)(-GammaYL.Imaginary()*(Radius1+5))+yCenter;
                    xcor3=(int)(GammaYL.Real()*(Radius1-5))+xCenter;
		    ycor3=(int)(-GammaYL.Imaginary()*(Radius1-5))+yCenter;                    
                    
		    g.drawLine(xCenter,yCenter,xcor3,ycor3);
		    xxcor=(int)(Math.cos(theta)*Radius2)+xCenter;
		    yycor=(int)(Math.sin(theta)*Radius2)+yCenter;
		    g.setColor(Color.red);
		    g.drawLine(xcor,ycor,xxcor,yycor);
		}
	}
	if(draw_YL){
		//For  YL ...
		if(draw_impedance_chart){
		    theta=-GammaYL.Arg2();
		    g.setColor(new Color(204,50,50));
		    xcor=(int)(GammaYL.Real()*(Radius1+5))+xCenter;
		    ycor=(int)(-GammaYL.Imaginary()*(Radius1+5))+yCenter;
		    xcor3=(int)(GammaYL.Real()*(Radius1-5))+xCenter;
		    ycor3=(int)(-GammaYL.Imaginary()*(Radius1-5))+yCenter;
                    
		    g.drawLine(xCenter,yCenter,xcor3,ycor3);
		    xxcor=(int)(Math.cos(theta)*Radius2)+xCenter;
		    yycor=(int)(Math.sin(theta)*Radius2)+yCenter;
		    g.setColor(Color.yellow);
		    g.drawLine(xcor,ycor,xxcor,yycor);
		}
		if(draw_admittance_chart){
		    theta=-GammaZL.Arg2();
		    g.setColor(new Color(204,50,50));
		    xcor=(int)(GammaZL.Real()*(Radius1+5))+xCenter;
		    ycor=(int)(-GammaZL.Imaginary()*(Radius1+5))+yCenter;
                    xcor3=(int)(GammaZL.Real()*(Radius1-5))+xCenter;
		    ycor3=(int)(-GammaZL.Imaginary()*(Radius1-5))+yCenter;
                    
		    g.drawLine(xCenter,yCenter,xcor3,ycor3);
		    xxcor=(int)(Math.cos(theta)*Radius2)+xCenter;
		    yycor=(int)(Math.sin(theta)*Radius2)+yCenter;
		    g.setColor(Color.yellow);
		    g.drawLine(xcor,ycor,xxcor,yycor);
		
		}
	}
	if(draw_Zin){
		//For Zin
		if(draw_impedance_chart){
		    theta=-GammaZin.Arg2();
		    g.setColor(new Color(204,50,50));
		    xcor=(int)(GammaZin.Real()*(Radius1+5))+xCenter;
		    ycor=(int)(-GammaZin.Imaginary()*(Radius1+5))+yCenter;
                    xcor3=(int)(GammaZin.Real()*(Radius1-5))+xCenter;
		    ycor3=(int)(-GammaZin.Imaginary()*(Radius1-5))+yCenter;
                    
		    g.drawLine(xCenter,yCenter,xcor3,ycor3);
		    xxcor=(int)(Math.cos(theta)*Radius2)+xCenter;
		    yycor=(int)(Math.sin(theta)*Radius2)+yCenter;
		    if(xpos==0){
                        g.setColor(Color.red);
                    }
                    else{
                        g.setColor(Color.green);
                    }
                    g.drawLine(xcor,ycor,xxcor,yycor);
		}
		if(draw_admittance_chart){
		    theta=-GammaYin.Arg2();
		    g.setColor(new Color(204,50,50));
		    xcor=(int)(GammaYin.Real()*(Radius1+5))+xCenter;
		    ycor=(int)(-GammaYin.Imaginary()*(Radius1+5))+yCenter;
                    xcor3=(int)(GammaYin.Real()*(Radius1-5))+xCenter;
		    ycor3=(int)(-GammaYin.Imaginary()*(Radius1-5))+yCenter;
                    
		    g.drawLine(xCenter,yCenter,xcor3,ycor3);
		    xxcor=(int)(Math.cos(theta)*Radius2)+xCenter;
		    yycor=(int)(Math.sin(theta)*Radius2)+yCenter;
		    if(xpos==0){
                        g.setColor(Color.red);
                    }
                    else{
                        g.setColor(Color.green);
                    }
                    g.drawLine(xcor,ycor,xxcor,yycor);
		}
	}
	if(draw_Yin){
		//For Yin
		if(draw_impedance_chart){
		    theta=-GammaYin.Arg2();
		    g.setColor(new Color(204,50,50));
                    if(xpos == 0){
                        xcor=(int)(GammaYL.Real()*(Radius1+5))+xCenter;
                        ycor=(int)(-GammaYL.Imaginary()*(Radius1+5))+yCenter;
                        xcor3=(int)(GammaYL.Real()*(Radius1-5))+xCenter;
                        ycor3=(int)(-GammaYL.Imaginary()*(Radius1-5))+yCenter; 
                    }
                    else{
                        xcor=(int)(GammaYin.Real()*(Radius1+5))+xCenter;
                        ycor=(int)(-GammaYin.Imaginary()*(Radius1+5))+yCenter;
                        xcor3=(int)(GammaYin.Real()*(Radius1-5))+xCenter;
                        ycor3=(int)(-GammaYin.Imaginary()*(Radius1-5))+yCenter;
                    }
		    g.drawLine(xCenter,yCenter,xcor3,ycor3);
		    xxcor=(int)(Math.cos(theta)*Radius2)+xCenter;
		    yycor=(int)(Math.sin(theta)*Radius2)+yCenter;
		    if(xpos==0.0){
                        g.setColor(Color.yellow);
                    }
                    else{
                        g.setColor(Color.blue);
                    }
                    //g.setColor(new Color(0,150,255));
		    g.drawLine(xcor,ycor,xxcor,yycor);
		}
		if(draw_admittance_chart){
		    theta=-GammaZin.Arg2();
		    g.setColor(new Color(204,50,50));
                    if(xpos == 0){
                        xcor=(int)(GammaZL.Real()*(Radius1+5))+xCenter;
                        ycor=(int)(-GammaZL.Imaginary()*(Radius1+5))+yCenter;
                        xcor3=(int)(GammaZL.Real()*(Radius1-5))+xCenter;
                        ycor3=(int)(-GammaZL.Imaginary()*(Radius1-5))+yCenter;
                    }
                    else{
                        xcor=(int)(GammaZin.Real()*(Radius1+5))+xCenter;
                        ycor=(int)(-GammaZin.Imaginary()*(Radius1+5))+yCenter;
                        xcor3=(int)(GammaZin.Real()*(Radius1-5))+xCenter;
                        ycor3=(int)(-GammaZin.Imaginary()*(Radius1-5))+yCenter;
                        
                    }
		    g.drawLine(xCenter,yCenter,xcor3,ycor3);
		    xxcor=(int)(Math.cos(theta)*Radius2)+xCenter;
		    yycor=(int)(Math.sin(theta)*Radius2)+yCenter;
		    if(xpos==0){
                        g.setColor(Color.yellow);
                    }
                    else{
                        g.setColor(Color.blue);
                    }
                    //g.setColor(new Color(0,150,255));
		    g.drawLine(xcor,ycor,xxcor,yycor);
		
		}
	}
}

public final synchronized void setZin(Complex Zin){
	this.Zin = Zin;
	GammaZin = EMF.computeGamma(Zin);
	this.Yin = EMF.Inv(Zin);
	GammaYin = EMF.computeGamma(Yin);
}

public final synchronized void setZin(Complex Zin, double Z0){
	setZin(Complex.Divide(Zin,Z0));
}

public final synchronized void setZin(Complex Zin, Complex Z0){
	setZin(Complex.Divide(Zin,Z0));
}

public final synchronized void setYin(Complex Yin){
	this.Yin = Yin;
	this.Zin = EMF.Inv(Yin);
	GammaZin = EMF.computeGamma(Zin);
	GammaYin = EMF.computeGamma(Yin);
}

public final synchronized void setYin(Complex Yin, double Z0){
	setZin(Complex.Divide(Yin,Z0));
}

public final synchronized void setYin(Complex Yin, Complex Z0){
	setZin(Complex.Divide(Zin,Z0));
}

public final synchronized void setZL(Complex ZL){
	this.ZL = ZL;
	GammaZL = EMF.computeGamma(this.ZL);
	this.YL = EMF.Inv(ZL);
	GammaYL = EMF.computeGamma(this.YL);
}

public final synchronized void setZL(Complex ZL, double Z0){
	setZL(Complex.Divide(ZL,Z0));
}

public final synchronized void setZL(Complex ZL, Complex Z0){
	setZL(Complex.Divide(ZL,Z0));
}

public final synchronized void setYL(Complex YL){
	this.YL = YL;
	GammaZL = EMF.computeGamma(this.YL,false);
	this.ZL = EMF.Inv(YL);
}

public final synchronized void setYL(Complex YL, double Z0){
	setYL(Complex.Multiply(YL,Z0));
}

public final synchronized void setYL(Complex YL, Complex Z0){
	setYL(Complex.Multiply(YL,Z0));
}

public synchronized void set_swr_circle(boolean arg){
	draw_swr_circle=arg;
}

public synchronized void set_swr_line(boolean arg){
	draw_swr_line=arg;
}

public synchronized void set_arcs(boolean arg){
	draw_arcs = arg;
}

public synchronized void set_impedance(boolean arg){
	draw_impedance_chart=arg;
	draw_admittance_chart=!arg;
}

public synchronized void set_admittance(boolean arg){
	draw_admittance_chart=arg;
	draw_impedance_chart=!arg;
}

public synchronized void setZL(boolean arg){
	draw_ZL = arg;
}

public synchronized void setYL(boolean arg){
	draw_YL = arg;
}

public synchronized void setZin(boolean arg){
	draw_Zin = arg;	
}

public synchronized void setYin(boolean arg){
	draw_Yin = arg;
}

public synchronized void setxpos(double xpos){
	this.xpos=xpos;
}

private  void cricket_1(MouseEvent evt){
	int tx1, ty1;
	int x, y;
	x=evt.getX();
	y=evt.getY();
	tx1=x-xCenter;
	ty1=y-yCenter;	
	if(Math.pow(tx1,2.0)+Math.pow(ty1,2.0)<=Math.pow(Radius1,2.0)){
		Graphics g = this.getGraphics();
		g.clipRect(xProbe-10,yProbe-10,20,20);
		paint(g);
		g.clipRect(0,0,getSize().width,getSize().height);
		SmithCanvas.plot_red_cross(g,x,y);
		g.dispose();
		xProbe=x;
		yProbe=y;
		GammaProbe=new Complex(1.0*tx1/Radius1,-1.0*ty1/Radius1);
		ZProbe=EMF.computeZ(GammaProbe);
	}
}
public void mouseDragged(MouseEvent evt){;}//cricket_1(evt);}
public void mouseMoved(MouseEvent evt){;}
public void mouseClicked(MouseEvent evt){;}//cricket_1(evt);}
public void mouseEntered(MouseEvent evt){;}//setCursor(cursorB);}
public void mouseExited(MouseEvent evt){;}//setCursor(cursorA);}
public void mousePressed(MouseEvent evt){;}//cricket_1(evt);}
public void mouseReleased(MouseEvent evt){;}//cricket_1(evt);}

public final Complex getZProbe(){
	return ZProbe;
}

public final Complex getGammaProbe(){
	return GammaProbe;
}

public static void plot_red_cross(Graphics g, int x, int y){
	g.setColor(Color.white);
			g.fillRect(x-5, y-1,11,3);
			g.fillRect(x-1,y-5,3,11);
		g.setColor(Color.red);
			g.drawLine(x-4,y,x+4,y);
			g.drawLine(x,y-4,x,y+4);
}

public Complex getZL(){
	return (Complex)ZL.clone();
}

public Complex getZin(){
	return (Complex)Zin.clone();
}

public Complex getYin(){
	return (Complex)Yin.clone();
}

//-------------------------------------NEW
public synchronized void setColoreDark(boolean colore_dark){
	this.colore_dark = colore_dark;
	repaint();
}
//---------------------------------------

}//End SmithCanvas

class SmithMan extends Panel{
    private Label[] vlab, lab;
    private static final Color bgcolor = new Color(236,236,236);
    int mode;//mode=1 Impedance, mode=2 Admittance Chart
    boolean IsLoadOpen, IsLoadShort, IsLoadImaginary, IsLoadRegular, Gamma_plusone, Gamma_minusone;
    public Complex ZL, YL, GammaL, Zin, Yin, GammaIn;
    private Image imago;
    private Graphics buffer;
    StateVars state;

        SmithMan(int width, int height,StateVars state){
		super();
                this.state = state;
		setLayout(null);
		mode=1;
		setBackground(bgcolor);
		
                Font labelfont = new Font("Sanserif",Font.PLAIN,state.s10); //TheFonts.italic11;
                
                int ystart = state.s1;
                int ystep = state.s14; 
                int yhigh = state.s12;
                IsLoadOpen = false;
                IsLoadShort = false;
                IsLoadImaginary = false;
                IsLoadRegular = true;
                ZL = new Complex(2.0,1.6);
                YL = EMF.Inv(ZL);
                Zin = ZL;
                Yin = YL;
                
                if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)==0.0){
                    IsLoadShort = true;
                    IsLoadOpen = false;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                }
                else if(Complex.Real(YL)==0.0 && Complex.Imaginary(YL)==0.0){
                    IsLoadOpen = true;
                    IsLoadShort = false;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                }
                else if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)!=0.0){
                    IsLoadImaginary = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadRegular = false;
                }
                else if((Complex.Real(ZL)!=0.0 && Complex.Imaginary(ZL)!=0.0) ||
                    (Complex.Real(ZL)!=0.0 && Complex.Imaginary(ZL)==0.0))
                {
                    IsLoadRegular = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadImaginary = false;
                }
                
                //not used -----------------------------------
                lab = new Label[6];
		
		lab[0] = new Label("z",Label.LEFT);
		lab[1] = new Label("",Label.LEFT);
                lab[2] = new Label("y",Label.LEFT);
		lab[3] = new Label("z(d)",Label.LEFT);
		lab[4] = new Label("(d)",Label.LEFT);
		lab[5] = new Label("y(d)",Label.LEFT);
                
                lab[0].setBounds(state.s30,ystart-state.s1,state.s10,yhigh);
                lab[1].setBounds(state.s30,ystart+ystep-state.s1,0,yhigh);
                lab[2].setBounds(state.s30,ystart+2*ystep+state.s1,state.s30,yhigh);
                lab[3].setBounds(state.s30,ystart+3*ystep+state.s1,state.s30,yhigh);
                lab[4].setBounds(state.s40,ystart+4*ystep+state.s1,state.s20,yhigh);
                lab[5].setBounds(state.s30,ystart+5*ystep+state.s1,state.s30,yhigh);
                //---------------------------------------------
                
		vlab = new Label[6];                
                for(int i = 0; i<lab.length; i++){
			vlab[i] = new Label("",Label.LEFT);
			vlab[i].setFont(labelfont);
			lab[i].setFont(labelfont);
		}
		
                int vstep = state.s75/5;
		int vstepinit = 0;
		int xinit = state.s75;
		int xinit2 = xinit+state.s5;
		int xinit3 = xinit2+state.s70;
		
		
		vlab[0].setBounds(xinit2,vstepinit,state.s225,vstep+state.s2);
		//add(vlab[0]);
		vlab[1].setBounds(xinit2,vstepinit+vstep,state.s225,vstep+state.s2);
		//add(vlab[1]);
		vlab[2].setBounds(xinit2,vstepinit+2*vstep,state.s225,vstep+state.s2);
		//add(vlab[2]);
		vlab[3].setBounds(xinit2,vstepinit+3*vstep,state.s225,vstep+state.s2);
		//add(vlab[3]);
		vlab[4].setBounds(xinit2,vstepinit+4*vstep,state.s225,vstep+state.s2);
		//add(vlab[4]);
		vlab[5].setBounds(xinit2,vstepinit+5*vstep,state.s225,vstep+state.s2);
		//add(vlab[5]);
                vlab[0].setFont(labelfont);
                vlab[1].setFont(labelfont);
                vlab[2].setFont(labelfont);
                vlab[3].setFont(labelfont);
                vlab[4].setFont(labelfont);
                vlab[5].setFont(labelfont);
	}
        
        public void paint(Graphics g){
	    if(imago == null){
		imago = createImage(getSize().width,getSize().height);
		buffer = imago.getGraphics();
		drawSmithMan(buffer);
	    }
	    else{
		drawSmithMan(buffer);
	    }
	    g.drawImage(imago,0,0,null);
        }

        //Addition to reduce flicker new routine
        public void update(Graphics g){		// added to avoid clearing
            paint(g);
        }
        
	public void drawSmithMan(Graphics g){
		
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
            g.clearRect(0,0,getSize().width,getSize().height);
                
	    if(mode==1){
		g.setColor(Color.red);
		g.fillOval(state.s7,state.s5,state.s8,state.s8);
		g.setColor(Color.yellow);
		g.fillOval(state.s7,state.s33,state.s8,state.s8);
		g.setColor(Color.green);
		g.fillOval(state.s7,state.s50,state.s8,state.s8);
                g.setColor(new Color(0,150,255));
		g.fillOval(state.s7,state.s78,state.s8,state.s8);	
    	    }
	    if(mode==2){
		g.setColor(Color.yellow);
		g.fillOval(state.s7,state.s5,state.s8,state.s8);
		g.setColor(Color.red);
		g.fillOval(state.s7,state.s33,state.s8,state.s8);
                g.setColor(new Color(0,150,255));
		g.fillOval(state.s7,state.s50,state.s8,state.s8);
		g.setColor(Color.green);
		g.fillOval(state.s7,state.s78,state.s8,state.s8);
	    }
		g.setColor(Color.black);
		g.drawOval(state.s7,state.s5,state.s8,state.s8);
		g.drawOval(state.s7,state.s33,state.s8,state.s8);
		g.drawOval(state.s7,state.s50,state.s8,state.s8);
		g.drawOval(state.s7,state.s78,state.s8,state.s8);
	    
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                
                FontMetrics fm;
		
		String alpha, Ohm, lambda, infinity, Gamma, plusj, minusj, sign;
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393";
                int vstep = state.s75/5;
		int vstepinit = state.s12;
		int xinit = state.s35;
		int xinit2 = xinit+state.s25;
		int xinit3 = xinit2+state.s70;
                int xinit4 = state.s80;            
            //-------------------------------------------------------------------------------------------
		
                g.setFont(new Font("Sanserif",Font.PLAIN,state.font11));
		plusj =" + j ";
		minusj=" - j ";
                
                fm = g.getFontMetrics();
             //-----------------------------------------------------------------------
             double tempR = 1.0, tempX = 2.0;
             int stepx = 0;
             
                g.drawString("=",xinit+state.s30,vstepinit);
                g.drawString("=",xinit+state.s30,vstepinit + vstep);
                g.drawString("=",xinit+state.s30,vstepinit + 2*vstep);
                g.drawString("=",xinit+state.s30,vstepinit + 3*vstep);
                g.drawString("=",xinit+state.s30,vstepinit + 4*vstep);                 
                g.drawString("=",xinit+state.s30,vstepinit + 5*vstep);
	    
                MaestroG.subscripter(""+vlab[0].getText(),"","",g,state.font10,xinit4,vstepinit);
                MaestroG.subscripter(""+vlab[1].getText(),"","",g,state.font10,xinit4,vstepinit+vstep);
                MaestroG.subscripter(""+vlab[2].getText(),"","",g,state.font10,xinit4,vstepinit+2*vstep);
                MaestroG.subscripter(""+vlab[3].getText(),"","",g,state.font10,xinit4,vstepinit+3*vstep);
                MaestroG.subscripter(""+vlab[4].getText(),"","",g,state.font10,xinit4,vstepinit+4*vstep);
                MaestroG.subscripter(""+vlab[5].getText(),"","",g,state.font10,xinit4,vstepinit+5*vstep);
                
             if(mode==1){
                 if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)==0.0){
                    IsLoadShort = true;
                    IsLoadOpen = false;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                }
                else if((Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)<-1.0E130)||Complex.Magnitude(ZL)>1.0E130){
                    IsLoadOpen = true;
                    IsLoadShort = false;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                }
                else if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)!=0.0){
                    IsLoadImaginary = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadRegular = false;
                }
                else if((Complex.Real(ZL)!=0.0 && Complex.Imaginary(ZL)!=0.0) ||
                    (Complex.Real(ZL)!=0.0 && Complex.Imaginary(ZL)==0.0))
                {
                    IsLoadRegular = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadImaginary = false;
                }
                
                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

                //-----------------------------------------------------------------------------
                g.setColor(Color.black);
                MaestroG.subscripter(" z","L","",g,state.font11,xinit,vstepinit);
                MaestroG.subscripterSS3(" "+Gamma,"L","",g,state.font11,xinit+state.s1,vstepinit + vstep);
                MaestroG.subscripter(" y","L","",g,state.font11,xinit,vstepinit+ 2*vstep);
                MaestroG.subscripter("z","","(d)",g,state.font11,xinit,vstepinit + 3*vstep);
                MaestroG.subscripterSS3(""+Gamma,"","(d)",g,state.font11,xinit+state.s1,vstepinit + 4*vstep);                 
                MaestroG.subscripter("y","","(d)",g,state.font11,xinit,vstepinit + 5*vstep);           
            }
                 
             else if(mode == 2){
                
                if(Complex.Real(YL)==0.0 && Complex.Imaginary(YL)==0.0){
                    IsLoadShort = false;
                    IsLoadOpen = true;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                }
                else if((Complex.Real(YL)==0.0 && Complex.Imaginary(YL)<-1.0E130)||Complex.Magnitude(YL)>1.0E100 ||
                    Complex.Magnitude(ZL)==0.0){
                    IsLoadOpen = false;
                    IsLoadShort = true;
                    IsLoadImaginary = false;
                    IsLoadRegular = false;
                }
                else if(Complex.Real(YL)==0.0 && Complex.Imaginary(YL)!=0.0){
                    IsLoadImaginary = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadRegular = false;
                }
                else if((Complex.Real(YL)!=0.0 && Complex.Imaginary(YL)!=0.0) ||
                    (Complex.Real(YL)!=0.0 && Complex.Imaginary(YL)==0.0))
                {
                    IsLoadRegular = true;
                    IsLoadShort = false;
                    IsLoadOpen = false;
                    IsLoadImaginary = false;
                }
                
                //--------------------------------------------------------------------------------------------- 
                 
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

                g.setColor(Color.black);
		MaestroG.subscripter(" y","L","",g,state.font11,xinit,vstepinit);
                MaestroG.subscripterSS3(" "+Gamma,"L","",g,state.font11,xinit+state.s1,vstepinit + vstep);
                MaestroG.subscripter(" z","L","",g,state.font11,xinit,vstepinit+ 2*vstep);
                MaestroG.subscripter("y","","(d)",g,state.font11,xinit,vstepinit + 3*vstep);
                MaestroG.subscripterSS3(""+Gamma,"","(d)",g,state.font11,xinit+state.s1,vstepinit + 4*vstep);                 
                MaestroG.subscripter("z","","(d)",g,state.font11,xinit,vstepinit + 5*vstep);      
            }
	}
        
        public void setZL(Complex ZL){
		Complex GammaL;
		if(mode==1){
		    lab[0].setText("Load Impedance = ");
		
		    if(Complex.Real(ZL)==0.0&&Complex.Imaginary(ZL)<-1.0e135){
			vlab[0].setText(" \u221e ");}
		    else{
			vlab[0].setText(ZL.toString(Complex.CARTESIAN,5));}
		    
		    lab[2].setText("Load Admittance =");
		
		    if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)==0.0){
			vlab[2].setText(" \u221e ");}
		    else{
			vlab[2].setText(EMF.Inv(ZL).toString(Complex.CARTESIAN,5));}
		}
		
		else{
		    lab[0].setText("Load Admittance =");
		
		    if(Complex.Real(ZL)==0.0 && Complex.Imaginary(ZL)==0.0){
			vlab[0].setText(" \u221e ");}
		    else{
			vlab[0].setText(EMF.Inv(ZL).toString(Complex.CARTESIAN,5));}
		
		    lab[2].setText("Load Impedance = ");
		
		    if(Complex.Real(ZL)==0.0&&Complex.Imaginary(ZL)<-1.0e135){
			vlab[2].setText(" \u221e ");}
		    else{
			vlab[2].setText(ZL.toString(Complex.CARTESIAN,5));}
		}
		
		GammaL=EMF.computeGamma(ZL);
		vlab[1].setText(GammaL.toString(Complex.POLAR_DEGREE,5));
	}
	public void setZL(Complex ZL, double Z0){
		setZL(Complex.Divide(ZL,Z0));
	}
	public void setZL(Complex ZL, Complex Z0){
		setZL(Complex.Divide(ZL,Z0));
	}
	
	public void setYL(Complex YL){
		Complex GammaL;
		if(mode==1){
		    lab[0].setText("Load Impedance = ");
		
		    if(Complex.Real(YL)==0.0&&Complex.Imaginary(YL)==0.0){
			vlab[0].setText(" \u221e ");}
		    else{
			vlab[0].setText(EMF.Inv(YL).toString(Complex.CARTESIAN,5));}
		
		}
		else{
		    lab[0].setText("Load Admittance =");
		
		    if(Complex.Real(YL)==0.0&&Complex.Imaginary(YL)<-1.0e135){
			vlab[0].setText(" \u221e ");}
		    else{
			vlab[0].setText(YL.toString(Complex.CARTESIAN,5));}
		}
		
		GammaL=EMF.computeGamma(YL,false);
		vlab[1].setText(GammaL.toString(Complex.POLAR_DEGREE,5));
	}
	
	public void setYL(Complex YL, double Z0){
		setYL(Complex.Multiply(YL,Z0));
	}
	
	public void setYL(Complex YL, Complex Z0){
		setYL(Complex.Divide(YL,Z0));
	}
	
	public void setZin(Complex Zin){
		Complex GammaIn, Yin;
		if(mode==1){
		    lab[5].setText("Line Admittance = ");
		    lab[3].setText("Line Impedance = ");
		    if(Complex.Real(Zin)==0.0&&Complex.Imaginary(Zin)<-1.E138)
			    {vlab[3].setText("\u221e");}
		    else{vlab[3].setText(Zin.toString(Complex.CARTESIAN,5));}
		
		    Yin = EMF.Inv(Zin);
		    GammaIn = EMF.computeGamma(Zin);
		    vlab[4].setText(GammaIn.toString(Complex.POLAR_DEGREE,5));
		
			if(Complex.Real(Yin)==0.0&&Complex.Imaginary(Yin)<-1.E138)
			{vlab[5].setText("\u221e");}
		    else{
		    vlab[5].setText(Yin.toString(Complex.CARTESIAN,5));
		    }
		}
		else{
		    lab[3].setText("Line Admittance = ");
		    lab[5].setText("Line Impedance = ");
		    if(Complex.Real(Zin)==0.0&&Complex.Imaginary(Zin)<-1.E138)
			    {vlab[3].setText("\u221e");}
		    else{vlab[5].setText(Zin.toString(Complex.CARTESIAN,5));}
		
		    Yin = EMF.Inv(Zin);
		    GammaIn = EMF.computeGamma(Zin);
		    vlab[4].setText(GammaIn.toString(Complex.POLAR_DEGREE,5));
                    vlab[3].setText(Yin.toString(Complex.CARTESIAN,5));
		}
	}
	
	public void setZin(Complex Zin, double Z0){
		setZin(Complex.Divide(Zin,Z0));
	}
	public void setZin(Complex Zin, Complex Z0){
		setZin(Complex.Divide(Zin,Z0));
	}
	public void setYin(Complex Yin){
		Complex GammaIn, Zin;
		if(mode==1){
		    lab[5].setText("Line Admittance = ");
		    lab[3].setText("Line Impedance = ");
		
		    if(Complex.Real(Yin)==0.0&&Complex.Imaginary(Yin)<-1.E138)
			    {vlab[5].setText("\u221e");}
		    else{vlab[5].setText(Yin.toString(Complex.CARTESIAN,5));}
		
		    Zin = EMF.Inv(Yin);
		    GammaIn = EMF.computeGamma(Zin);
		    vlab[4].setText(GammaIn.toString(Complex.POLAR_DEGREE,5));
		    if(Complex.Real(Zin)==0.0&&Complex.Imaginary(Zin)<-1.E138)
			{vlab[3].setText("\u221e");}
		    else{vlab[3].setText(Zin.toString(Complex.CARTESIAN,5));}
		}
		else{
		    lab[3].setText("Line Admittance = ");
		    lab[5].setText("Line Impedance = ");
		
		    if(Complex.Real(Yin)==0.0&&Complex.Imaginary(Yin)<-1.E138)
			    {vlab[3].setText("\u221e");}
		    else{vlab[3].setText(Yin.toString(Complex.CARTESIAN,5));}
		
		    Zin = EMF.Inv(Yin);
		    GammaIn = EMF.computeGamma(Zin);
		    vlab[4].setText(GammaIn.toString(Complex.POLAR_DEGREE,5));
		    if(Complex.Real(Zin)==0.0&&Complex.Imaginary(Zin)<-1.E138)
			{vlab[5].setText("\u221e");}
		    else{vlab[5].setText(Zin.toString(Complex.CARTESIAN,5));}
		}
	}
	
	public void setYin(Complex Yin, double Z0){
		setYin(Complex.Divide(Yin,Z0));
	}
	
	public void setYin(Complex Yin, Complex Z0){
		setYin(Complex.Divide(Yin,Z0));
	}
	
	public synchronized void setMode(int mode){
		this.mode=mode;
	}
}//End of SmithMan	


//--------------------------------------------------------------------------------

class SmithProbe extends Panel{
Label[] lab;
private int mode;
private static final Color bcolor = new Color(236,236,236);
private double xpos_update;
StateVars state;

    SmithProbe(int width, int height,StateVars state){
	super();
        this.state = state;
	setLayout(null);
	setBackground(bcolor);
        xpos_update = 0.0;
	Font labfont = TheFonts.sanSerif11;
	Panel p = new Panel();
	p.setLayout(new GridLayout(2,1));
	lab = new Label[2];
	lab[0] = new Label(" ",Label.LEFT);
	lab[1] = new Label(" ",Label.LEFT);
	lab[0].setFont(labfont);
	lab[1].setFont(labfont);
	//p.add(lab[0]);
	//p.add(lab[1]);
	//add(p);
	p.setBounds(25,1,width-25,height-1);	
	mode=1;
}

public void paint(Graphics g){
	int width=getSize().width;
	int height=getSize().height;
        
        g.setColor(bcolor);
	g.fillRect(1,1,width-2,height-2);
	draw_icon(g);
        setxpos(0.0);
}

public void update(Graphics g){
	draw_icon(g);
}

public void setxpos(double xposnew){
        xpos_update = xposnew; 
        repaint();
}


public void set(String str1, String str2){
	lab[0].setText(str1);
	lab[1].setText(str2);
}

private void draw_icon(Graphics g){
	//g.clipRect(2,2,getSize().width/4,getSize().height-2);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
	g.clearRect(0,0,getSize().width,getSize().height);
        if(mode==1){
                String temp1, temp2, temp3, temp4;
                
                if(xpos_update<=0.5){
                    temp1 = "d = "+MaestroA.rounder(xpos_update,4)+" \u03bb";
                    temp2 = "0.5 \u03bb - d = "+MaestroA.rounder((0.5-xpos_update),4)+" \u03bb";
                    temp3 = "2\u03b2 d = "+MaestroA.rounder(xpos_update*4*Math.PI,4)+" rad = "+MaestroA.rounder(xpos_update*4*180,4)+"\u00ba";
                    temp4 = "2\u03b2 (0.5 \u03bb - d) = "+MaestroA.rounder((0.5-xpos_update)*4*Math.PI,4)+" rad = "+MaestroA.rounder((0.5-xpos_update)*4*180,4)+"\u00ba";
	    	}
                else{
                    while(xpos_update>0.5) {xpos_update-=0.5;}
                    temp1 = "d' = "+MaestroA.rounder(xpos_update,4)+" \u03bb";
                    temp2 = "0.5 \u03bb - d' = "+MaestroA.rounder((0.5-xpos_update),4)+" \u03bb";
                    temp3 = "2\u03b2 d' = "+MaestroA.rounder(xpos_update*4*Math.PI,4)+" rad = "+MaestroA.rounder(xpos_update*4*180,4)+"\u00ba";
                    temp4 = "2\u03b2 (0.5 \u03bb - d') = "+MaestroA.rounder((0.5-xpos_update)*4*Math.PI,4)+" rad = "+MaestroA.rounder((0.5-xpos_update)*4*180,4)+"\u00ba";
                }
                
		g.setColor(bcolor);
		g.fillRect(1,1,22,28);
		FontMetrics fm = g.getFontMetrics();
		double sep, sep2, height;
				
                sep = (float)state.s4; height = (float)state.s10;
                Color newred = new Color(250,0,0);
                MaestroG.drawArcThick(g,(float)state.s7,sep,height,40.0,180.0,3,newred);
                
		sep = (float)state.s20; height = (float)state.s10;
                Color newgreen = new Color(0,220,0);
                MaestroG.drawArcThick(g,(float)state.s7,sep,height,40.0,180.0,3,newgreen);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                g.setColor(Color.black);
                MaestroG.subscripter(temp1,"","",g,state.font11,state.s28,state.s12);
                MaestroG.subscripter(temp2,"","",g,state.font11,state.s28,state.s30);
                g.setColor(Color.red.darker());
                MaestroG.subscripter(temp3,"","",g,state.font11,state.s152,state.s12);
                g.setColor(Color.blue.darker());
                MaestroG.subscripter(temp4,"","",g,state.font11,state.s152,state.s30);
	}
	else if(mode==2){
		//int x=15, y=getSize().height/2;
		//g.setColor(bcolor);
		//g.fillRect(1,1,25,28);
		//plot_red_cross(g,x,y);
	}
	else{
		g.setColor(bcolor);
		g.fillRect(1,1,25,28);
	}
	g.dispose();
}

public static void plot_red_cross(Graphics g, int x, int y){
                g.setColor(Color.gray);
			g.fillRect(x-6, y-2,13,5);
			g.fillRect(x-2,y-6,5,13);
                g.setColor(Color.white);
			g.fillRect(x-5, y-1,11,3);
			g.fillRect(x-1,y-5,3,11);
		g.setColor(Color.red);
			g.drawLine(x-4,y,x+4,y);
			g.drawLine(x,y-4,x,y+4);
}


public synchronized void setmode(int mode){
	this.mode=mode;
	repaint();
}
		
}//End of SmithProbe

//----------------------------------------------------------------------

class SmithControls extends Panel{
Checkbox imp, adm;
CheckboxGroup cgrid;
Checkbox swr, swrline;
private static final Color bgcolor = new Color(236,236,236);
//private static final Color bgcolor = new Color(216,216,191);
SmithControls(){
	//setLayout(new GridLayout(2,2));
	setLayout(null);
	setBackground(bgcolor);
	cgrid = new CheckboxGroup();
	
	
	imp = new Checkbox("Impedance Chart",cgrid,true);
	adm = new Checkbox("Admittance Chart",cgrid,false);
	
	swr = new Checkbox("SWR Circle",null,false);
	swrline = new Checkbox("Cursor Line",null,false);
		
	//add(imp);
	//add(adm);
	//add(swr);
	//add(swrline);
	
	imp.setBounds(20,2,180,24);
	adm.setBounds(210,2,170,24);
	
	imp.setBackground(bgcolor);
	adm.setBackground(bgcolor);
	swr.setBackground(bgcolor);
	swrline.setBackground(bgcolor);
}
public void paint(Graphics g){
                //g.setColor(Color.white);
		//g.drawLine(0,0,0,getSize().height-1);
		//g.drawLine(0,0,getSize().width-1,0);
		//g.setColor(Color.black);
		//g.drawLine(getSize().width-1,0,getSize().width-1,getSize().height-1);
		//g.drawLine(0,getSize().height-1,getSize().width-1,getSize().height-1);
}

}//End of SmithControls;
