//CircuitCanvas.java

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

//import Trans_State;
public class CircuitCanvas extends Panel implements MouseListener, ActionListener{
	private int x[], y[];
	private Trans_State state;
	private Image im;
	private Graphics buf;
        public Checkbox c0, c1, c2, c3, c4, c5, c6, c7, c8, c9;
        protected CheckboxGroup cgrp;
    
	private static final Color bgcolor = Color.gray;
	private static final Color ccolor1 = new Color(50,204,153);
	private static final Font labfont = new Font("SanSerif",Font.BOLD,10);
        private static final Font buttonfont = new Font("SanSerif",Font.PLAIN,10);
        
	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, IsTopoOn;
        public Button details;
        public int colorFlag[];
        
	public CircuitCanvas(Trans_State state){
		super();
                setLayout(null);
		this.state = state;
		setBackground(bgcolor);
                details = new Button("Details");
                //add(details);
                details.setBackground(Color.white);
                //details.setBounds(750,2,40,16);
                details.setFont(buttonfont);
                colorFlag = new int[3];
                
                colorFlag[0]=0;
                colorFlag[1]=1;
                colorFlag[2]=2;
                
                cgrp = new CheckboxGroup();
                c0 = new Checkbox("",cgrp,false);
                c1 = new Checkbox("",cgrp,false);
                c2 = new Checkbox("",cgrp,false);
                c3 = new Checkbox("",cgrp,false);
                c4 = new Checkbox("",cgrp,false);
                c5 = new Checkbox("",cgrp,false);
                c6 = new Checkbox("",cgrp,false);
                c7 = new Checkbox("",cgrp,false);
                c8 = new Checkbox("",cgrp,false);
                c9 = new Checkbox("no shunt",cgrp,false);
                
                add(c0);
                add(c1);
                add(c2);
                add(c3);
                add(c4);
                add(c5);
                add(c6);
                add(c7);
                add(c8);
                add(c9);
                
		x = new int[5];
		y = new int[5];
		this.addMouseListener(this);
		IsFocusOn = false;
                
                
                details.addActionListener(this);
	}

	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){
		
		//set the arrays
		x[0] = state.s20;
		x[4] = getSize().width-state.s20;
		x[2] = x[0] + state.s50;
		x[3] = x[4] - state.s20;
		x[1] = (x[0]+x[2])/2;

		y[4] = state.s25;
		y[0] = getSize().height-state.s35;
		y[2] = (y[4]+y[0])/2;
		y[3] = (y[4]+y[2])/2;
		y[1] = (y[0]+y[2])/2;
                    
                // clear to allow for updates - IMPORTANT!!!!
		g.clearRect(0,0,getSize().width-1,getSize().height-1);
		
                //Draw the background
		g.setColor(Color.black);
                g.drawRect(0,0,getSize().width-1,getSize().height-1);
		
                //Wires
		drawWire(x[0],y[4],x[4],y[4],1,g);
		drawWire(x[0],y[0],x[4],y[0],1,g);
		drawWire(x[0],y[4] + state.s1,x[0],y[0] - state.s2,2,g);
		drawWire(x[4],y[4] + state.s1,x[4],y[0] - state.s1,2,g);

		//Horizontal axis
		drawAxis(g);
		
                //Draw parameter strings
                drawStrings(g);
                
		draw3DRect(x[4],y[2],state.s24,y[1]-y[3],ccolor1,g);
		g.setColor(Color.black);
                //ZL
		MaestroG.subscripter("Z","L","",g,state.font11,x[4] - state.s5,y[2] + state.s2);
		//Zg
		draw3DRect(x[1],y[4],x[2]-x[0]-state.s16,state.s22,ccolor1,g);	
		g.setColor(Color.black);
		MaestroG.subscripter("Z","g","",g,state.font11,x[1]-state.s5,y[4]+state.s2);
		//Vg
		drawCircle(x[0],y[2],state.s12,ccolor1,g);
		g.setColor(Color.black);
		MaestroG.subscripter("V","g","",g,state.font11,x[0]-state.s6,y[2]+state.s1);
                
		//g.setFont(new Font("SanSerif",Font.BOLD,16));
		//g.drawString("~",x[0]-5,y[2]+5);
		
                //Reference
		drawRef(g);
		
		//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);                
	}

	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){
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		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-state.s1,y-radius-state.s1,2*radius+state.s2,2*radius+state.s2,30,-120);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	}

	private void drawConnect(int x, int y, Graphics g){
            int tmp1, tmp2;
            tmp1 = state.s7;
            tmp2 = state.s5;

            Graphics2D g2d = (Graphics2D)g;

            g2d.setStroke(new BasicStroke(1));
        
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
            g2d.setPaint(Color.black);
            g.fillOval(x-tmp1/2,y-tmp1/2,tmp1,tmp1);
            
            g2d.setPaint(Color.white);
            g.drawOval(x-tmp1/2,y-tmp1/2,tmp1,tmp1);
            
            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,int i,Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            Color stubColor = Color.blue;
		
		if(colorFlag[i] == 0){stubColor = Color.blue;}
		if(colorFlag[i] == 1){stubColor = Color.black;}
		if(colorFlag[i] == 2){stubColor = Color.red;}
            
            
	   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];
		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(stubColor);
		buf.drawLine(tax1,tay1+state.s1,tax2,tay2+state.s1);
		buf.drawLine(tbx1,tby1+state.s1,tbx2,tby2+state.s1);
		if(!stub.isOpen()){
			buf.setColor(Color.white);
			buf.drawLine(tax2,tay2,tbx2,tby2);
			buf.setColor(stubColor);
			buf.drawLine(tax2+state.s1,tay2,tbx2+state.s1,tby2);
		}
            }
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	}

	public void drawRef(Graphics g){
                Font symbolfont=new Font("Symbol",Font.PLAIN,state.font14);
                Font normalfont=new Font("SanSerif",Font.PLAIN,state.font12);
                
                String Ohm, lambda, infinity, Gamma, epsilon;
	
		g.setFont(symbolfont);
		
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393"; 
		epsilon="\u03b5";
		g.setFont(normalfont);
                
	  if(state.xpos <=state.lineLength && state.xpos >= 0.0){
		int tx1, ty1, tx2, ty2;
                int tline0, tline1, tline2, tline3, tline4, 
                    tline5, tline6, tline7, tline8, tline9;
		tx1 = (int)(x[3] - (state.xpos/state.lineLength)*(x[3]-x[2]));
		ty1 = y[4]-2;
		tx2 = tx1;
		ty2 = y[0]+2;
                
                tline0 = (int)(x[3] - (state.locations[0]/state.lineLength)*(x[3]-x[2]));
                tline1 = (int)(x[3] - (state.locations[1]/state.lineLength)*(x[3]-x[2]));
		tline2 = (int)(x[3] - (state.locations[2]/state.lineLength)*(x[3]-x[2]));
		tline3 = (int)(x[3] - (state.locations[3]/state.lineLength)*(x[3]-x[2]));
		tline4 = (int)(x[3] - (state.locations[4]/state.lineLength)*(x[3]-x[2]));
                tline5 = (int)(x[3] - (state.locations[5]/state.lineLength)*(x[3]-x[2]));
		tline6 = (int)(x[3] - (state.locations[6]/state.lineLength)*(x[3]-x[2]));
		tline7 = (int)(x[3] - (state.locations[7]/state.lineLength)*(x[3]-x[2]));
		tline8 = (int)(x[3] - (state.locations[8]/state.lineLength)*(x[3]-x[2]));
                tline9 = (int)(x[3] - (state.locations[9]/state.lineLength)*(x[3]-x[2]));
                
                Color very_lightGray = new Color(230,230,230);
                g.setColor(Color.green);
		//MaestroG.drawArrow(tx1,ty1+state.s14,5,g);
                //MaestroG.drawArrow(tx1,ty2-state.s14,6,g);
                MaestroG.drawArrowScaled(tx1,ty1+state.s14,1,state.sfactor,g);
                MaestroG.drawArrowScaled(tx1,ty2-state.s14,2,state.sfactor,g);
                
		g.drawLine(tx1,ty1+state.s14,tx1,ty1+state.s19);
                g.drawLine(tx1,ty2-state.s14,tx1,ty2-state.s19);
                
                g.setColor(very_lightGray);
                if(state.ShuntPlace == 1){
                    drawZS(g, very_lightGray, tline9);
                }
                
                g.setColor(Color.yellow);
                if(state.ShuntPlace == 2){
                    drawZS(g, Color.yellow, tline8);
                }
                
                g.setColor(Color.pink);
                if(state.ShuntPlace == 3){
                    drawZS(g, Color.pink, tline7);
                }
                
                g.setColor(Color.cyan);
                if(state.ShuntPlace == 4){
                    drawZS(g, Color.cyan, tline6);
                }
                
                g.setColor(Color.orange);
                if(state.ShuntPlace == 5){
                    drawZS(g, Color.orange, tline5);
                }
                
                g.setColor(very_lightGray);
                if(state.ShuntPlace == 6){
                    drawZS(g, very_lightGray, tline4);
                }
                
                g.setColor(Color.yellow);
                if(state.ShuntPlace == 7){
                    drawZS(g, Color.yellow, tline3);
                }
                
                g.setColor(Color.pink);
                if(state.ShuntPlace == 8){
                    drawZS(g, Color.pink, tline2);
                }
                
                // DRAW TRANSFORMER HERE (WITH TRANSPARENCY)
                
                int rule; float alpha;
                rule = AlphaComposite.SRC_OVER;
                alpha = 0.2f;
        
                Graphics2D g2d = (Graphics2D)g;
                
                // arrows with lambda/4 label
                g.setColor(Color.yellow);
                
                // dash lines - boundaries of transformer
                g.setColor(Color.white);
                float[] dashPattern = {3,3};
                g2d.setStroke(new BasicStroke(1.0F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0F,dashPattern,0));
                g2d.setStroke(new BasicStroke(1.0F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));
                //MaestroG.drawArrow(tline0, y[0]+state.s10, 5, g);
                MaestroG.drawArrowScaled(tline0, y[0]+state.s10, 1, state.sfactor,g);
                
                g.drawLine(tline0, y[0], tline0, y[0]+state.s15);
                
                g.setColor(Color.white);
                MaestroG.subscripterSanSym2("Z","0"," = "+MaestroA.rounder(state.ZIMP[0],4)+" ",Ohm, g, state.font13, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2-state.s15);
                
                int yshifto = state.s10;
                double Sensitive;
                Sensitive = Complex.Magnitude(state.Gammain);
                if(Sensitive > 1.0){Sensitive = 1.0;} 
                
                double VSWRin;
                
                if(Sensitive == 1.0){
                    VSWRin = 1.0E130;
                }
                else{
                    VSWRin = (1.0+Sensitive)/(1-Sensitive);
                }
                
                Color colorwarn = Color.white;
                if(VSWRin <= state.targetVSWR){
                    colorwarn = Color.yellow;
                }
                else{
                    colorwarn = Color.orange;
                }
                
                if(VSWRin >= 1.0E10){
                    g.setColor(colorwarn);
                    MaestroG.subscripter("SWR = \u221E","","", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin >= 1.0E9 && Math.abs(state.YS.Imaginary()) < 1.0E10){
                    g.setColor(colorwarn);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e9,3)+" x 10 ","9","", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin >= 1.0E8 && Math.abs(state.YS.Imaginary()) < 1.0E9){
                    g.setColor(colorwarn);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e8,3)+" x 10 ","8","", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin >= 1.0E7 && Math.abs(state.YS.Imaginary()) < 1.0E8){
                    g.setColor(colorwarn);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e7,3)+" x 10 ","7","", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin >= 1.0E6 && Math.abs(state.YS.Imaginary()) < 1.0E7){
                    g.setColor(colorwarn);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e6,3)+" x 10 ","6","", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin >= 1.0E5 && Math.abs(state.YS.Imaginary()) < 1.0E6){
                    g.setColor(colorwarn);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e5,3)+" x 10 ","5","", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin >= 1.0E4 && Math.abs(state.YS.Imaginary()) < 1.0E5){
                    g.setColor(colorwarn);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e4,3)+" x 10 ","4","", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin >= 1000.0 && Math.abs(state.YS.Imaginary()) < 1.0E4){
                    g.setColor(colorwarn);
                    MaestroG.subscripter("SWR","", " = "+(int)VSWRin+" ", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin > 1.5 && VSWRin <1000.0){
                    g.setColor(colorwarn);
                    MaestroG.subscripter("SWR","", " = "+MaestroA.rounder(VSWRin,3)+" ", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.5 && VSWRin > 1.2){
                    g.setColor(colorwarn);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,3)+" ", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.2 && VSWRin > 1.1){
                    g.setColor(colorwarn);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,4)+" ", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.1 && VSWRin > 1.001){
                    g.setColor(colorwarn);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,4)+" ", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.001 && VSWRin > 1.000001){
                    g.setColor(colorwarn);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,6)+" ", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.000001){
                    g.setColor(colorwarn);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,9)+" ", g, state.font14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                
                //------------------------------------------------------------------------------------------
                
                double tempR = state.Gammain.Magnitude();
                int xinit2 = tline2+(tline1-tline2)/2;
                int yinit2 = y[4]+(y[0]-y[4])/2+state.s30;
                        
                if(tempR < 0.2){
		    if(tempR >= 1.0E-3){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR,6),"","",g,state.font14,xinit2,yinit2);
		    }
		    else if(tempR < 1.0E-3 && tempR >= 1.0E-4){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR*1.0E4,4)+" x 10","-4","",g,state.font14,xinit2,yinit2);
		    }
                    else if(tempR < 1.0E-4 && tempR >= 1.0E-5){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR*1.0E5,4)+" x 10","-5","",g,state.font14,xinit2,yinit2);
		    }
                    else if(tempR < 1.0E-5 && tempR >= 1.0E-6){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR*1.0E6,4)+" x 10","-6","",g,state.font14,xinit2,yinit2);
		    }
		    else if(tempR < 1.0E-6 && tempR >= 1.0E-7){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR*1.0E9,4)+" x 10","-7","",g,state.font14,xinit2,yinit2);
		    }
                    else if(tempR < 1.0E-7 && tempR >= 1.0E-8){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR*1.0E9,4)+" x 10","-8","",g,state.font14,xinit2,yinit2);
		    }
                    else if(tempR < 1.0E-8 && tempR >= 1.0E-9){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR*1.0E9,4)+" x 10","-9","",g,state.font14,xinit2,yinit2);
		    }
		    else if(tempR < 1.0E-9 && tempR >= 1.0E-10){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR*1.0E12,4)+" x 10","-10","",g,state.font14,xinit2,yinit2);
		    }
		    else if(tempR < 1.0E-10 && tempR >= 1.0E-11){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR*1.0E15,4)+" x 10","-11","",g,state.font14,xinit2,yinit2);
		    }
		    else if(tempR < 1.0E-11 && tempR >= 1.0E-12){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR*1.0E18,4)+" x 10","-12","",g,state.font14,xinit2,yinit2);
		    }
                    else if(tempR < 1.0E-12 && tempR != 0.0){
			MaestroG.superscripter2B("| \uu0393 | ","= 0.0","","",g,state.font14,xinit2,yinit2);
		    }
                }
                else{
			MaestroG.superscripter2("| \uu0393 | ","= "+MaestroA.rounder(tempR,6),"","",g,state.font14,xinit2,yinit2);
                }
                
                g.setColor(Color.green);
                if(VSWRin <= state.targetVSWR){
                    MaestroG.subscripterB("SWR < "+state.targetVSWR,"","  -  MATCHING TARGET ACHIEVED", g, state.font12, xinit2-state.s60, y[0]+state.s13);
                }
                //------------------------------------------------------------------------------------------
                if(state.ShuntPlace == 9){
                    drawZS(g, Color.cyan, tline1);
                }
                
                g.setColor(Color.orange);
                if(state.ShuntPlace == 10){
                    drawZS(g, Color.orange, tline0);
                }
         
                if((state.is_Load_Ztype && (state.ZL.Real() == 0.0 && state.ZL.Imaginary() == 0.0))){
                    g.setColor(Color.black);
                    g.fillRect(state.s440, state.s30, state.s300, state.s70);
                    g.setColor(Color.yellow);
                    g.setColor(Color.yellow);
                    MaestroG.subscripter("IMPEDANCE MATCH IS NOT POSSIBLE","","",g,state.font14,state.s450,state.s50);
                    MaestroG.subscripter("IF LOAD IS A SHORT CIRCUIT","","",g,state.font14,state.s450,state.s70);
                    g.setColor(Color.pink);
                    MaestroG.subscripterB("PLEASE, SELECT A NEW LOAD","","",g,state.font14,state.s450,state.s95);
                }
                else if(state.is_Load_Ztype && (state.ZL.Real() == 0.0 && state.ZL.Imaginary() != 0.0)){
                    g.setColor(Color.black);
                    g.fillRect(state.s440, state.s30, state.s300, state.s70);
                    g.setColor(Color.yellow);
                    g.setColor(Color.green);
                    MaestroG.subscripter("IMPEDANCE MATCH IS NOT POSSIBLE","","",g,state.font14,state.s450,state.s50);
                    MaestroG.subscripter("IF LOAD IS A PURE REACTANCE","","",g,state.font14,state.s450,state.s70);
                    g.setColor(Color.pink);
                    MaestroG.subscripterB("PLEASE, SELECT A NEW LOAD","","",g,state.font14,state.s450,state.s95);
                }
                else if(!state.is_Load_Ztype && (state.YL.Real() == 0.0 && state.YL.Imaginary() != 0.0)){
                    g.setColor(Color.black);
                    g.fillRect(state.s440, state.s30, state.s300, state.s70);
                    g.setColor(Color.yellow);
                    g.setColor(Color.green);
                    MaestroG.subscripter("IMPEDANCE MATCH IS NOT POSSIBLE","","",g,state.font14,state.s450,state.s50);
                    MaestroG.subscripter("IF LOAD IS A PURE SUSCEPTANCE","","",g,state.font14,state.s450,state.s70);
                    g.setColor(Color.pink);
                    MaestroG.subscripterB("PLEASE, SELECT A NEW LOAD","","",g,state.font14,state.s450,state.s95);
                }
                else if(!state.is_Load_Ztype && (state.YL.Real() == 0.0 && state.YL.Imaginary() == 0.0)){
                    g.setColor(Color.black);
                    g.fillRect(state.s440, state.s30, state.s300, state.s70);
                    g.setColor(Color.yellow);
                    g.setColor(Color.cyan);
                    MaestroG.subscripter("IMPEDANCE MATCH IS NOT POSSIBLE","","",g,state.font14,state.s450,state.s50);
                    MaestroG.subscripter("IF LOAD IS AN OPEN CIRCUIT","","",g,state.font14,state.s450,state.s70);
                    g.setColor(Color.pink);
                    MaestroG.subscripterB("PLEASE, SELECT A NEW LOAD","","",g,state.font14,state.s450,state.s95);
                }
          }
	}
        
	private void drawZS(Graphics g, Color colore, int tx){    
                    int tax1, tax2, tay1, tay2, tbx1, tbx2, tby1, tby2; 
                    int MaxL = state.s80;
                    int GoL;
                    
                    GoL = (int)(MaxL*(2.0 * state.Stub_Length));
                    tax1 = tx;
		    tay1 = y[4];
		    tax2 = tx - GoL;
		    tay2 = y[4] + GoL/4;
		
		    tbx1 = tax1;
		    tby1 = y[0];
		    tbx2 = tax2;
		    tby2 = y[0] + GoL/4;

		    g.setColor(Color.white);
		    g.drawLine(tax1,tay1,tax2,tay2);
		    g.drawLine(tbx1,tby1,tbx2,tby2);
                    if(state.StubIsShort){g.drawLine(tbx2,tay2,tbx2,tby2);}
                    
		    g.setColor(Color.blue);
		    g.drawLine(tax1,tay1+state.s1,tax2,tay2+state.s1);
		    g.drawLine(tbx1,tby1+state.s1,tbx2,tby2+state.s1);
                    if(state.StubIsShort){g.drawLine(tbx2+state.s1,tay2+state.s1,tbx2+state.s1,tby2+state.s1);}
        }
        
	private void drawAxis(Graphics g){
                Font normalfont=new Font("SanSerif",Font.PLAIN,state.font12);
                String alpha, Ohm, lambda, infinity, Gamma, epsilon;
	
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393"; 
		epsilon="\u03b5";
		g.setFont(normalfont);
                
		int ytmp;
		FontMetrics fm;
		ytmp=y[0] + state.s15;
		g.setColor(Color.black);
		g.drawLine(x[4],ytmp,x[0],ytmp);
		g.drawLine(x[2],ytmp-state.s4,x[2],ytmp+state.s4);
		g.drawLine(x[3],ytmp-state.s4,x[3],ytmp+state.s4);
		//Arrowhead
		//MaestroG.drawArrow(x[0],ytmp,4,g);
                MaestroG.drawArrowScaled(x[0],ytmp,4,state.sfactor,g);
                
		g.setColor(Color.white);
		
                g.setFont(new Font("Serif",Font.ITALIC,state.font13));
                fm = g.getFontMetrics();
                g.drawString("l",x[2]-state.s6,ytmp+fm.getHeight());
                
                g.setFont(new Font("SanSerif",Font.BOLD,state.font12));
		fm = g.getFontMetrics();
                g.drawString("d = 0",x[3]-state.s12,ytmp+fm.getHeight()+state.s1);
		
                g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		fm = g.getFontMetrics();
                g.setColor(Color.green);
                MaestroG.SansNosansSymb2("d = "+MaestroA.rounder(state.xpos,8)+""," ",""+lambda,
                        g,state.font11,x[4]/2-state.s30,y[4]-state.s8);
        }
	
	private void drawStrings(Graphics g){
		
                String alpha, Ohm, lambda, infinity, Gamma, epsilon;
                Font symbolfont = new Font("Symbol",Font.PLAIN,state.font12);
                Font normalfont = new Font("Serif",Font.PLAIN,state.font11);
                FontMetrics fm = g.getFontMetrics();
						
		g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393";
                epsilon="\u03b5";
                
                g.setColor(Color.white);
                
                int xZL = x[4] - state.s200;
                int yf = getSize().height - state.s6;
                
                if(Complex.Real(state.YL)==0.0&&Complex.Imaginary(state.YL)==0.0){ 
                    MaestroG.subscripterInfinityTwo("Z","L"," =  ","", "     (Open Circuit)",g,state.font12,xZL,y[4]-state.s8);
	        }
		else if(Complex.Real(state.ZL)==0.0&&Complex.Imaginary(state.ZL)==0.0){ 
                    MaestroG.subscripter("Z","L"," = 0    (Short Circuit)",g,state.font12,xZL,y[4]-state.s8);
		}
		else{
                    MaestroG.subscripterSanSym2("Z","L"," = "+state.ZL.toString(Complex.CARTESIAN,3)+" ",Ohm,g,state.font12,xZL,y[4]-state.s8);
		}
                
                //if(state.ShuntPlace > 0)
                {   
                    if(state.StubIsShort){
                        if(state.Stub_Length == 0.0 || state.Stub_Length == 0.5){
                            MaestroG.subscripterInfinityTwo("Y","S"," =  ","", " [ S ]  (Short Circuit)",g,state.font11,xZL,yf);
                        }
                        else if(state.Stub_Length == 0.25){
                            MaestroG.subscripter("Y","S"," =  0.0 [ S ]  (Open Circuit)",g,state.font11,xZL,yf);
                        }
                        else{
                            MaestroG.subscripter("Y","S"," = "+state.YS.toString(Complex.CARTESIAN,4)+" [ S ]",g,state.font11,xZL,yf);
                        }
                    }
                    else{
                         if(state.Stub_Length == 0.0 || state.Stub_Length == 0.5){
                            MaestroG.subscripter("Y","S"," =  0.0 [ S ]  (Open Circuit)",g,state.font11,xZL,yf);
                        }
                        else if(state.Stub_Length == 0.25){
                            MaestroG.subscripterInfinityTwo("Y","S"," =  ","", " [ S ]  (Short Circuit)",g,state.font11,xZL,yf);
                        }
                        else{
                            MaestroG.subscripter("Y","S"," = "+state.YS.toString(Complex.CARTESIAN,4)+" [ S ]",g,state.font11,xZL,yf);
                        }       
                    }
                    /*
                    if(Complex.Real(state.YS)==0.0&&Complex.Imaginary(state.YS)==0.0){ 
                        MaestroG.subscripter("Y","S"," =  0.0 S  (Open Circuit)",g,state.font11,xZL,yf);
                    }
                    else if(Complex.Real(state.ZS)==0.0&&Complex.Imaginary(state.ZS)==0.0){ 
                        MaestroG.subscripterInfinityTwo("Y","S"," =  ","", " S  (Short Circuit)",g,state.font11,xZL,yf);
                    }
                    else{
                        MaestroG.subscripter("Y","S"," = "+state.YS.toString(Complex.CARTESIAN,3)+" S",g,state.font11,xZL,yf);
                    }
                    */
                }
                
                MaestroG.subscripterSanSym2("Z","g"," = "+state.generator.getZg().toString()+" ",Ohm,g,state.font12,x[2]+state.s5,y[4]-state.s8);
		MaestroG.subscripter("V","g"," = "+state.generator.getVg().toString()+"  V",g,state.font12,x[2]-state.s30,y[4]+state.s45);
                fm = g.getFontMetrics();
                int goup = fm.getHeight();
                MaestroG.subscripter("~","","",g,state.font13,x[2]-state.s30,y[4]+state.s45-90*goup/200);
                
                g.setColor(Color.yellow);
                int xf = getSize().width/3 + state.s20;
                if(state.frequency_ref < 1.0E3){
                    MaestroG.subscripterSymFirst2I("f","o"," = "+MaestroA.rounder(state.frequency_ref,6)+"  Hz",g,state.font12,xf,yf);
                }
                else if(state.frequency_ref >= 1.0E3 && state.frequency_ref < 1.0E6){
                    MaestroG.subscripterSymFirst2I("f","o"," = "+MaestroA.rounder(state.frequency_ref/1.0E3,6)+"  kHz",g,state.font12,xf,yf);    
                }
                else if(state.frequency_ref >= 1.0E6 && state.frequency_ref < 1.0E9){
                    MaestroG.subscripterSymFirst2I("f","o"," = "+MaestroA.rounder(state.frequency_ref/1.0E6,6)+"  MHz",g,state.font12,xf,yf);    
                }
                else if(state.frequency_ref >= 1.0E9 && state.frequency_ref < 1.0E12){
                    MaestroG.subscripterSymFirst2I("f","o"," = "+MaestroA.rounder(state.frequency_ref/1.0E9,6)+"  GHz",g,state.font12,xf,yf);    
                }
                else if(state.frequency_ref >= 1.0E12 && state.frequency_ref < 1.0E15){
                    MaestroG.subscripterSymFirst2I("f","o"," = "+MaestroA.rounder(state.frequency_ref/1.0E12,6)+"  THz",g,state.font12,xf,yf);    
                }
                else if(state.frequency_ref >= 1.0E15){
                    MaestroG.subscripterSymFirst2I("f","o"," = "+MaestroA.rounder(state.frequency_ref/1.0E15,6)+"  PHz",g,state.font12,xf,yf);    
                }
                
                g.setColor(Color.cyan);
                xf = getSize().width/3 + state.s170;
                yf = getSize().height - state.s5;
                
                if(state.frequency < 1.0E3){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency,6)+"  Hz",g,state.font12,xf,yf);
                }
                else if(state.frequency >= 1.0E3 && state.frequency < 1.0E6){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E3,6)+"  kHz",g,state.font12,xf,yf);    
                }
                else if(state.frequency >= 1.0E6 && state.frequency < 1.0E9){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E6,6)+"  MHz",g,state.font12,xf,yf);    
                }
                else if(state.frequency >= 1.0E9 && state.frequency < 1.0E12){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E9,6)+"  GHz",g,state.font12,xf,yf);    
                }
                else if(state.frequency >= 1.0E12 && state.frequency < 1.0E15){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E12,6)+"  THz",g,state.font12,xf,yf);    
                }
                else if(state.frequency >= 1.0E15){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E15,6)+"  PHz",g,state.font12,xf,yf);    
                }
                
                g.setColor(Color.white);
                String tempo;
		MaestroG.subscripterSanSym2("   =","",""+MaestroA.rounder(state.lineLength,6)+" ",lambda,g,state.font12,x[2]-state.s2,getSize().height-state.s3);
		
		tempo = new String("  = "+MaestroA.rounder(state.lineLength,6));
		
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
	
                //Changes for general lengths units		
		double testlength=state.lineLength_meters;
                
		//  With no frequency, remove meters for line length
		if(testlength < 1.0E-9 && testlength >0.0 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1.0E9,8)
			+" nm",x[2]+state.s18+fm.stringWidth(tempo),getSize().height-state.s3);
		}
		else if(testlength < 1.0E-6 && testlength >= 1.0E-9 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1.0E9,4)
			+" nm",x[2]+state.s18+fm.stringWidth(tempo),getSize().height-state.s3);
		}
		else if(testlength < 1.0E-3 && testlength >= 1.0E-6 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1.0E6,4)
			+" \u00b5m",x[2]+state.s18+fm.stringWidth(tempo),getSize().height-state.s3);
		}
		else if(testlength < 1.0 && testlength >= 1.0E-3 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1000,4)
			+" mm",x[2]+state.s18+fm.stringWidth(tempo),getSize().height-state.s3);
		}
		else if((testlength < 1000.0 && testlength >= 1.0 ) || testlength == 0.0){
		    g.drawString(" = "+	MaestroA.rounder(testlength,6)
			+" m",x[2]+state.s18+fm.stringWidth(tempo),getSize().height-state.s3);
		}
		else if(testlength < 1.0E6 && testlength >=1.0E3){
		    g.drawString(" = "+	MaestroA.rounder(testlength/1000,4)
			+" km",x[2]+state.s18+fm.stringWidth(tempo),getSize().height-state.s3);
		}
		else if(testlength < 1.0E9 && testlength >=1.0E6){
		    //g.drawString(" = "+	MaestroA.rounder(testlength/1.0E6,4)
			//+" Mm",x[2]+18+fm.stringWidth(tempo),getSize().height-3);
                    
                    MaestroG.superscripter(" = "+	MaestroA.rounder(testlength/1.0E6,4)
			+" x 10","3"," km",g,state.font12,x[2]+state.s18+fm.stringWidth(tempo),getSize().height-state.s3);
		}
		else if(testlength >=1.0E9){
		    //g.drawString(" = "+	MaestroA.rounder(testlength/1.0E9,4)
			//+" Gm",x[2]+18+fm.stringWidth(tempo),getSize().height-3);
                    MaestroG.superscripter(" = "+	MaestroA.rounder(testlength/1.0E9,4)
			+" x 10","6"," km",g,state.font12,x[2]+state.s18+fm.stringWidth(tempo),getSize().height-state.s3);
                }
	}
	
	public void mouseClicked(MouseEvent evt){
	   //if(IsFocusOn){
		//IsFocusOn = false;
		//repaint();
	    //}
	
	    //else{
		//IsFocusOn = true;
		//repaint();
	    //}
    
	}
        
       public void actionPerformed(ActionEvent evt){
            
            if(evt.getSource()==details){
                if(IsFocusOn){
                    IsFocusOn = false;
                    repaint();
                }
                else{
                    IsFocusOn = true;
                    repaint();
                }
            }
        }

    
	public void mouseEntered(MouseEvent evt){
	    IsTopoOn = true;
	    repaint();
	}
	public void mouseExited(MouseEvent evt){
	    IsTopoOn = false;
	    repaint();
	}
	public void mousePressed(MouseEvent evt){}
	public void mouseReleased(MouseEvent evt){}

    
}//End of CircuitCanvs

