//SmithPanel.java
/** 
 * @author - Singh T. Jr, and U. Ravaioli
 * Maestro Series, Amanogawa, 1997
 */
/* Usage: SmithPanel sp = new SmithPanel(); sp.setBounds(5,5,310,469); */
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;

import java.net.*;
import java.awt.event.*;

public class SmithPanel2 extends Panel implements MouseListener, MouseMotionListener, ItemListener{
	public SmithCanvas2 sc;
	public SmithMan2 sm; 
	private SmithProbe2 sp;
	public SmithControls2 scon;
	//private static final Color bgcolor = new Color(170,200,200);
	private static final Color bgcolor = Color.white;
	//public Checkbox large;
        
        //---------------------------------NEW
        public Button large;
        public Button colore;
        public boolean colore_dark;
        
        protected static final Font buttonfont = TheFonts.sanSerif11;
        //--------------------------------------
	
public SmithPanel2(){
	setLayout(null);
        setFont(TheFonts.sanSerif14);

	//large = new Checkbox("Reduce",null,false);
	//add(large);
	//large.setBackground(Color.white);
	//large.setBounds(5,2,68,20);
        
        //-------------------------------------NEW
        large = new Button("Reduce");
        add(large);
	large.setBackground(Color.white);
	large.setBounds(7,7,50,20);
        large.setFont(buttonfont);
        
         colore = new Button("Color");
	add(colore);
	colore.setBackground(Color.white);
	colore.setBounds(520,7,40,20);
        colore.setFont(buttonfont);
        
        colore_dark = false;
        //----------------------------------------

	
	setBackground(bgcolor);
	
	sc = new SmithCanvas2();
	add(sc);
	sc.setBounds(5,5,557,557);
	
	//sm = new SmithMan(340,65);
	sm = new SmithMan2(242,97);
	add(sm);
	sm.setBounds(6,568,242,97);

	sp = new SmithProbe2(306,45);
	add(sp);
	sp.setBounds(254,568,306,45);

	scon = new SmithControls2();
	add(scon);
	scon.setBounds(254,619,307,47);
	
	Panel pgraph1 = new Panel();
	    pgraph1.setBackground(Color.black);
	    add(pgraph1);
	    pgraph1.setBounds(5,567,245,100);
	    
	Panel pgraph2 = new Panel();
	    pgraph2.setBackground(Color.black);
	    add(pgraph2);
	    pgraph2.setBounds(253,567,309,48);
	      
	Panel pgraph4 = new Panel();
	    pgraph4.setBackground(Color.black);
	    add(pgraph4);
	    pgraph4.setBounds(253,618,309,49);
	
	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);
    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 SmithCanvas2 extends Canvas implements MouseListener, MouseMotionListener{
//------------------------------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;
public boolean draw_impedance_chart;
public boolean draw_admittance_chart;
public boolean draw_swr_circle;
public 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);
private int mode;
private Image im;
private Graphics buf;
public boolean OverRide = false;
public SmithCanvas2(){
	super();
        setFont(TheFonts.sanSerif14);
	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;
	
	//Draw the black rectangle      REMOVE
	//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);
	drawCircle(g,xCenter,yCenter,(int)(Radius1*0.04));//NEW
	

	//Draw external and internal white circle //REMOVE
	//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==1){
            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==1){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,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){
	int radius=4;
	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){
		    tmpx=xCenter+(int)(GammaYin.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaYin.Imaginary()*Radius1);
		    //g.setColor(Color.blue);
                    g.setColor(new Color(0,150,255));
		    fillCircle(g,tmpx,tmpy,radius);
		    g.setColor(Color.black);
		    drawCircle(g,tmpx,tmpy,radius);
		}
		if(draw_admittance_chart){
		    tmpx=xCenter+(int)(GammaZin.Real()*Radius1);
		    tmpy=yCenter+(int)(-GammaZin.Imaginary()*Radius1);
		    //g.setColor(Color.blue);
                    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;
	//System.out.println("draw_ZL = "+draw_ZL+"  draw_YL = "+draw_YL+"   draw_Zin = "+draw_Zin+"   draw_Yin = "+draw_Yin);
	if(draw_ZL){
		//For  ZL ...
		if(draw_impedance_chart){
		    theta=-GammaZL.Arg2();
		    g.setColor(new Color(204,50,50));
		    xcor=(int)(GammaZL.Real()*(Radius1+8))+xCenter;
		    ycor=(int)(-GammaZL.Imaginary()*(Radius1+8))+yCenter;
                    xcor3=(int)(GammaZL.Real()*(Radius1-8))+xCenter;
		    ycor3=(int)(-GammaZL.Imaginary()*(Radius1-8))+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+8))+xCenter;
		    ycor=(int)(-GammaYL.Imaginary()*(Radius1+8))+yCenter;
		    xcor3=(int)(GammaYL.Real()*(Radius1-8))+xCenter;
		    ycor3=(int)(-GammaYL.Imaginary()*(Radius1-8))+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+8))+xCenter;
		    ycor=(int)(-GammaYL.Imaginary()*(Radius1+8))+yCenter;
		    xcor3=(int)(GammaYL.Real()*(Radius1-8))+xCenter;
		    ycor3=(int)(-GammaYL.Imaginary()*(Radius1-8))+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+8))+xCenter;
		    ycor=(int)(-GammaZL.Imaginary()*(Radius1+8))+yCenter;
		    xcor3=(int)(GammaZL.Real()*(Radius1-8))+xCenter;
		    ycor3=(int)(-GammaZL.Imaginary()*(Radius1-8))+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+8))+xCenter;
		    ycor=(int)(-GammaZin.Imaginary()*(Radius1+8))+yCenter;
		    xcor3=(int)(GammaZin.Real()*(Radius1-8))+xCenter;
		    ycor3=(int)(-GammaZin.Imaginary()*(Radius1-8))+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.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+8))+xCenter;
		    ycor=(int)(-GammaYin.Imaginary()*(Radius1+8))+yCenter;
                    xcor3=(int)(GammaYin.Real()*(Radius1-8))+xCenter;
		    ycor3=(int)(-GammaYin.Imaginary()*(Radius1-8))+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.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));
		    xcor=(int)(GammaYin.Real()*(Radius1+8))+xCenter;
		    ycor=(int)(-GammaYin.Imaginary()*(Radius1+8))+yCenter;
                    xcor3=(int)(GammaYin.Real()*(Radius1-8))+xCenter;
		    ycor3=(int)(-GammaYin.Imaginary()*(Radius1-8))+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));
		    xcor=(int)(GammaZin.Real()*(Radius1+8))+xCenter;
		    ycor=(int)(-GammaZin.Imaginary()*(Radius1+8))+yCenter;
                    xcor3=(int)(GammaZin.Real()*(Radius1-8))+xCenter;
		    ycor3=(int)(-GammaZin.Imaginary()*(Radius1-8))+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);
		
		}
	}
}


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 SmithMan2 extends Panel{
private Label[] vlab, lab;
private static final Color bgcolor = new Color(236,236,236);
//private static final Color bgcolor = new Color(216,216,191);
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;

	SmithMan2(int width, int height){
		super();
                setFont(TheFonts.sanSerif14);
		setLayout(null);
		mode=1;
		setBackground(bgcolor);
		Font labelfont=TheFonts.italic11;
		
		lab = new Label[6];
		vlab = new Label[6];
		lab[0] = new Label("Load Impedance = ",Label.LEFT);
		lab[1] = new Label("Load Refl. Coef. = ",Label.LEFT);
		lab[2] = new Label("Load Admittance = ",Label.LEFT);
		lab[3] = new Label("Line Impedance = ",Label.LEFT);
		lab[4] = new Label("Line Refl. Coef. = ",Label.LEFT);
		lab[5] = new Label("Line Admittance = ",Label.LEFT);
		for(int i = 0; i<lab.length; i++){
			vlab[i] = new Label("",Label.LEFT);
			vlab[i].setFont(labelfont);
			lab[i].setFont(labelfont);
		}
		
                ZL = new Complex(2.0,1.6);
                YL = EMF.Inv(ZL);
                Zin = ZL;
                Yin = YL;
                
		//input for ourwidth the number used for the smith man panel
		int ourwidth = 242;
		//78 is 1 pixel smaller than the width of the panel (79)
		int vstep = 75/5;
		
		int vstepinit = 0;
		int xinit = 15; 
		int xinit2 = 48*ourwidth/100-45;
		
		lab[0].setBounds(xinit,vstepinit,100,vstep+2);
		//add(lab[0]);
		lab[1].setBounds(xinit,vstepinit+vstep,100,vstep+2);
		//add(lab[1]);
		lab[2].setBounds(xinit,vstepinit+2*vstep,100,vstep+2);
		//add(lab[2]);
		lab[3].setBounds(xinit,vstepinit+3*vstep,100,vstep+2);
		//add(lab[3]);
		lab[4].setBounds(xinit,vstepinit+4*vstep,100,vstep+2);
		//add(lab[4]);
		lab[5].setBounds(xinit,vstepinit+5*vstep,100,vstep+2);
		//add(lab[5]);
		
		vlab[0].setBounds(xinit2,vstepinit,125,vstep+2);
		add(vlab[0]);
		vlab[1].setBounds(xinit2,vstepinit+vstep,125,vstep+2);
		add(vlab[1]);
		vlab[2].setBounds(xinit2,vstepinit+2*vstep,125,vstep+2);
		add(vlab[2]);
		vlab[3].setBounds(xinit2,vstepinit+3*vstep,125,vstep+2);
		add(vlab[3]);
		vlab[4].setBounds(xinit2,vstepinit+4*vstep,125,vstep+2);
		add(vlab[4]);
		vlab[5].setBounds(xinit2,vstepinit+5*vstep,125,vstep+2);
		add(vlab[5]);
		
	}
	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);
		
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	    if(mode==1){
		g.setColor(Color.red);
		g.fillOval(7,5,8,8);
		g.setColor(Color.yellow);
		g.fillOval(7,33,8,8);
		g.setColor(Color.green);
		g.fillOval(7,50,8,8);
                g.setColor(new Color(0,150,255));
		g.fillOval(7,78,8,8);	
    	    }
	    if(mode==2){
		g.setColor(Color.yellow);
		g.fillOval(7,5,8,8);
		g.setColor(Color.red);
		g.fillOval(7,33,8,8);
                g.setColor(new Color(0,150,255));
		g.fillOval(7,50,8,8);
		g.setColor(Color.green);
		g.fillOval(7,78,8,8);
	    }
		g.setColor(Color.black);
		g.drawOval(7,5,8,8);
		g.drawOval(7,33,8,8);
		g.drawOval(7,50,8,8);
		g.drawOval(7,78,8,8);
	    
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
            
            
            Font symbolfont=TheFonts.symbol12;
            FontMetrics fm;
		
            g.setFont(TheFonts.serif12);
                    
            String alpha, Ohm, lambda, infinity, Gamma, plusj, minusj, sign;
		g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393";
                int vstep = 75/5;
		int vstepinit = 12;
		int xinit = 25;
		int xinit2 = xinit+40;
		int xinit3 = xinit2+70;
                            
            //-------------------------------------------------------------------------------------------
		g.setFont(TheFonts.sanSerif11);
		plusj =" + j ";
		minusj=" - j ";
                
                fm = g.getFontMetrics();
             //-----------------------------------------------------------------------
             double tempR = 1.0, tempX = 2.0;
             int stepx = 0;
             
                g.drawString("=",xinit+30,vstepinit);
                g.drawString("=",xinit+30,vstepinit + vstep);
                g.drawString("=",xinit+30,vstepinit+ 2*vstep);
                g.drawString("=",xinit+30,vstepinit + 3*vstep);
                g.drawString("=",xinit+30,vstepinit + 4*vstep);                 
                g.drawString("=",xinit+30,vstepinit + 5*vstep);
	    
             if(mode==1){
                g.setColor(Color.black);
                MaestroG.subscripter(" z","L","",g,11,xinit,vstepinit);
                MaestroG.subscripterSS3(" "+Gamma,"L","",g,11,xinit+1,vstepinit + vstep);
                MaestroG.subscripter(" y","L","",g,11,xinit,vstepinit+ 2*vstep);
                MaestroG.subscripter("z","","(d)",g,11,xinit,vstepinit + 3*vstep);
                MaestroG.subscripterSS3(""+Gamma,"","(d)",g,11,xinit+1,vstepinit + 4*vstep);                 
                MaestroG.subscripter("y","","(d)",g,11,xinit,vstepinit + 5*vstep);  
                
                
                 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;
                }
                
                //-----------------------------------------------------------------------------         
            }
                 
             else if(mode == 2){
                 
                g.setColor(Color.black);
		MaestroG.subscripter(" y","L","",g,11,xinit,vstepinit);
                MaestroG.subscripterSS3(" "+Gamma,"L","",g,11,xinit+1,vstepinit + vstep);
                MaestroG.subscripter(" z","L","",g,11,xinit,vstepinit+ 2*vstep);
                MaestroG.subscripter("y","","(d)",g,11,xinit,vstepinit + 3*vstep);
                MaestroG.subscripterSS3(""+Gamma,"","(d)",g,11,xinit+1,vstepinit + 4*vstep);                 
                MaestroG.subscripter("z","","(d)",g,11,xinit,vstepinit + 5*vstep);    
                
                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;
                }
                //--------------------------------------------------------------------------------------------- 
            }
	}
	public void setZL(Complex ZL){
                this.ZL = 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){
                this.YL = 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){
                this.Zin = 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[5].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));
		
			//if(Complex.Real(Yin)==0.0&&Complex.Imaginary(Yin)<-1.E138)
			//{vlab[4].setText("\u221e");}
		    //else{
		    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){
                this.Yin = 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 SmithProbe2 extends Panel{
Label[] lab;
private int mode;
private static final Color bcolor = new Color(236,236,236);
//private final Color bcolor = new Color(216,216,191);
SmithProbe2(int width, int height){
	super();
	setLayout(null);
        setFont(TheFonts.sanSerif14);
	setBackground(bcolor);
	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(Color.black);
	//g.drawLine(0,height-1,width-1,height-1);
	//g.drawLine(width-1,0,width-1,height-1);
	g.setColor(Color.white);
	g.drawLine(0,0,width-2,0);
	g.drawLine(0,0,0,height-1);
	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 xpos){
	double txpos=xpos;
	if(xpos<=0.5){
	    lab[0].setText("d = "+MaestroA.rounder(txpos,4)+"\u03bb  \u00a6  2\u03b2 d = "+MaestroA.rounder(txpos*4*Math.PI,4)+" rad = "+MaestroA.rounder(txpos*4*180,4)+"\u00ba");
	    lab[1].setText("0.5\u03bb-d = "+MaestroA.rounder((0.5-txpos),4)+"\u03bb  \u00a6  2\u03b2 (0.5\u03bb-d) = "+MaestroA.rounder((0.5-txpos)*4*Math.PI,4)+" rad = "+MaestroA.rounder((0.5-txpos)*4*180,4)+"\u00ba");
	    repaint();
	}
	else{
	    while(txpos>0.5) {txpos-=0.5;}
	    lab[0].setText("d' = "+MaestroA.rounder(txpos,4)+"\u03bb  \u00a6  2\u03b2 d' = "+MaestroA.rounder(txpos*4*Math.PI,4)+" rad = "+MaestroA.rounder(txpos*4*180,4)+"\u00ba");
	    lab[1].setText("0.5\u03bb-d' = "+MaestroA.rounder((0.5-txpos),4)+"\u03bb  \u00a6 2\u03b2 (0.5\u03bb-d') = "+MaestroA.rounder((0.5-txpos)*4*Math.PI,4)+" rad = "+MaestroA.rounder((0.5-txpos)*4*180,4)+"\u00ba");
	    repaint();
	}
	
	/*
	while(txpos>0.5) {txpos-=0.5;}
	lab[0].setText("d = "+MaestroA.rounder(txpos,4)+" \u03bb  \u00a6  2\u03b2d = "+MaestroA.rounder(txpos*4*Math.PI,4)+" rad = "+MaestroA.rounder(txpos*4*180,5)+"\u00ba");
	lab[1].setText("0.5\u03bb-d = "+MaestroA.rounder((0.5-txpos),4)+" \u03bb  \u00a6  2\u03b2(0.5\u03bb-d) = "+MaestroA.rounder((0.5-txpos)*4*Math.PI,4)+" rad = "+MaestroA.rounder((0.5-txpos)*4*180,5)+"\u00ba");
	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);
	if(mode==1){
		g.setColor(bcolor);
		g.fillRect(1,1,25,35);
		FontMetrics fm = g.getFontMetrics();
		double sep, sep2, height;
		
		sep = 5.0; height = 12.0;
                Color newred = new Color(250,0,0);
                MaestroG.drawArcThick(g,5.0,sep,height,40.0,180.0,3,newred);

		sep = 27.0; height = 12.0;
                Color newgreen = new Color(0,220,0);
                MaestroG.drawArcThick(g,5.0,sep,height,40.0,180.0,3,newgreen);
	}
	else if(mode==2){
		int x=15, y=getSize().height/2;
		g.setColor(bcolor);
		g.fillRect(1,1,30,39);
		plot_red_cross(g,x,y);
	}
	else{
		g.setColor(bcolor);
		g.fillRect(1,1,30,35);
	}
	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 SmithControls2 extends Panel{
public Checkbox imp, adm;
public CheckboxGroup cgrid;
public Checkbox swr, swrline;
private static final Color bgcolor = new Color(236,236,236);
//private static final Color bgcolor = new Color(216,216,191);
SmithControls2(){
	//setLayout(new GridLayout(2,2));
	setLayout(null);
	setBackground(bgcolor);
        setFont(TheFonts.sanSerif14);
	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,10,150,20);
	adm.setBounds(180,10,120,20);
	
	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;
