//CircuitCanvas.java
/* A Java class for
 * LineImpedance.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - All Rights Reserved
 */   

import java.awt.*;
import java.awt.event.*;
//import Trans_State;
public class CircuitCanvas extends Panel implements MouseListener{
	private int x[], y[];
	private Trans_State state;
	private Image im;
	private Graphics buf;
	private static final Color bgcolor = Color.gray;
	private static final Color ccolor1 = new Color(50,204,153);
	private static final double epsilon0 = 8.8541878176E-12; // Units: F/m
	private static final double mu0 = 1.25663706144E-6; //      Units H/m
	private static final double light_velocity = Math.sqrt(1.0/(epsilon0*mu0)); //  Units m/s
	private boolean IsFocusOn;
	public CircuitCanvas(Trans_State state){
		super();
		this.state = state;
		setBackground(bgcolor);
		x = new int[10];
		y = new int[5];
		this.addMouseListener(this);
		IsFocusOn = false;
		
	}

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

	public void paint(Graphics g){
		if(im == null){
			im = createImage(getSize().width,getSize().height);
			buf = im.getGraphics();
			drawCircuit(buf);
		}
		else{
			drawCircuit(buf);
		}
		g.drawImage(im,0,0,null);
	}
	
	public void drawCircuit(Graphics g){
		//Draw the background
		
                //g.drawLine(0,getSize().height-1,getSize().width-1,getSize().height-1);
		//g.drawLine(getSize().width-1,0,getSize().width-1,getSize().height-1);
		//g.setColor(Color.white);
		//g.drawLine(0,0,getSize().width-1,0);
		//g.drawLine(0,0,0,getSize().height-1);

		//set the arrays
		x[0] = 20;
		x[4] = getSize().width-20;
		x[2] = x[0] + 50;
		x[3] = x[4] - 20;
		x[1] = (x[0]+x[2])/2;
                x[5] = x[2]; //x[4]/2;  // position of Z(z) value
                x[6] = 3*x[4]/5; // position of ZL and Zo values 

		y[4] = 25;
		y[0] = getSize().height-35;
		y[2] = (y[4]+y[0])/2;
		y[3] = (y[4]+y[2])/2;
		y[1] = (y[0]+y[2])/2;

		//drawGrid(x,y,g);

		//g.clearRect(x[0],y[4]-10,x[4]-x[0],y[0]-y[4]+20);
		g.clearRect(0,0,getSize().width-1,getSize().height-1);
		g.setColor(Color.black);
		g.drawRect(0,0,getSize().width-1,getSize().height-1);
                
		//Reference
		drawRef(g);
		
		//Wires
		drawWire(x[2],y[4],x[4],y[4],1,g);
		drawWire(x[2],y[0],x[4],y[0],1,g);
		//drawWire(x[0],y[4]+1,x[0],y[0]-2,2,g);
		drawWire(x[4],y[4]+1,x[4],y[0]-1,2,g);

		//Horizontal axis
		drawAxis(g);
		
		//ZL
		draw3DRect(x[4],y[2],18,y[1]-y[3],ccolor1,g);
		g.setColor(Color.black);
		MaestroG.subscripter("Z","L","",g,11,x[4]-7,y[2]+2);

		//Connections
		drawConnect(x[2],y[4],g);
		drawConnect(x[2],y[0],g);
		drawConnect(x[3],y[4],g);
		drawConnect(x[3],y[0],g);

		//Draw parameter strings
		//if(IsFocusOn){drawStrings(g);}
		{drawStrings(g);}
		//Draw Stubs
		for(int i =0; i < 3; i++){
		    drawStub(state.stub[i]);
		}
	}

	private void drawGrid(int [] x,int [] y,Graphics g){
		g.setColor(Color.yellow);
		for(int i = 0 ; i< 5; i++){
			g.drawLine(x[0],y[i],x[4],y[i]);
			g.drawLine(x[i],y[0],x[i],y[4]);
		}
	}


	private void draw3DRect(int x, int y, int width, int height, Color c, Graphics g){
		g.setColor(c);
		g.fillRect(x-width/2,y-height/2,width,height);
		g.setColor(Color.white);
		g.drawLine(x-width/2,y-height/2,x+width/2,y-height/2);
		g.drawLine(x-width/2,y-height/2,x-width/2,y+height/2);
		g.setColor(Color.black);
		g.drawLine(x-width/2,y+height/2,x+width/2,y+height/2);
		g.drawLine(x+width/2,y-height/2,x+width/2,y+height/2);
	}
	
	private void drawRect(int x, int y, int width, int height, Color c, Graphics g){
		g.setColor(c);
		g.fillRect(x-width/2,y-height/2,width,height);
	}
	
	private void drawCircle(int x, int y, int radius, Color c, Graphics g){
		g.setColor(c);
		g.fillOval(x-radius,y-radius,2*radius,2*radius);
		g.setColor(Color.white);
		g.drawOval(x-radius,y-radius,2*radius,2*radius);
		g.setColor(Color.black);
		g.drawArc(x-radius-1,y-radius-1,2*radius+2,2*radius+2,30,-120);
	}

	private void drawConnect(int x, int y, Graphics g){
		int tmp1, tmp2;
		tmp1 = 7;
		tmp2 = 5;
		//drawRect(x,y,tmp1,tmp1,Color.white,g);
		//drawRect(x,y,tmp2,tmp2,Color.black,g);
                
                Graphics2D g2d = (Graphics2D)g;
               
                g2d.setStroke(new BasicStroke(1));
        
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
            g2d.setPaint(Color.white);
            g.fillOval(x-tmp1/2,y-tmp1/2,tmp1,tmp1);
            g2d.setPaint(Color.black);
            g.fillOval(x-tmp2/2,y-tmp2/2,tmp2,tmp2);
	
            g2d.setStroke(new BasicStroke(1));
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	}

	private void drawWire(int x1, int y1, int x2, int y2, int mode, Graphics g){
		g.setColor(Color.white);
		g.drawLine(x1,y1,x2,y2);
		if(mode==1){
			g.setColor(Color.black);
			g.drawLine(x1,y1+1,x2,y2+1);
		}
		else if(mode==2){
			g.setColor(Color.black);
			g.drawLine(x1+1,y1,x2+1,y2);
		}
	}

	public void drawStub(Stub stub){
	   if(stub.isEnable()){
		int tax1, tay1, tax2, tay2;
		int tbx1, tby1, tbx2, tby2;
		double maxstublength=0.5;
		double stubangle;
		int stublength;
		stubangle=0.5235;
		stublength=40;
		
		tax1=(int)(x[3]-(stub.getPosition()/state.lineLength)*(x[3]-x[2]));
		tay1=y[4]+2;
		tax2=(int)(tax1-stublength*stub.getLength()/maxstublength*Math.cos(stubangle));
		tay2=(int)(tay1+stublength*stub.getLength()/maxstublength*Math.sin(stubangle));
		
		tbx1=tax1;
		tby1=y[0];
		tbx2=tax2;
		tby2=(int)(tby1+stublength*stub.getLength()/maxstublength*Math.sin(stubangle));

		buf.setColor(Color.white);
		buf.drawLine(tax1,tay1,tax2,tay2);
		buf.drawLine(tbx1,tby1,tbx2,tby2);
		buf.setColor(Color.blue);
		buf.drawLine(tax1,tay1+1,tax2,tay2+1);
		buf.drawLine(tbx1,tby1+1,tbx2,tby2+1);
		if(!stub.isOpen()){
			buf.setColor(Color.white);
			buf.drawLine(tax2,tay2,tbx2,tby2);
			buf.setColor(Color.blue);
			buf.drawLine(tax2+1,tay2,tbx2+1,tby2);
		}
            }
	}

	public void drawRef(Graphics g){
	  if(state.frequency >=state.f_bottom && state.frequency <= state.f_top)
          {
		int tx1, ty1, tx2, ty2;
		if(state.Is_Wavelength){
		    tx1 = (int)(x[3] - (state.xpos/state.lineLength)*(x[3]-x[2]));
		}
		else{
		    tx1 = (int)(x[3] - (state.xpos/state.lineLength)*(x[3]-x[2]));
		}
		ty1 = y[4]-3;
		tx2 = tx1;
		ty2 = y[0]+3;
		g.setColor(Color.green);
                MaestroG.drawArrow(tx1,y[4]+10,5,g);
                MaestroG.drawArrow(tx1,y[0]-9,6,g);
                
		g.drawLine(tx1,ty1,tx2,y[4]);
                g.drawLine(tx1,ty2,tx2,y[0]);
          }
          
	}	
        
	private void drawAxis(Graphics g){
            Font symbolfont=TheFonts.symbol14;
            Font normalfont=TheFonts.sanSerif12;
                
                String alpha, Ohm, lambda, infinity, Gamma, epsilon;
	
		g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\uu0393"; 
		epsilon="\u03b5";
		g.setFont(normalfont);

		int ytmp;
		FontMetrics fm;
		ytmp=y[0] + 15;
		g.setColor(Color.black);
		g.drawLine(x[4],ytmp,x[0],ytmp);
		g.drawLine(x[2],ytmp-4,x[2],ytmp+4);
		g.drawLine(x[3],ytmp-4,x[3],ytmp+4);
		//Arrowhead
		MaestroG.drawArrow(x[0],ytmp,8,g);
		g.setFont(TheFonts.serif12);
		fm = g.getFontMetrics();
		g.setColor(Color.white);
		
                //g.drawString("-l",x[2]-4,ytmp+fm.getHeight());
                // CHANGE ======================================================
                //g.drawString("d",x[2]-4,ytmp+fm.getHeight());
                //==============================================================
                
                // to smooth Italic "l" which loks a bit choppy - but letter washes out on gray background
                //Graphics2D g2d = (Graphics2D)g;
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                //g.drawString("l",x[2]-2,ytmp+fm.getHeight());
                
                // CHANGE ======================================================
                //MaestroG.subscripter("d","","",g,14,x[2]-4,ytmp+5+fm.getHeight());
                //==============================================================
                
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                
                g.setFont(TheFonts.bold12);
		fm = g.getFontMetrics();
                g.drawString("d = 0",x[3]-13,ytmp+fm.getHeight()+2);
		g.setFont(TheFonts.sanSerif12);
		//g.drawString("z",15,ytmp+fm.getHeight()+2);
                
		if(state.frequency >=state.f_bottom && state.frequency <= state.f_top){
                g.setColor(Color.green);
                    //g.drawString("d = "+MaestroA.rounder(state.xpos,8)+" "+lambda,x[2]+50,y[0]+14);
                    if(state.xpos == 0.0){
                        MaestroG.SansNosansSymb2("d = "+MaestroA.rounder(state.xpos,8)+"","",""+lambda,g,11,x[5]+15,y[4]-10);
                    }
                    else{
                        //if(state.xpos <= 20.0){
                        MaestroG.SansNosansSymb2("d = "+MaestroA.rounder(state.xpos,8)+"","",""+lambda,g,11,x[5]+15,y[4]-10);
                        //}
                        //else if(state.xpos > 20.0){
                        //MaestroG.SansNosansSymb2("d = - "+MaestroA.rounder(state.xpos,6)+"","",""+lambda,g,11,x[5]+15,y[4]-10);
                        //}
                        
                    }
                }
                else if(state.frequency < state.f_bottom){
                    g.setColor(Color.yellow);
                    MaestroG.superscripterSanSym2IT("Numerical Range is"," l"," > 10","-4"," ",""+lambda,g,11,x[2]+30,y[0]+13);
                }
                else if(state.frequency > state.f_top){
                    g.setColor(Color.yellow);
                    MaestroG.superscripterSanSym2IT("Numerical Range is"," l", " < 10","5"," ",""+lambda,g,11,x[2]+30,y[0]+13);
                }
                
                g.setColor(Color.white);
	}
	
	private void drawStrings(Graphics g){
                Font symbolfont=TheFonts.symbol14;
                Font normalfont=TheFonts.sanSerif12;
                
                String alpha, Ohm, lambda, infinity, Gamma, epsilon;
	
		g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\uu0393"; 
		epsilon="\u03b5";
		g.setFont(normalfont);
		
            
		g.setColor(Color.white);
		FontMetrics fm = g.getFontMetrics();
		
		MaestroG.subscripter("  Z","0","",g,11,x[6]+15,y[0]-40);
                //MaestroG.subscripter("","","=  "+state.lineZ0+"  "+Ohm,g,11,x[2]+40,y[4]+20);
                
                MaestroG.subscripterSanSym2("","","=  "+state.lineZ0+"  ",Ohm,g,11,x[6]+40,y[0]-40);

                
                MaestroG.subscripter(""+epsilon,"r","",g,11,x[6]+25,y[0]-20);
		//MaestroG.subscripter("","","=  "+state.epsilon_r,g,11,x[2]+40,y[0]-12);
		
                MaestroG.subscripterSymFirst2("","","=  "+MaestroA.rounder(state.epsilon_r,6),g,11,x[6]+40,y[0]-20);
                
                String tempo;  double temp1 = 1.0;
		if(state.lineLength < 1.0){
                    temp1 = MaestroA.rounder(state.lineLength_part1 + state.lineLength_part2,8);
                }
                else if(state.lineLength >= 1.0 && state.lineLength < 10.0){
                    temp1 = MaestroA.rounder(state.lineLength_part1 + state.lineLength_part2,7);
                }
                else if(state.lineLength >= 10.0 && state.lineLength < 100.0){
                    temp1 = MaestroA.rounder(state.lineLength_part1 + state.lineLength_part2,6);
                }
                else if(state.lineLength >= 100.0 && state.lineLength < 1000.0){
                    temp1 = MaestroA.rounder(state.lineLength_part1 + state.lineLength_part2,5);
                }
                else if(state.lineLength >= 1000.0 && state.lineLength < 10000.0){
                    temp1 = MaestroA.rounder(state.lineLength_part1 + state.lineLength_part2,4);
                }
                else if(state.lineLength >= 10000.0 && state.lineLength <= 100000.0){
                    temp1 = MaestroA.rounder(state.lineLength_part1 + state.lineLength_part2,3);
                }
                
                double testpos, rounded;
                rounded = MaestroA.rounder(state.xpos,8);
                testpos = Math.abs((temp1-rounded));
                //System.out.println("rounded = "+rounded+"  temp1 = "+temp1+"  testpos = "+testpos);
                
                //if(testpos > 1.0e-10){
                if(rounded < temp1){
		    if(Complex.Real(state.Zin)==0.0&&Complex.Imaginary(state.Zin)<=-2.0E140){ 
			MaestroG.subscripter("Z(d)","","",g,11,x[5]+15,y[2]-10+15);
                        //MaestroG.subscripter("","","=  "+infinity,g,11,x[2]+40,y[2]-10+15);
                        MaestroG.subscripterInfinityTwo("","","=   ","", "     (Open Circuit)",g,1,x[5]+40,y[2]-10+15);
                    }
		    else{
		        MaestroG.subscripter("Z(d)","","",g,11,x[5]+15,y[2]-10+15);
                        //MaestroG.subscripter("","","=  "+state.Zin.toString(Complex.CARTESIAN,3)+"  "+Ohm,g,11,x[2]+40,y[2]-10+15);
                        MaestroG.subscripterSanSym2("","","=  "+state.Zin.toString(Complex.CARTESIAN,3)+"   ",Ohm,g,11,x[5]+40,y[2]-10+15);
		    }
		}
		else{
		    if(Complex.Real(state.Zin)==0.0&&Complex.Imaginary(state.Zin)<=-2.0E140){ 
			MaestroG.subscripter("  Z","in","",g,11,x[5]+15,y[2]-10+15);
                        //MaestroG.subscripter("","","=  "+infinity,g,11,x[2]+40,y[2]-10+15);
                        MaestroG.subscripterInfinityTwo("","","=   ","", "     (Open Circuit)",g,11,x[5]+40,y[2]-10+15);
		    }
		    else{
			MaestroG.subscripter("  Z","in","",g,11,x[5]+15,y[2]-10+15);
                        //MaestroG.subscripter("","","=  "+state.Zin.toString(Complex.CARTESIAN,3)+"  "+Ohm,g,11,x[2]+40,y[2]-10+15);
                        MaestroG.subscripterSanSym2("","","=  "+state.Zin.toString(Complex.CARTESIAN,3)+"   ",Ohm,g,11,x[5]+40,y[2]-10+15);
                    }
		}
		
		g.setFont(TheFonts.sanSerif11);
		
                if(state.frequency >=state.f_bottom && state.frequency <= state.f_top)
          {
                	
		tempo = new String("   = "+temp1+"  "+lambda);
                //MaestroG.subscripterSanSym2("   = "+temp1,"","",""+lambda,g,11,x[2]-3,getSize().height-3);
		MaestroG.SansNosansSymb2(" d = "+temp1,"",""+lambda,g,11,x[2]-3,getSize().height-3);
				
                g.setFont(TheFonts.sanSerif11);
		
                double testlength=state.lineLength_meters;
		
                //Changes for general lengths units
                	
		if(testlength < 1.0E-9 && testlength >0.0 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1.0E9,8)
			+" nm",x[2]+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength < 1.0E-6 && testlength >= 1.0E-9 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1.0E9,6)
			+" nm",x[2]+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength < 1.0E-3 && testlength >= 1.0E-6 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1.0E6,6)
			+" \u00b5m",x[2]+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength < 1.0 && testlength >= 1.0E-3 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1000,6)
			+" mm",x[2]+fm.stringWidth(tempo),getSize().height-3);
		}
		else if((testlength < 1000.0 && testlength >= 1.0 ) || testlength == 0.0){
		    g.drawString(" = "+	MaestroA.rounder(testlength,6)
			+" m",x[2]+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength < 1.0E6 && testlength >=1.0E3){
		    g.drawString(" = "+	MaestroA.rounder(testlength/1000,6)
			+" km",x[2]+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength < 1.0E9 && testlength >=1.0E6){
		    //g.drawString(" = "+	MaestroA.rounder(testlength/1.0E6,6)
			//+" Mm",x[2]+fm.stringWidth(tempo),getSize().height-3);
                    MaestroG.superscripter(" = "+	MaestroA.rounder(testlength/1.0E6,6)
			+" x 10","3"," km",g,11,x[2]+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength >=1.0E9){
		    //g.drawString(" = "+	MaestroA.rounder(testlength/1.0E9,6)
                    //	+" Gm",x[2]+fm.stringWidth(tempo),getSize().height-3);
                    MaestroG.superscripter(" = "+	MaestroA.rounder(testlength/1.0E9,6)
			+" x 10","6"," km",g,11,x[2]+fm.stringWidth(tempo),getSize().height-3);
		}
          }
          else if(state.frequency < state.f_bottom){
                    g.setColor(Color.yellow);
                    //MaestroG.superscripterSanSym("Increase Length or Frequency","","","",g,11,x[2]+30,getSize().height-3);
                    MaestroG.superscripter("Increase Length or Frequency","","",g,11,x[2]+30,getSize().height-3);
          }
          else if(state.frequency > state.f_top){
                    g.setColor(Color.yellow);
                    MaestroG.superscripter("Decrease Length or Frequency","","",g,11,x[2]+30,getSize().height-3);
          }
                g.setColor(Color.white);
                
          //  Changes finish here	
		
		//if(Complex.Real(state.ZL)==0.0&&Complex.Imaginary(state.ZL)<=-2.0E140){ 
                  //  MaestroG.subscripter("  Z","L","",g,11,x[2]+15,y[4]-10);
                  //  MaestroG.subscripter("","","=  "+infinity+"     (Open Circuit)",g,11,x[2]+40,y[4]-10);
                //}
		//else if(Complex.Real(state.ZL)==0.0&&Complex.Imaginary(state.ZL)==0.0){ 
                  //  MaestroG.subscripter("  Z","L","",g,11,x[2]+15,y[4]-10);
                  //  MaestroG.subscripter("","","=  0    (Short Circuit)",g,11,x[2]+40,y[4]-10);
		//}
		//else{
                  //  MaestroG.subscripter("  Z","L","",g,11,x[2]+15,y[4]-10);
                  //  MaestroG.subscripter("","","=  "+state.ZL.toString(Complex.CARTESIAN,3)+"  "+Ohm,g,11,x[2]+40,y[4]-10);
                //}
                
                if(Complex.Real(state.YL)==0.0&&Complex.Imaginary(state.YL)==0.0){ 
                    MaestroG.subscripter("  Z","L","",g,11,x[6]+15,y[4]-10);
		    MaestroG.subscripterInfinityTwo("","","=   ","", "     (Open Circuit)",g,11,x[6]+40,y[4]-10);
	        }
		else if(Complex.Real(state.ZL)==0.0&&Complex.Imaginary(state.ZL)==0.0){ 
                    MaestroG.subscripter("  Z","L","",g,11,x[6]+15,y[4]-10);
                    MaestroG.subscripter("","","=  0    (Short Circuit)",g,11,x[6]+40,y[4]-10);
		}
		else{
                    MaestroG.subscripter("  Z","L","",g,11,x[6]+15,y[4]-10);
                    MaestroG.subscripterSanSym2("","","=  "+state.ZL.toString(Complex.CARTESIAN,3)+"   ",Ohm,g,11,x[6]+40,y[4]-10);
		}

		
	}
	
	public void mouseClicked(MouseEvent evt){
	
	}
	public void mouseEntered(MouseEvent evt){
	    IsFocusOn = true;
	    repaint();
	}
	public void mouseExited(MouseEvent evt){
	    IsFocusOn = false;
	    repaint();
    	}
	public void mousePressed(MouseEvent evt){
	
	}
	public void mouseReleased(MouseEvent evt){
	
	}
    
}//End of CircuitCanvs

