/**
 * NewGuideCanvas.java
*/
/* A Java class for
 * TwoWire.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 java.text.*;

public class NewGuideCanvas extends Canvas implements MouseListener{
    //private static final Color bgcolor = new Color(236,236,221);
    private static final Color bgcolor = Color.white;
    private static final Color medium1Color = Color.yellow;
    private static final Color medium2Color = Color.cyan;
    private int LeftMargin=20, RightMargin=10, TopMargin=10, BottomMargin=10;
    //private static final Font labfont2 = new Font("SanSerif",Font.PLAIN,12);
    public static Font labfont2;
    
    private int yBottom;
    private double frequency, a, b, w, epsilon_r, epsilon_r0, mu_r, phase_velocity, phase_velocity0;
    private double a_maximum, w_maximum, b_maximum, sigma2, sigma_metal;
    
    // Exact epsilon for exact phase velocity
    //private static final double epsilon0 = 8.8541878176E-12; //Units: F/m
    // Approximate epsilon for phase velocity 3 x 10^8 m/s
    private static final double epsilon0 = 8.841941286E-12; //8.8541878176E-12; //Units: F/m 

    private static final double mu0 = 1.25663706144E-6; //Units H/m
    
    private Image im;
    private Graphics buf;
    private String stmp;
    private double temp;
    private boolean IsFocusOn;
    
    private NewGuide_State state;
    
    public NewGuideCanvas(NewGuide_State state){
	super();
        this.state = state;

	IsFocusOn = false;
	setBackground(bgcolor);
	//Listeners
	this.addMouseListener(this);
        
        LeftMargin = state.s20; 
        RightMargin = state.s10; 
        TopMargin = state.s10; 
        BottomMargin = state.s10;
	
	a = 10.0; // mm
	a_maximum = 11.0; //mm
	b = 0.005; //mm
	b_maximum = 0.5; //mm
	w = 1.5; //mm
	w_maximum = 11.0; //mm
	epsilon_r = 2.3;
	epsilon_r0 = 1.0;
	mu_r = 1.0;
	frequency = 1.0E9;
	sigma2 = 0.0;
	sigma_metal = 5.8E7 ;
    }

    public void drawCanvas(Graphics g){
	g.clearRect(0,0,getSize().width,getSize().height);
	g.setColor(Color.black);
        
 	//if(IsFocusOn){drawBackLabels(g);}
	//drawAxis(g);
	drawGuide(g);
    }
    
    private void drawAxis(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
	double theta = Math.PI/4.0;
	int ArrowPos1, ArrowPos2;
	int Lsize = state.s30;
	        
        g.setColor(Color.black);
	//Vertical axis
	g.drawLine(LeftMargin,getSize().height-BottomMargin,LeftMargin,getSize().height-BottomMargin-Lsize);
	MaestroG.drawArrow(LeftMargin,getSize().height-BottomMargin-Lsize,5,g);
        
	//Horizontal axis
	g.drawLine(LeftMargin,getSize().height-BottomMargin,LeftMargin+Lsize,getSize().height-BottomMargin);
	//Draw up arrow
	MaestroG.drawArrow(LeftMargin+Lsize,getSize().height-BottomMargin,7,g);
	//Third axis
	g.drawLine(LeftMargin,getSize().height-BottomMargin,
		   LeftMargin+(int)(Lsize*Math.cos(theta)),getSize().height-BottomMargin-(int)(Lsize*Math.sin(theta)));
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        g.setColor(Color.black);
		// draw line
		int tipH = -state.s25;
		int horizH = LeftMargin;
		int verticH = getSize().height-BottomMargin;
		//g.drawLine(horizH,verticH,horizH-tipH,verticH+tipH);
		//draw oblique arrow head
		Polygon pH = new Polygon();
		pH.addPoint(horizH - tipH,verticH + tipH);
		pH.addPoint(horizH - (tipH + state.s2),verticH +(tipH + state.s6));
		pH.addPoint(horizH - (tipH + state.s6),verticH + (tipH + state.s2));
		//g.drawPolygon(pH);
		//g.fillPolygon(pH);
        MaestroG.drawArrow(LeftMargin+(int)(Lsize*Math.cos(theta)),getSize().height-BottomMargin-(int)(Lsize*Math.sin(theta)),9,g);
	
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        //Draw labels for axes
	//g.setFont(new Font("SanSerif",Font.PLAIN,12));
	g.setFont(labfont2);
	FontMetrics fm = g.getFontMetrics();
	g.drawString("x",LeftMargin-fm.stringWidth("x")/2,getSize().height-BottomMargin-Lsize-state.s10);
	g.drawString("y",LeftMargin+Lsize+state.s10+fm.stringWidth("y"),
		getSize().height-BottomMargin+fm.getHeight()/3);
	
	g.drawString("z",LeftMargin+(int)(Lsize*Math.cos(theta))+state.s10,getSize().height-BottomMargin-(int)(Lsize*Math.sin(theta))+state.s2);
    }
    
    private void drawGuide(Graphics g){    
        
	int xLeft, xRight, xCenter, xRef, yCenter;
	int yTop, width, thick, widthmax, amax, thickmax;
	int slabDH;//Thickness of the slab
	int slabDepth; //Depth of the slab
	double theta = Math.PI/4.0;
	Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
	thickmax = state.s5;
	widthmax = state.s100;
	amax = state.s100;
	if( w_maximum >= a_maximum){
	    amax = (int)(a_maximum/w_maximum*widthmax);
	    thickmax = (int)(b_maximum/a_maximum*amax);
	}
	if(w_maximum < a_maximum){
	    amax = state.s100;
	    widthmax = (int)(w_maximum/a_maximum*amax);
	    thickmax = (int)(b_maximum/a_maximum*amax);
	}
	width = (int)(w/w_maximum*widthmax)+1;
	if(w==0.0){
	    width = 0;
	}
	thick = (int)(b/b_maximum*thickmax)+1;
	if(b==0.0){
	    thick = 0;
	}

        int movex = 0;
        int movey = state.s3;
        
        // draw inside bottom wall
        g.setColor(new Color(234,245,255));
        //g.fillRect(state.s15+movex,state.s40+movey, state.s260,state.s100);
        Polygon boxbot = new Polygon();
        boxbot.addPoint(state.s15+movex,state.s140+movey);
        boxbot.addPoint(state.s175+movex,movey);
        boxbot.addPoint(state.s307+movex,movey);
        boxbot.addPoint(state.s275+movex,state.s40+movey);
        boxbot.addPoint(state.s275+movex,state.s140+movey);
        g.fillPolygon(boxbot);
        
        // draw top of box
        //Polygon boxtop = new Polygon();
        //boxtop.addPoint(15+movex,40+movey);
        //boxtop.addPoint(67+movex,movey);
        //boxtop.addPoint(287+movex,movey);
        //boxtop.addPoint(255+movex,40+movey);
        //g.fillPolygon(boxtop);
        
        // draw outside wall on right
        g.setColor(new Color(224,235,245));
        Polygon boxside = new Polygon();
        boxside.addPoint(state.s307+movex,movey);
        boxside.addPoint(state.s275+movex,state.s40+movey);
        boxside.addPoint(state.s275+movex,state.s140+movey);
        boxside.addPoint(state.s307+movex,state.s100+movey);
        g.fillPolygon(boxside);
	
	Color MetalColor1 = Color.lightGray;
	Color MetalColor2 = new Color(220,220,220);
	Color MetalColor3 = new Color(235,235,235);
	
	xLeft = LeftMargin + state.s40;
	xRight = getSize().width - RightMargin - state.s80;
	xCenter = (xRight+xLeft)/2;
	yCenter = getSize().height/2;
	xRef = xCenter-width/2;
	yBottom = TopMargin + state.s60 + (int)(a/a_maximum*amax) + state.s1;
	yTop = TopMargin + state.s60;
	slabDH = state.s5;
	slabDepth = state.s90;
	// Draw waveguide
	        
           double raggio = (double)(width/2);
           int iraggio = (width/2);
           
           g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
            int x1 = xCenter+(int)(2*Math.sin(theta)*raggio);
            int y1 = yCenter-(int)(3.25*Math.sin(theta)*raggio);
            int x2 = xCenter+(int)(3.25*Math.sin(theta)*raggio);
            int y2 = yCenter-(int)(2*Math.sin(theta)*raggio);
            int far = state.s150;
            
            //cylinder lines for conductors
            g2d.setPaint(new GradientPaint(xCenter+iraggio-(int)(a/a_maximum*amax/2)-(int)(Math.sin(theta)*iraggio),
                            yCenter-(int)(Math.sin(theta)*iraggio),Color.white,
                            xCenter+iraggio-(int)(a/a_maximum*amax/2)+(int)(Math.sin(theta)*iraggio)-state.s1,yCenter+(int)(Math.sin(theta)*iraggio),
                            Color.gray));
            
            //g2d.setPaint(new GradientPaint(113.0f,10.0f,Color.white,182.0f,10.0f,Color.lightGray));
            Polygon pK = new Polygon();
		pK.addPoint(xCenter+iraggio-(int)(a/a_maximum*amax/2)-(int)(Math.sin(theta)*iraggio),
                            yCenter-(int)(Math.sin(theta)*iraggio));
		pK.addPoint(xCenter+iraggio-(int)(a/a_maximum*amax/2)+(int)(Math.sin(theta)*iraggio)-state.s1,yCenter+(int)(Math.sin(theta)*iraggio));
                pK.addPoint((x1+x2)/2+2*far,(y1+y2)/2-2*far);
		pK.addPoint((x1+x2)/2+2*far,(y1+y2)/2-2*far);
            g.fillPolygon(pK);
            
            // conductor on left
            
            g.setColor(Color.lightGray);
            g.drawLine(xCenter+iraggio-(int)(a/a_maximum*amax/2)-(int)(Math.sin(theta)*iraggio),yCenter-(int)(Math.sin(theta)*iraggio),
                       (x1+x2)/2+2*far,(y1+y2)/2-2*far);
                        
            g.setColor(Color.gray);
            g.drawLine(xCenter+iraggio-(int)(a/a_maximum*amax/2)+(int)(Math.sin(theta)*iraggio)-state.s1,yCenter+(int)(Math.sin(theta)*iraggio),
                       (x1+x2)/2+2*far,(y1+y2)/2-2*far);
            
            
            // conductor on the right
            g.setColor(bgcolor);
            
            g2d.setPaint(new GradientPaint(xCenter+iraggio+(int)(a/a_maximum*amax/2)-(int)(Math.sin(theta)*iraggio),
                            yCenter-(int)(Math.sin(theta)*iraggio),Color.white,
                            xCenter+iraggio+(int)(a/a_maximum*amax/2)+(int)(Math.sin(theta)*iraggio)-state.s1,yCenter+60*(int)(Math.sin(theta)*iraggio)/100,
                            Color.gray));
            
            //g2d.setPaint(new GradientPaint(193.0f,10.0f,Color.white,242.0f,10.0f,Color.lightGray));
            Polygon pH = new Polygon();
		pH.addPoint(xCenter+iraggio+(int)(a/a_maximum*amax/2)-(int)(Math.sin(theta)*iraggio),
                            yCenter-(int)(Math.sin(theta)*iraggio));
		pH.addPoint(xCenter+iraggio+(int)(a/a_maximum*amax/2)+(int)(Math.sin(theta)*iraggio)-state.s1,yCenter+(int)(Math.sin(theta)*iraggio));
                pH.addPoint((x1+x2)/2+2*far,(y1+y2)/2-2*far);
		pH.addPoint((x1+x2)/2+2*far,(y1+y2)/2-2*far);
		
            //g.drawPolygon(pH);
            g.fillPolygon(pH);
            
            g.setColor(Color.lightGray);
            g.drawLine(xCenter+iraggio+(int)(a/a_maximum*amax/2)-(int)(Math.sin(theta)*iraggio),yCenter-(int)(Math.sin(theta)*iraggio),
                       (x1+x2)/2+2*far,(y1+y2)/2-2*far);
                        
            g.setColor(Color.gray);
            g.drawLine(xCenter+iraggio+(int)(a/a_maximum*amax/2)+(int)(Math.sin(theta)*iraggio)-state.s1,yCenter+(int)(Math.sin(theta)*iraggio),
                       (x1+x2)/2+2*far,(y1+y2)/2-2*far);    
            
            // Border strip on top
            g.setColor(bgcolor);
            g.fillRect(getSize().width/3,0,2*getSize().width/3+state.s10,state.s3);
            //------------------------------------------------------------------
            // draw vertical inner wall on the left 
            g.setColor(new Color(224,235,245));
            Polygon box2 = new Polygon();
            box2.addPoint(state.s15+movex,state.s140+movey);
            box2.addPoint(state.s175+movex,movey);
            box2.addPoint(state.s67+movex,movey);
            box2.addPoint(state.s15+movex,state.s40+movey);
            // bottom line
            g.fillPolygon(box2);
            //------------------------------------------------------------------
            g.setColor(Color.lightGray);
            g.drawLine(state.s15+movex,state.s140+movey, state.s175+movex,movey);
            //------------------------------------------------------------------
            //  Make top box slightly transparent
            int rule; float alpha;
            rule = AlphaComposite.SRC_OVER;
            alpha = 0.90f;
        
            g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
            //------------------------------------------------------------------
            g.setColor(new Color(245,245,255));
            // draw top of box to appear as if opaque
            Polygon boxtop2 = new Polygon();
            boxtop2.addPoint(state.s15+movex,state.s40+movey);
            boxtop2.addPoint(state.s67+movex,movey);
            boxtop2.addPoint(state.s307+movex,movey);
            boxtop2.addPoint(state.s275+movex,state.s40+movey);
            g.fillPolygon(boxtop2);
            //------------------------------------------------------------------
            alpha = 0.2f;
            g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
            // draw front rectangle - semitransparent
            g.setColor(new Color(250,250,250));
            g.fillRect(state.s15+movex,state.s40+movey, state.s260,state.s100);
            //------------------------------------------------------------------
            // return to opaque (alpha = 1.0)
            g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
            
	    //g.setColor(Color.red);
	    // Draw wires	    
	    g.setColor(new Color(240,240,240));
            
            g.fillOval(xCenter-(int)(a/a_maximum*amax/2)+1,yCenter-(int)(iraggio)+1,width-2,width-2);
	    g.fillOval(xCenter+(int)(a/a_maximum*amax/2)+1,yCenter-(int)(iraggio)+1,width-2,width-2);
	    
	    g.setColor(new Color(205,205,205));
	    
            g.drawOval(xCenter-(int)(a/a_maximum*amax/2)+1,yCenter-(int)(iraggio)+1,width-2,width-2);
	    g.drawOval(xCenter+(int)(a/a_maximum*amax/2)+1,yCenter-(int)(iraggio)+1,width-2,width-2);
	    
            // draw wire centers  ==============================================
            if((int)(iraggio) > state.s3){
                int myX = xCenter-(int)(a/a_maximum*amax/2)+iraggio;
                int myY = yCenter;
                g.setColor(Color.black);
                g.drawLine(myX-state.s2, myY, myX+state.s2, myY);
                g.drawLine(myX, myY-state.s2, myX, myY+state.s2);

                myX = xCenter+(int)(a/a_maximum*amax/2)+iraggio;
                g.drawLine(myX-state.s2, myY, myX+state.s2, myY);
                g.drawLine(myX, myY-state.s2, myX, myY+state.s2);
            }
            //------------------------------------------------------------------
	    // Draw vertical lines for wire diameters
	    g.setColor(Color.red);
	    g.drawLine(xCenter-(int)(a/a_maximum*amax/2),yCenter,
		       xCenter-(int)(a/a_maximum*amax/2),yCenter-(int)(iraggio)-state.s25);
	    g.drawLine(xCenter+(int)(width)-(int)(a/a_maximum*amax/2),yCenter,
		       xCenter+(int)(width)-(int)(a/a_maximum*amax/2),yCenter-(int)(iraggio)-state.s25);
	    g.drawLine(xCenter+(int)(a/a_maximum*amax/2),yCenter,
		       xCenter+(int)(a/a_maximum*amax/2),yCenter-(int)(iraggio)-state.s25);
	    g.drawLine(xCenter+(int)(width)+(int)(a/a_maximum*amax/2),yCenter,
		       xCenter+(int)(width)+(int)(a/a_maximum*amax/2),yCenter-(int)(iraggio)-state.s25);
	    //==================================================================
	    if((int)(iraggio) > state.s14){
		g.drawLine(xCenter+(int)(width)-(int)(a/a_maximum*amax/2)-state.s1,yCenter-(int)(iraggio)-state.s20,
		       xCenter-(int)(a/a_maximum*amax/2),yCenter-(int)(iraggio)-state.s20);
		g.drawLine(xCenter+(int)(width)+(int)(a/a_maximum*amax/2)-state.s1,yCenter-(int)(iraggio)-state.s20,
		       xCenter+(int)(a/a_maximum*amax/2),yCenter-(int)(iraggio)-state.s20);
		
		MaestroG.drawArrowScaled(xCenter+(int)(width)-(int)(a/a_maximum*amax/2)-state.s9,
			       yCenter-(int)(iraggio)-state.s20,3,state.sfactor,g);
		MaestroG.drawArrowScaled(xCenter-(int)(a/a_maximum*amax/2)+state.s8,
			       yCenter-(int)(iraggio)-state.s20,4,state.sfactor,g);
		
		MaestroG.drawArrowScaled(xCenter+(int)(width)+(int)(a/a_maximum*amax/2)-state.s9,
			       yCenter-(int)(iraggio)-state.s20,3,state.sfactor,g);
		MaestroG.drawArrowScaled(xCenter+(int)(a/a_maximum*amax/2)+state.s8,
			       yCenter-(int)(iraggio)-state.s20,4,state.sfactor,g);
                
	    }
	    else{
		
		g.drawLine(xCenter+(int)(width)-(int)(a/a_maximum*amax/2)+state.s1,yCenter-(int)(iraggio)-state.s20,
		       xCenter-(int)(a/a_maximum*amax/2)-state.s1,yCenter-(int)(iraggio)-state.s20);
		g.drawLine(xCenter+(int)(width)+(int)(a/a_maximum*amax/2)+state.s1,yCenter-(int)(iraggio)-state.s20,
		       xCenter+(int)(a/a_maximum*amax/2)-state.s1,yCenter-(int)(iraggio)-state.s20);
		
                if((int)(a/a_maximum*amax/2) > state.s15){
		    MaestroG.drawArrowScaled(xCenter+(int)(width)-(int)(a/a_maximum*amax/2)+state.s7,
			       yCenter-(int)(iraggio)-state.s20,4,state.sfactor,g);
		}
		MaestroG.drawArrowScaled(xCenter-(int)(a/a_maximum*amax/2)-state.s8,
			       yCenter-(int)(iraggio)-state.s20,3,state.sfactor,g);
		
		MaestroG.drawArrowScaled(xCenter+(int)(width)+(int)(a/a_maximum*amax/2)+state.s7,
			       yCenter-(int)(iraggio)-state.s20,4,state.sfactor,g);
		if((int)(a/a_maximum*amax/2) > state.s15){
		    MaestroG.drawArrowScaled(xCenter+(int)(a/a_maximum*amax/2)-state.s8,
			       yCenter-(int)(iraggio)-state.s20,3,state.sfactor,g);
		}
	    }
	    
	    // Draw lines for arrow between centers
	    g.drawLine(xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2),yCenter+(int)(iraggio),
		       xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2),yCenter+(int)(iraggio)+state.s20);
	    g.drawLine(xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2),yCenter+(int)(iraggio),
		       xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2),yCenter+(int)(iraggio)+state.s20);
	    	    
	    if((int)(a/a_maximum*amax/2) > state.s10){
		g.drawLine(xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2),yCenter+(int)(iraggio)+state.s15,
		       xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2),yCenter+(int)(iraggio)+state.s15);
		
                MaestroG.drawArrowScaled(xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2)-state.s8,yCenter+(int)(iraggio)+state.s15,3,state.sfactor,g);
		MaestroG.drawArrowScaled(xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2)+state.s8,yCenter+(int)(iraggio)+state.s15,4,state.sfactor,g);
	    }
	    else{g.drawLine(xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2),yCenter+(int)(iraggio)+state.s15,
		       xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2),yCenter+(int)(iraggio)+state.s15);
		
                MaestroG.drawArrowScaled(xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2)+state.s8,yCenter+(int)(iraggio)+state.s15,4,state.sfactor,g);
		MaestroG.drawArrowScaled(xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2)-state.s8,yCenter+(int)(iraggio)+state.s15,3,state.sfactor,g);
	    }
	    
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
	    // Draw Labels
	    //g.drawString("d",xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2)-state.s3,yCenter-(int)(iraggio)-state.s27);
	    //g.drawString("d",xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2)-state.s3,yCenter-(int)(iraggio)-state.s27);
	    //g.drawString("D",xCenter+(int)(iraggio)-state.s3,yCenter+(int)(iraggio)+state.s30);
	    
	    MaestroG.subscripter("d","","",g,state.font12,xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2)-state.s3,yCenter-(int)(iraggio)-state.s27);
	    MaestroG.subscripter("d","","",g,state.font12,xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2)-state.s3,yCenter-(int)(iraggio)-state.s27);
	    MaestroG.subscripter("D","","",g,state.font12,xCenter+(int)(iraggio)-state.s3,yCenter+(int)(iraggio)+state.s30);
	    
            g.setColor(Color.black);
            
            //g.drawString("+",xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2)-4,yCenter+5);
	    //g.drawString("+",xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2)-4,yCenter+5);
	    //MaestroG.subscripter("+","","",g,state.font10,xCenter+(int)(iraggio)-(int)(a/a_maximum*amax/2)-4,yCenter+5);
	    //MaestroG.subscripter("+","","",g,state.font10,xCenter+(int)(iraggio)+(int)(a/a_maximum*amax/2)-4,yCenter+5);
	    
	
	g.setColor(Color.black);
    	int x = state.s100;
	int y = getSize().height-state.s5;
	double f_normalized;
	
	/*
	if(frequency < 1.0E3){
		    f_normalized = frequency;
		    g.drawString("    f =  "+MaestroA.rounder(f_normalized,6)+"   [ Hz ]",x,y);	
		}
		else if(frequency < 1.0E6 && frequency >= 1.0E3  ){
		    f_normalized = frequency/1.0E3;
		    g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ kHz ]",x,y);	
		}
		else if(frequency < 1.0E9 && frequency >= 1.0E6 ){
		    f_normalized = frequency/1.0E6;
		    g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ MHz ]",x,y);	
		}
		else if(frequency < 1.0E12 && frequency >= 1.0E9 ){
		    f_normalized = frequency/1.0E9;
		    g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ GHz ]",x,y);	
		}
		else if(frequency < 1.0E15 && frequency >= 1.0E12 ){
		    f_normalized = frequency/1.0E12;
		    g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]",x,y);	
		}
		else{
		    f_normalized = frequency/1.0E12;
		    g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]",x,y);
		}
        */
		
        //MaestroG.subscripter("\u03bc","r","  = 1.0",g,12,18,42);
        //MaestroG.subscripter("\u03b5","r","  = "+MaestroA.rounder(epsilon_r,4),g,12,18,27);
        //MaestroG.subscripter("\u03c3",""," = "+MaestroA.rounder(sigma2,6)+" S/m",g,12,23,13);
        
        g.setColor(Color.lightGray);
        g.drawLine(state.s15+movex,state.s40+movey, state.s67+movex,0+movey);
        g.drawLine(state.s275+movex,state.s40+movey, state.s307+movex,0+movey);

        g.drawLine(state.s15+movex,state.s40+movey, state.s275+movex,state.s40+movey);
        g.drawLine(state.s15+movex,state.s140+movey, state.s275+movex,state.s140+movey);

        g.drawLine(state.s15+movex,state.s40+movey, state.s15+movex,state.s140+movey);
        g.drawLine(state.s275+movex,state.s40+movey, state.s275+movex,state.s140+movey);

        g.drawLine(state.s275+movex,state.s140+movey, state.s307+movex,state.s100+movey);
        //g.setColor(new Color(194,235,255));
        //g.fillRect(25,50, 240,100);

        //g.setFont(symbfont);
        //g.drawString("\u03bc",10,42);
        //g.drawString("\u03c3",13,13);
        //g.drawString("\u03b5",13,27);
        g.setFont(labfont2);

        if(w >= a){
            g.setColor(Color.red);
            int xstart1 = state.s5;
            g.drawString("WARNING!",xstart1, state.s65);
            g.drawString("Reduce ",xstart1, state.s80);
            g.drawString("diameter d",xstart1, state.s95);
            g.drawString("or increase",xstart1, state.s110);
            g.drawString("distance D",xstart1, state.s125);
        }

        int refnew = yTop + state.s30;
        g.setColor(Color.red);
        g.drawString("Substrate",state.s25,state.s160);
        g.drawString("Wires",state.s170,state.s160);
        g.setColor(Color.black);

        MaestroG.subscripter("\u03b5","r","  = "+MaestroA.rounder(epsilon_r,4),
                             g,state.font12,state.s18,state.s175);
        MaestroG.subscripter("\u03c3",""," = "+MaestroA.rounder(sigma2,6)+
                             " [S/m]",g,state.font12,state.s23,state.s190);
        DecimalFormat dec = new DecimalFormat("0.###E0");
        MaestroG.subscripter("\u03c3","c"," = "+dec.format(sigma_metal)+
                             " [S/m]",g,state.font12,state.s165,state.s175);
        //MaestroG.subscripter("\u03c3","c"," = "+MaestroA.rounder(sigma_metal,6)+
        //                     " [S/m]",g,state.font12,state.s165,state.s175);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    
        //THIN FRAME
        g.setColor(Color.black);
        g.drawRect(0,0,getSize().width-1,getSize().height-1); 
    }
    
    
    public synchronized void setThick(double b){
	this.b = b;
	repaint();
    }
    
    public synchronized void setThickmax(double b_maximum){
	this.b_maximum = b_maximum;
	repaint();
    }
    
    public synchronized void setStrip(double w){
	this.w = w;
	repaint();
    }
    
    public synchronized void setStripmax(double w_maximum){
	this.w_maximum = w_maximum;
	repaint();
    }
    
    public synchronized void setFrequency(double frequency){
	this.frequency = frequency;
    }
    
    public synchronized void setWidth(double a){
	this.a = a;
    }
    
    public synchronized void setWidthMax(double a_maximum){
	this.a_maximum = a_maximum;
    }
    
    public synchronized void setEpsilon(double epsilon_r){
	this.epsilon_r = epsilon_r;
	
    }
    
    public synchronized void setEpsilon0(double epsilon_r0){
	this.epsilon_r0 = epsilon_r0;
	
    }
    
    public synchronized void setMu(double mu){
	this.mu_r = mu;
    }
    
    public synchronized void setSigma2(double sigma2){
	this.sigma2 = sigma2;
    }
    
    public synchronized void setSigmaMetal(double sigma_metal){
	this.sigma_metal = sigma_metal;
    }
    
    public void paint(Graphics g){
	    if(im == null){
		im = createImage(getSize().width,getSize().height);
		buf = im.getGraphics();
		drawCanvas(buf);
	    }
	    else{
		drawCanvas(buf);
	    }
	    g.drawImage(im,0,0,null);
    }
    
    
    public void update(Graphics g){
	paint(g);
    }
    
    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){}
}
