//CircuitCanvas.java
/* Usage: CircuitCanvas cc = new CircuitCanvas(310,120); */

import java.awt.*;
import java.awt.geom.*;
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] = 20;
		x[4] = getSize().width-20;
		x[2] = x[0] + 50;
		x[3] = x[4] - 20;
		x[1] = (x[0]+x[2])/2;

		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;
                    
                // 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]+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);
		
                //Draw parameter strings
                drawStrings(g);
                
                
		//ZL
		draw3DRect(x[4],y[2],24,y[1]-y[3],ccolor1,g);
		g.setColor(Color.white);
		MaestroG.subscripter("Z","L","",g,11,x[4]-5,y[2]+2);
		//Zg
		draw3DRect(x[1],y[4],x[2]-x[0]-16,22,ccolor1,g);	
		g.setColor(Color.white);
		MaestroG.subscripter("Z","g","",g,11,x[1]-5,y[4]+2);
		//Vg
		drawCircle(x[0],y[2],12,ccolor1,g);
		g.setColor(Color.white);
		MaestroG.subscripter("V","g","",g,11,x[0]-6,y[2]+1);
                
		//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);

		//Draw parameter strings
		//if(IsFocusOn){drawStrings(g);}
		
		/*
		if(IsFocusOn){drawStrings(g);
		    if(IsTopoOn){
			//g.setFont(labfont);
			//g.setColor(Color.white);
			//g.fillRect(getSize().width-80,2,75,22);
			
                        //g.setColor(Color.red);			
			//g.drawString("Click to hide",getSize().width-75,11);
			//g.drawString("details",getSize().width-75,21);
		    }
		}
		else{
		    if(IsTopoOn){
			//g.setFont(labfont);
			//g.setColor(Color.white);
			//g.fillRect(getSize().width-80,2,75,22);
			
			//g.setColor(Color.blue);
			//g.drawString("Click to show",getSize().width-75,11);
			//g.drawString("details",getSize().width-75,21);
		    }
		}
                */
		
		
		//Draw Stubs
		/*
                if(!IsFocusOn)
		{
		    for(int i =0; i < 3; i++){
			drawStub(state.stub[i],i,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-1,y-radius-1,2*radius+2,2*radius+2,30,-120);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	}

	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,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+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(stubColor);
			buf.drawLine(tax2+1,tay2,tbx2+1,tby2);
		}
            }
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	}

	public void drawRef(Graphics g){
	  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);
		//g.drawLine(tx1,ty1,tx2,ty2);
                MaestroG.drawArrow(tx1,ty1+14,5,g);
                MaestroG.drawArrow(tx1,ty2-14,6,g);
		g.drawLine(tx1,ty1+14,tx1,ty1+19);
                g.drawLine(tx1,ty2-14,tx1,ty2-19);
		
                g.setColor(very_lightGray);
                //g.fillRect(x[2],y[4]+5,tline8-x[2],10);
                //g.drawString("10",(tline8-tline9)/2 + tline9,y[4]+25);
                //if((tline8-tline9)> 12){g.drawString("10",tline9+1,y[4]+25);}
                //if((state.locations[9] - state.locations[8])> 0){g.drawString("1",tline9+1,y[4]+26);}
                if(state.ShuntPlace == 1){
                    drawZS(g, very_lightGray, tline9);
                }
                
                g.setColor(Color.yellow);
                //g.fillRect(tline8,y[4]+5,tline7-tline8,10);
                //if((tline7-tline8)> 6){g.drawString("9",tline8+1,y[4]+25);}
                //if((state.locations[8] - state.locations[7])> 0){g.drawString("2",tline8+1,y[4]+26);}
                if(state.ShuntPlace == 2){
                    drawZS(g, Color.yellow, tline8);
                }
                
                g.setColor(Color.pink);
                //g.fillRect(tline7,y[4]+5,tline6-tline7,10);
                //if((tline6-tline7)> 6){g.drawString("8",tline7+1,y[4]+25);}
                //if((state.locations[7] - state.locations[6])> 0){g.drawString("3",tline7+1,y[4]+26);}
                if(state.ShuntPlace == 3){
                    drawZS(g, Color.pink, tline7);
                }
                
                g.setColor(Color.cyan);
                //g.fillRect(tline6,y[4]+5,tline5-tline6,10);
                //if((tline5-tline6)> 6){g.drawString("7",tline6+1,y[4]+25);}
                //if((state.locations[6] - state.locations[5])> 0){g.drawString("4",tline6+1,y[4]+26);}
                if(state.ShuntPlace == 4){
                    drawZS(g, Color.cyan, tline6);
                }
                
                g.setColor(Color.orange);
                //g.fillRect(tline5,y[4]+5,tline4-tline5,10);
                //if((tline4-tline5)> 6){g.drawString("6",tline5+1,y[4]+25);}
                //if((state.locations[5] - state.locations[4])> 0){g.drawString("5",tline5+1,y[4]+26);}
                if(state.ShuntPlace == 5){
                    drawZS(g, Color.orange, tline5);
                }
                
                g.setColor(very_lightGray);
                //g.fillRect(tline4,y[4]+5,tline3-tline4,10);
                //if((tline3-tline4)> 6){g.drawString("5",tline4+1,y[4]+25);}
                //if((state.locations[4] - state.locations[3])> 0){g.drawString("6",tline4+1,y[4]+26);}
                if(state.ShuntPlace == 6){
                    drawZS(g, very_lightGray, tline4);
                }
                
                g.setColor(Color.yellow);
                //g.fillRect(tline3,y[4]+5,tline2-tline3,10);
                //if((tline2-tline3)> 6){g.drawString("4",tline3+1,y[4]+25);}
                //if((state.locations[3] - state.locations[2])> 0){g.drawString("7",tline3+1,y[4]+26);}
                if(state.ShuntPlace == 7){
                    drawZS(g, Color.yellow, tline3);
                }
                
                g.setColor(Color.pink);
                //g.fillRect(tline2,y[4]+5,tline1-tline2,10);
                //if((tline1-tline2)> 6){g.drawString("3",tline2+1,y[4]+25);}
                //if((state.locations[2] - state.locations[1])> 0){g.drawString("8",tline2+1,y[4]+26);}
                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;
                
                // transformer background
                g2d.setPaint(Color.white);
                g2d.setComposite(AlphaComposite.getInstance(rule, alpha));  
                g.fillRect(tline1,y[4]+2,tline0-tline1,y[0]-y[4]-4);    
                g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f)); // end transparency
                
                // arrows with lambda/4 label
                g.setColor(Color.yellow);
                //g.drawLine(tline1,y[0]-(y[0]-y[4])/4,tline1+(tline0-tline1),y[0]-(y[0]-y[4])/4);
                g.drawLine(tline1,y[0]-(y[0]-y[4])/4,tline1+(tline0-tline1)/4,y[0]-(y[0]-y[4])/4);
                g.drawLine(tline1+(tline0-tline1)-(tline0-tline1)/4,y[0]-(y[0]-y[4])/4,tline1+(tline0-tline1),y[0]-(y[0]-y[4])/4);
                MaestroG.drawArrow(tline1+8,y[0]-(y[0]-y[4])/4,8,g);
                MaestroG.drawArrow(tline1+(tline0-tline1)-8,y[0]-(y[0]-y[4])/4,7,g);
                
                // labels of transformer
                g.setColor(Color.white);
                MaestroG.subscripterSanSym2("Z", "\u03bb/4", " = "+MaestroA.rounder(state.ZIMP[1],4)+" ","\u03a9", g, 12, tline1+10,y[4]+(y[0]-y[4])/2-15);
                g.setColor(Color.yellow);
                MaestroG.subscripterSSym("\u03bb/4", "", "", g, 12,tline1+(tline0-tline1)/2-10,y[0]-(y[0]-y[4])/5);
                
                // 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));
                //g.drawLine(tline1, y[4]-3, tline1, y[0]+8);
                g.drawLine(tline0, y[4]-3, tline0, y[0]);
                //g.drawLine(tline0, y[0], tline0, y[0]+20);
                g2d.setStroke(new BasicStroke(1.0F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));
                //MaestroG.drawArrow(tline0, y[4]+10, 1, g);
                MaestroG.drawArrow(tline0, y[0]+9, 5, g);
                g.drawLine(tline0, y[0], tline0, y[0]+20);
                
                g.setColor(Color.white);
                MaestroG.subscripterSanSym2("Z","0"," = "+MaestroA.rounder(state.ZIMP[0],4)+" ","\u03a9", g, 12, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2-15);
                
                int yshifto = 10;
                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);
                }
                
                if(VSWRin >= 1.0E10){
                    g.setColor(Color.orange);
                    MaestroG.subscripter("SWR = \u221E","","", g, 14, 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(Color.orange);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e9,3)+" x 10 ","9","", g, 14, 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(Color.orange);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e8,3)+" x 10 ","8","", g, 14, 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(Color.orange);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e7,3)+" x 10 ","7","", g, 14, 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(Color.orange);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e6,3)+" x 10 ","6","", g, 14, 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(Color.orange);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e5,3)+" x 10 ","5","", g, 14, 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(Color.orange);
                    MaestroG.superscripter("SWR = "+MaestroA.rounder(VSWRin/1.e4,3)+" x 10 ","4","", g, 14, 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(Color.orange);
                    MaestroG.subscripter("SWR","", " = "+(int)VSWRin+" ", g, 14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin > 1.5 && VSWRin <1000.0){
                    g.setColor(Color.orange);
                    MaestroG.subscripter("SWR","", " = "+MaestroA.rounder(VSWRin,3)+" ", g, 14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.5 && VSWRin > 1.2){
                    g.setColor(Color.orange);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,3)+" ", g, 14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.2 && VSWRin > 1.1){
                    g.setColor(Color.yellow);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,4)+" ", g, 14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.1 && VSWRin > 1.001){
                    g.setColor(Color.yellow);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,4)+" ", g, 14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.001 && VSWRin > 1.000001){
                    g.setColor(Color.yellow);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,6)+" ", g, 14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                else if(VSWRin <= 1.000001){
                    g.setColor(Color.yellow);
                    MaestroG.subscripterB("SWR","", " = "+MaestroA.rounder(VSWRin,9)+" ", g, 14, tline2+(tline1-tline2)/2,y[4]+(y[0]-y[4])/2+yshifto);
                }
                //if((tline0-tline1)> 0){g.drawString("2",tline1+1,y[4]+25);}
                //if((state.locations[1] - state.locations[0])> 0.0){g.drawString("9",tline1+1,y[4]+26);}
                
                //------------------------------------------------------------------------------------------
                
			double tempR = state.Gammain.Magnitude();
                        int xinit2 = tline2+(tline1-tline2)/2;
                        int yinit2 = y[4]+(y[0]-y[4])/2+30;
                if(tempR < 0.2){
		    if(tempR >= 1.0E-3){
			MaestroG.superscripter2B("| \uu0393 | ","= "+MaestroA.rounder(tempR,6),"","",g,14,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,14,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,14,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,14,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,14,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,14,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,14,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,14,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,14,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,14,xinit2,yinit2);
		    }
                    else if(tempR < 1.0E-12 && tempR != 0.0){
			MaestroG.superscripter2B("| \uu0393 | ","= 0.0","","",g,14,xinit2,yinit2);
		    }
                }
                else{
			MaestroG.superscripter2("| \uu0393 | ","= "+MaestroA.rounder(tempR,6),"","",g,14,xinit2,yinit2);
                }
                
                g.setColor(Color.green);
                if(VSWRin <= state.targetVSWR){
                    MaestroG.subscripterB("SWR < "+state.targetVSWR,"","  -  MATCHING TARGET ACHIEVED", g, 12, xinit2-60, y[0]+13);
                }
                //------------------------------------------------------------------------------------------
                if(state.ShuntPlace == 9){
                    drawZS(g, Color.cyan, tline1);
                }
                
                if((state.is_Load_Ztype && (state.ZL.Real() == 0.0 && state.ZL.Imaginary() == 0.0))){
                    g.setColor(Color.black);
                    g.fillRect(440, 30, 300, 70);
                    g.setColor(Color.yellow);
                    MaestroG.subscripter("IMPEDANCE MATCH IS NOT POSSIBLE","","",g,14,450,50);
                    MaestroG.subscripter("IF LOAD IS A SHORT CIRCUIT","","",g,14,450,70);
                    g.setColor(Color.pink);
                    MaestroG.subscripterB("PLEASE, SELECT A NEW LOAD","","",g,14,450,90);
                }
                else if(state.is_Load_Ztype && (state.ZL.Real() == 0.0 && state.ZL.Imaginary() != 0.0)){
                    g.setColor(Color.black);
                    g.fillRect(440, 30, 300, 70);
                    g.setColor(Color.green);
                    MaestroG.subscripter("IMPEDANCE MATCH IS NOT POSSIBLE","","",g,14,450,50);
                    MaestroG.subscripter("IF LOAD IS A PURE REACTANCE","","",g,14,450,70);
                    g.setColor(Color.pink);
                    MaestroG.subscripterB("PLEASE, SELECT A NEW LOAD","","",g,14,450,90);
                }
                else if(!state.is_Load_Ztype && (state.YL.Real() == 0.0 && state.YL.Imaginary() != 0.0)){
                    g.setColor(Color.black);
                    g.fillRect(440, 30, 300, 70);
                    g.setColor(Color.green);
                    MaestroG.subscripter("IMPEDANCE MATCH IS NOT POSSIBLE","","",g,14,450,50);
                    MaestroG.subscripter("IF LOAD IS A PURE SUSCEPTANCE","","",g,14,450,70);
                    g.setColor(Color.pink);
                    MaestroG.subscripterB("PLEASE, SELECT A NEW LOAD","","",g,14,450,90);
                }
                else if(!state.is_Load_Ztype && (state.YL.Real() == 0.0 && state.YL.Imaginary() == 0.0)){
                    g.setColor(Color.black);
                    g.fillRect(440, 30, 300, 70);
                    g.setColor(Color.cyan);
                    MaestroG.subscripter("IMPEDANCE MATCH IS NOT POSSIBLE","","",g,14,450,50);
                    MaestroG.subscripter("IF LOAD IS AN OPEN CIRCUIT","","",g,14,450,70);
                    g.setColor(Color.pink);
                    MaestroG.subscripterB("PLEASE, SELECT A NEW LOAD","","",g,14,450,90);
                }
                    g.setColor(Color.orange);
                    //g.fillRect(tline0,y[4]+5,x[3]-tline0,10);
                    //if((x[3]-tline0)> 0){g.drawString("1",tline0+1,y[4]+25);}
                    //if((state.locations[0])> 0.0){g.drawString("10",tline0+1,y[4]+26);}
                    if(state.ShuntPlace == 10){
                        drawZS(g, Color.orange, tline0); 
                    }
                
          }
	}	

	private void drawZS(Graphics g, Color colore, int tx){
                g.setColor(new Color(220,220,220));
                    g.drawLine(tx-1,y[4],tx-11,y[4]+10);
                    g.drawLine(tx-11,y[4]+10,tx-11,y[0]+10);
                    g.drawLine(tx-11,y[0]+10,tx-1,y[0]);
                    
                    g.setColor(Color.black);
                    g.drawLine(tx,y[4],tx-10,y[4]+10);
                    g.drawLine(tx-10,y[4]+10,tx-10,y[0]+10);
                    g.drawLine(tx-10,y[0]+10,tx,y[0]);
                    g.setColor(colore);
                    g.fillRect(tx-20,y[4]+10+(y[0]-y[4])/2-20,20,40);
                    g.setColor(new Color(240,240,240));
                    g.drawRect(tx-20,y[4]+10+(y[0]-y[4])/2-20,20,40);
                    g.setColor(Color.black);
                    g.drawLine(tx,y[4]+10+(y[0]-y[4])/2-20,tx,y[4]+10+(y[0]-y[4])/2+20);
                    g.drawLine(tx-20,y[4]+10+(y[0]-y[4])/2+20,tx,y[4]+10+(y[0]-y[4])/2+20);
                    MaestroG.subscripter("Z","S","",g,11,tx-16,y[4]+10+(y[0]-y[4])/2+5);
        }
        
	private void drawAxis(Graphics g){
                Font symbolfont=new Font("Symbol",Font.PLAIN,14);
                Font normalfont=new Font("SanSerif",Font.PLAIN,12);
                
                String alpha, Ohm, lambda, infinity, Gamma, epsilon;
	
		g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393"; 
		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,4,g);
		g.setFont(new Font("Serif",Font.ITALIC,16));
		fm = g.getFontMetrics();
		g.setColor(Color.white);
		//g.drawString("l",x[2]-2,ytmp+fm.getHeight()-5);
                
                g.setFont(new Font("SanSerif",Font.PLAIN,12));
                fm = g.getFontMetrics();
                g.drawString("d",x[2]-8,ytmp+fm.getHeight()+1);
                g.setFont(new Font("SanSerif",Font.BOLD,12));
		g.drawString("d = 0",x[3]-12,ytmp+fm.getHeight()+1);
		g.setFont(new Font("SanSerif",Font.PLAIN,12));
		//g.drawString("d",15,ytmp+fm.getHeight()+1);
		
                g.setColor(Color.green);
                //g.drawString("d = "+MaestroA.rounder(state.xpos,8)+" "+lambda,x[2]+50,y[0]+14);
                MaestroG.SansNosansSymb2("d = "+MaestroA.rounder(state.xpos,8)+""," ",""+lambda,g,11,x[4]/2-30,y[4]-8);//y[0]+13);
                
        }
	
	private void drawStrings(Graphics g){
		
                String alpha, Ohm, lambda, infinity, Gamma, epsilon;
                Font symbolfont = new Font("Symbol",Font.PLAIN,12);
                Font normalfont = new Font("Serif",Font.PLAIN,11);
                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] - 200;
                int yf = getSize().height-6;
                
                if(Complex.Real(state.YL)==0.0&&Complex.Imaginary(state.YL)==0.0){ 
                    //MaestroG.subscripterSymMiddle2("Z","L"," =  ",infinity, "     (Open Circuit)",g,11,x[2]+10,y[4]-8);
                    MaestroG.subscripterInfinityTwo("Z","L"," =  ","", "     (Open Circuit)",g,11,xZL,y[4]-8);
	        }
		else if(Complex.Real(state.ZL)==0.0&&Complex.Imaginary(state.ZL)==0.0){ 
                    MaestroG.subscripter("Z","L"," = 0    (Short Circuit)",g,11,xZL,y[4]-8);
		}
		else{
                    MaestroG.subscripterSanSym2("Z","L"," = "+state.ZL.toString(Complex.CARTESIAN,3)+"   ",Ohm,g,11,xZL,y[4]-8);
		}
                
                if(state.ShuntPlace > 0){
                    if(Complex.Real(state.YS)==0.0&&Complex.Imaginary(state.YS)==0.0){ 
                        //MaestroG.subscripterSymMiddle2("Z","S"," =  ",infinity, "     (Open Circuit)",g,11,x[2]+10,y[4]-8);
                        MaestroG.subscripterInfinityTwo("Z","S"," =  ","", "     (Open Circuit)",g,11,xZL,yf);
                    }
                    else if(Complex.Real(state.ZS)==0.0&&Complex.Imaginary(state.ZS)==0.0){ 
                        MaestroG.subscripter("Z","S"," = 0    (Short Circuit)",g,11,xZL,yf);
                    }
                    else{
                        MaestroG.subscripterSanSym2("Z","S"," = "+state.ZS.toString(Complex.CARTESIAN,3)+"   ",Ohm,g,11,xZL,yf);
                    }
                }
                
                //MaestroG.subscripterSanSym2("Z","0"," = "+state.lineZ0+"  ",Ohm,g,11,x[2]+10,y[4]+17);
                MaestroG.subscripterSanSym2("Z","g"," = "+state.generator.getZg().toString()+"   ",Ohm,g,11,x[2]+5,y[4]-8);
		MaestroG.subscripter("V","g"," = "+state.generator.getVg().toString()+"  V",g,11,x[2]-30,y[4]+45);
                fm = g.getFontMetrics();
                int goup = fm.getHeight();
                MaestroG.subscripter("~","","",g,13,x[2]-30,y[4]+45-90*goup/200);
                
                g.setColor(Color.yellow);
                int xf = getSize().width/3+20;
                if(state.frequency_ref < 1.0E3){
                    MaestroG.subscripterSymFirst2I("f","o"," = "+MaestroA.rounder(state.frequency_ref,6)+"  Hz",g,11,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,11,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,11,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,11,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,11,xf,yf);    
                }
                else if(state.frequency_ref >= 1.0E15){
                    MaestroG.subscripterSymFirst2I("f","o"," = "+MaestroA.rounder(state.frequency_ref/1.0E15,6)+"  PHz",g,11,xf,yf);    
                }
                
                g.setColor(Color.cyan);
                xf = getSize().width/3+170;
                yf = getSize().height-5;
                
                if(state.frequency < 1.0E3){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency,6)+"  Hz",g,11,xf,yf);
                }
                else if(state.frequency >= 1.0E3 && state.frequency < 1.0E6){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E3,6)+"  kHz",g,11,xf,yf);    
                }
                else if(state.frequency >= 1.0E6 && state.frequency < 1.0E9){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E6,6)+"  MHz",g,11,xf,yf);    
                }
                else if(state.frequency >= 1.0E9 && state.frequency < 1.0E12){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E9,6)+"  GHz",g,11,xf,yf);    
                }
                else if(state.frequency >= 1.0E12 && state.frequency < 1.0E15){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E12,6)+"  THz",g,11,xf,yf);    
                }
                else if(state.frequency >= 1.0E15){
                    MaestroG.subscripterSymFirst2I("f",""," = "+MaestroA.rounder(state.frequency/1.0E15,6)+"  PHz",g,11,xf,yf);    
                }
                
                g.setColor(Color.white);
                String tempo;
		MaestroG.subscripterSanSym2("   =","",""+MaestroA.rounder(state.lineLength,6)+" ",lambda,g,12,x[2]-2,getSize().height-3);
		
		tempo = new String("  = "+MaestroA.rounder(state.lineLength,6));
		
                
                 //g.drawString(tempo,x[2]-3,getSize().height-3);
		
		//g.setFont(new Font("Symbol",Font.PLAIN,12));
		//g.drawString("\u03bb",x[2]+8+fm.stringWidth(tempo),getSize().height-3);
		g.setFont(new Font("SanSerif",Font.PLAIN,12));
	
	//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]+18+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength < 1.0E-6 && testlength >= 1.0E-9 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1.0E9,4)
			+" nm",x[2]+18+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength < 1.0E-3 && testlength >= 1.0E-6 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1.0E6,4)
			+" \u00b5m",x[2]+18+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength < 1.0 && testlength >= 1.0E-3 ){
		    g.drawString(" = "+	MaestroA.rounder(testlength*1000,4)
			+" mm",x[2]+18+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]+18+fm.stringWidth(tempo),getSize().height-3);
		}
		else if(testlength < 1.0E6 && testlength >=1.0E3){
		    g.drawString(" = "+	MaestroA.rounder(testlength/1000,4)
			+" km",x[2]+18+fm.stringWidth(tempo),getSize().height-3);
		}
		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,12,x[2]+18+fm.stringWidth(tempo),getSize().height-3);
		}
		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,12,x[2]+18+fm.stringWidth(tempo),getSize().height-3);
                }
                
                
	}
	
	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

