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

public class NewGuideCanvas extends Canvas implements MouseListener{
    private static final Color ccolor1 = new Color(50,204,153);
    private static final Color bgcolor2 = new Color(255,255,250);
    private static final Color bgcolor = Color.white;
    //private static final Color bgcolor2 = Color.gray;
    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 Font labfont = new Font("SanSerif",Font.PLAIN,10);
    private Font symbfont= new Font("Serif",Font.PLAIN,12);
    private Font symbfont2= new Font("Serif",Font.PLAIN,14);
    private Font labfont2= new Font("Serif",Font.BOLD,16);
    private Font labfont4= new Font("Serif",Font.PLAIN,12);
    private Font labfont3 = new Font("SanSerif",Font.PLAIN,12);
    private int yBottom;
    private int x[], y[];
    private double frequency, epsilon_r, epsilon_r0, mu_r, phase_velocity, phase_velocity0;
    private double DipoleLength_lambda, wavelength, radius_lambda;
    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 static final double light_velocity = 3.0E8; //  Units m/s - simplified
    
    private Image im;
    private Graphics buf;
    private String stmp;
    private double temp;
    private boolean IsFocusOn, IsTopoOn;
    public boolean Is3D;
    NewGuide_State state;
    
    public NewGuideCanvas(NewGuide_State state){
	super();
        this.state = state;
        
	IsFocusOn = false;
	IsTopoOn = false;
	
        LeftMargin = state.s20;
        RightMargin = state.s10;
        TopMargin = state.s10;
        BottomMargin = state.s10;
        labfont = new Font("SanSerif",Font.PLAIN,state.font10);
        symbfont= new Font("Serif",Font.PLAIN,state.font12);
        symbfont2= new Font("Serif",Font.PLAIN,state.font14);
        labfont2= new Font("Serif",Font.BOLD,state.font16);
        labfont4= new Font("Serif",Font.PLAIN,state.font12);
        labfont3 = new Font("SanSerif",Font.PLAIN,state.font12);
        
	setBackground(bgcolor);
	x = new int[5];
	y = new int[5];

	//Listeners
	this.addMouseListener(this);
	
	epsilon_r = 1.0;
	epsilon_r0 = 1.0;
	mu_r = 1.0;
	frequency = 1.0E9;
	DipoleLength_lambda = 0.5;
	radius_lambda = 1.0E-5;
	
	Is3D = true;
	
	wavelength = light_velocity/frequency/Math.sqrt(epsilon_r);
	
    }

    public void drawCanvas(Graphics g){
	
	setBackground(bgcolor2);
	g.clearRect(0,0,getSize().width,getSize().height);
	g.setColor(Color.black);
	
	    draw3D(g);
    }
    
    private void drawAxis(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	double theta = Math.PI/6.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);
        MaestroG.drawArrowScaled(LeftMargin,getSize().height-BottomMargin-Lsize, 1, state.sfactor, 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);
	MaestroG.drawArrowScaled(LeftMargin+Lsize,getSize().height-BottomMargin, 3, state.sfactor, g);
	
        //Third axis
	g.drawLine(LeftMargin,getSize().height-BottomMargin,
		   LeftMargin+(int)(Lsize*Math.cos(theta)),getSize().height-BottomMargin-(int)(Lsize*Math.sin(theta)));
	
        //Draw labels for axes
	g.setFont(new Font("SanSerif",Font.ITALIC,state.font11));
	FontMetrics fm = g.getFontMetrics();
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        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.s15,getSize().height-BottomMargin-(int)(Lsize*Math.sin(theta))+state.s2);
	
    }
    
    private void drawWire(int x1, int y1, int x2, int y2, int mode, Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		//g.setColor(Color.white);
		g.setColor(Color.gray);
		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);
		}
    }

    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.setColor(Color.gray);
		g.drawLine(x-width/2,y-height/2,x+width/2,y-height/2);
		g.drawLine(x-width/2,y-height/2,x-width/2,y+height/2);
		g.setColor(Color.black);
		g.drawLine(x-width/2,y+height/2,x+width/2,y+height/2);
		g.drawLine(x+width/2,y-height/2,x+width/2,y+height/2);
	}
	
	private void drawRect(int x, int y, int width, int height, Color c, Graphics g){
		g.setColor(c);
		g.fillRect(x-width/2,y-height/2,width,height);
	}
	

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

	private void drawAxis2(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
            
                int ytmp;
		FontMetrics fm;
		ytmp=y[0] + state.s10;
		//g.setColor(Color.white);
		g.setColor(Color.gray);
		g.drawLine(x[4],ytmp,x[2]-state.s5,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);
		g.drawLine(x[2],y[0]+state.s15,x[2],y[4]-state.s30);
		//Arrowhead
		//MaestroG.drawArrow(x[2],y[4]-state.s30,5,g);
		//MaestroG.drawArrow(x[4],ytmp,7,g);
                MaestroG.drawArrowScaled(x[2],y[4]-state.s30, 1, state.sfactor, g);
                MaestroG.drawArrowScaled(x[4],ytmp, 3, state.sfactor, g);
                
		g.setFont(new Font("SanSerif",Font.BOLD,state.font12));
		fm = g.getFontMetrics();
		g.setColor(Color.black);
		g.drawString("0",x[2]-state.s2,ytmp+fm.getHeight()+state.s2);
		g.drawString("L",x[3]-state.s3,ytmp+fm.getHeight()+state.s2);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		g.drawString("z",x[4]+state.s5,ytmp+fm.getHeight());
		g.drawString("y",x[2]+state.s8,y[4]-state.s30);
    }
	
    private void draw3D(Graphics g){    
	Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	//Draw the background
		g.setColor(Color.black);
		//set the arrays
		x[0] = getSize().width/2-state.s30;
		x[1] = x[0] - state.s7;
		x[2] = getSize().width-state.s50;
		
		y[0] = getSize().height-state.s20;
		y[1] = getSize().height/2;

		g.clearRect(0,0,getSize().width-1,getSize().height-1);
		//------------------------------------------------------------ Insert frequency printout
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font10));
		//---------------------------------------------------------
		if(IsTopoOn){
		    if(!IsFocusOn){
			g.setColor(Color.blue);
			g.drawString("Click to show ",getSize().width*2/3+state.s2,getSize().height*3/4);
			g.drawString("angles reference",getSize().width*2/3+state.s2,getSize().height*3/4+state.s11);
		    }
		    else{
			g.setColor(Color.red);
			g.drawString("Click to show ",getSize().width*2/3+state.s2,getSize().height*3/4);
			g.drawString("dipole diagram",getSize().width*2/3+state.s2,getSize().height*3/4+state.s11);
			
		    }
		}
		//----------------------------------------------------------
		g.setColor(Color.black);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		int xf = x[0]+state.s30;
		int yf = getSize().height-state.s5;
		double f_normalized;
                
		if(frequency < 1.0E3){
		    f_normalized = frequency;
		    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ Hz ]","",g,state.font12,xf,yf);	
		}
		else if(frequency < 1.0E6 && frequency >= 1.0E3  ){
		    f_normalized = frequency/1.0E3;
		    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ kHz ]","",g,state.font12,xf,yf);
		}
		else if(frequency < 1.0E9 && frequency >= 1.0E6 ){
		    f_normalized = frequency/1.0E6;
		    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ MHz ]","",g,state.font12,xf,yf);
		}
		else if(frequency < 1.0E12 && frequency >= 1.0E9 ){
		    f_normalized = frequency/1.0E9;
		    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ GHz ]","",g,state.font12,xf,yf);
		}
		else if(frequency < 1.0E15 && frequency >= 1.0E12 ){
		    f_normalized = frequency/1.0E12;
		    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]","",g,state.font12,xf,yf);
		}
		else{
		    f_normalized = frequency/1.0E12;
		    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]","",g,state.font12,xf,yf);
		}
		//-----------------------------------------------------------------------------------
		String lambda, rho;
		g.setFont(symbfont2);
		g.getFontMetrics();
		lambda="\u03bb";
		rho ="\u03c1";
		
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.setColor(Color.blue);
		g.setFont(new Font("Serif",Font.ITALIC,state.font15));
                
		g.getFontMetrics();
		g.drawString("l",x[0]+state.s60,state.s15);
                g.drawString("l",x[0]+state.s60,state.s15);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		g.getFontMetrics();
		g.drawString("   = "+MaestroA.rounder(DipoleLength_lambda,4)+"  "+lambda,x[0]+state.s60,state.s15);
		
		if(radius_lambda > (DipoleLength_lambda/10.0)){
		  //if(!IsFocusOn){
		    g.setColor(Color.red);
		    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.special("WARNING: "," a"," > 0.1"," l",g,state.font12,x[0]+state.s35,state.s35);
                    
		  //}  
		}
		
	    if(IsFocusOn){
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
		//top piece of x-axis
		g.setColor(Color.gray);
		int tip = state.s37;
		g.drawLine(x[0]+tip-state.s6,y[1]-tip+state.s6,x[0]+tip,y[1]-tip);
		
		//Arc for phi
		int xCenter = x[0];
		int Radius = state.s65;
		int yCenter = y[1]+ Radius/2+2;
		int beginAngle = 115;
		int Angle = 305;
		int Angle2 = 52;
		int raio = 2*Radius;
		g.setColor(Color.white);
		g.fillArc(xCenter-Radius,yCenter-Radius,raio,raio/2,beginAngle,Angle);
		g.setColor(Color.black);
		g.drawArc(xCenter-Radius,yCenter-Radius,raio,raio/2,beginAngle,Angle);
                
		//Arc for theta
		Radius = state.s65;
		xCenter = x[0] + Radius/2+state.s2;
		yCenter = y[1];
		beginAngle = -28;
		//Angle = 128+56;
		Angle = 360;
		Angle2 = 62;
		raio = 2*Radius;
		g.setColor(new Color(240,240,255));
		g.fillArc(xCenter-Radius,yCenter-Radius,raio/2,raio,beginAngle,Angle);
		g.setColor(Color.black);
		g.drawArc(xCenter-Radius,yCenter-Radius,raio/2,raio,beginAngle-120,Angle+120);
		beginAngle = -28;
		Angle = 118;
		g.setColor(Color.cyan);
		g.fillArc(xCenter-Radius,yCenter-Radius,raio/2,raio,beginAngle,Angle);
		
		g.setColor(Color.gray);
		g.drawArc(x[0]-Radius,y[1]-Radius,raio,raio,0,360);
		g.setColor(Color.black);
		
		g.drawLine(x[0]+state.s39,y[1]+state.s10,x[0]+state.s32,y[1]+state.s20);
		g.drawLine(x[0]+state.s29,y[1]+state.s8,x[0]+state.s32,y[1]+state.s20);
				
		g.setColor(Color.black);
		
		//Arc for phi
		xCenter = x[0];
		yCenter = y[1]+ Radius/2+state.s2;
		Radius = state.s65;
		beginAngle = 117;
		Angle = 180;
		Angle2 = 52;
		raio = 2*Radius;
		g.setColor(Color.white);
		g.fillArc(xCenter-Radius,yCenter-Radius,raio,raio/2,beginAngle,Angle);
		g.setColor(Color.black);
		g.drawArc(xCenter-Radius,yCenter-Radius,raio,raio/2,beginAngle,Angle);
		
		beginAngle = 242;
		Angle = 55;
		g.setColor(Color.yellow);
		g.fillArc(xCenter-Radius,yCenter-Radius,raio,raio/2,beginAngle,Angle);
		
                // arrow arc for phi
                //g.setColor(Color.black);
		//g.drawArc(xCenter-Radius,yCenter-Radius,raio,raio/2,beginAngle,Angle);
		//g.drawArc(xCenter-Radius,yCenter-Radius,raio-1,raio/2-1,beginAngle,Angle);
		
		//phi - hidden edge
		g.setColor(new Color(220,220,220));
		g.drawArc(xCenter-Radius,yCenter-Radius,raio,raio/2,67,45);
		//theta - hidden edge
		Radius = state.s65;
		xCenter = x[0] + Radius/2+state.s2;
		yCenter = y[1];
		g.setColor(new Color(220,220,220));
		g.drawArc(xCenter-Radius,yCenter-Radius,raio/2,raio,157,45);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
		//labels
		g.setColor(Color.blue);
		g.setFont(new Font("Serif",Font.PLAIN,state.font15));
		
                // theta in angle
                g.drawString("\u03b8",x[0]+state.s3,y[1]-state.s13);
		
                // theta zero reference
                g.drawString("\u03b8",x[0]+state.s13,y[1]-state.s70);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		g.drawString("= 0",x[0]+state.s25,y[1]-state.s70);
						
		// phi in angle
                g.setColor(Color.blue);
		g.setFont(new Font("Serif",Font.PLAIN,state.font15));
		g.drawString("\u03c6",x[0]-state.s9,y[1]+state.s25);
		
                // phi zero reference
                g.drawString("\u03c6",x[0]-state.s44,y[1]+state.s90);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		g.drawString("= 0",x[0]-state.s32,y[1]+state.s92);
		
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setColor(Color.red);
		g.drawLine(x[0]-state.s30,y[1]+state.s82,x[0]-state.s30,y[1]+state.s34); // line for phi zero reference
		g.drawLine(x[0],y[1]-state.s67,x[0]+state.s10,y[1]-state.s74); // line for theta zero reference
                
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                	
		g.setColor(Color.black);
		
		//draw axes
		g.setColor(Color.gray);
		g.drawLine(x[0],state.s20,x[0],y[0]); 
		//MaestroG.drawArrow(x[0],state.s20,5,g);
                MaestroG.drawArrowScaled(x[0],state.s20, 1, state.sfactor, g);
                
		//g.setColor(new Color(210,210,210));
		g.drawLine(x[0],y[1],x[0],y[1]+state.s35);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font11));
		g.setColor(Color.black);
		g.drawString("z",x[0]-state.s12,state.s20);
		g.setColor(Color.gray);
		g.drawLine(x[1]-state.s70,y[1],x[2]+state.s10,y[1]);
		//MaestroG.drawArrow(x[2]+state.s10,y[1],7,g);
                MaestroG.drawArrowScaled(x[2]+state.s10, y[1], 3, state.sfactor, g);
                
		g.setColor(Color.black);
		g.drawString("y",x[2]+state.s10,y[1]-state.s9);
		g.setColor(Color.gray); 
                //g.setColor(new Color(210,210,210));
		g.drawLine(x[0],y[1],x[0]+state.s33,y[1]);
		g.drawLine(x[0],y[1],x[0]+state.s28,y[1]-state.s28);
		
		g.setColor(Color.gray);
		tip = state.s87;
		g.drawLine(x[0],y[1],x[0]-tip,y[1]+tip);
		g.setColor(Color.black);
		g.drawString("x",x[0]-(tip-state.s12),y[1]+tip);
		g.setColor(Color.gray);
		//draw oblique arrow head
                g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
                //MaestroG.drawArrow(x[0]-tip+state.s5,y[1]+tip-state.s5,10,g);
                MaestroG.drawArrowScaled(x[0]-tip+state.s5,y[1]+tip-state.s5, 6, state.sfactor, g);
                
		Polygon p = new Polygon();
		p.addPoint(x[0]-tip,y[1]+tip);
		p.addPoint(x[0]-(tip-state.s2),y[1]+(tip-state.s6));
		p.addPoint(x[0]-(tip-state.s6),y[1]+(tip-state.s2));
		//g.drawPolygon(p);
		//g.fillPolygon(p);
		
		// arrowhead for phi
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setColor(Color.black);
		g.drawLine(x[0]+state.s15,y[1]+state.s28,x[0]+state.s25,y[1]+state.s31);
		g.drawLine(x[0]+state.s15,y[1]+state.s38,x[0]+state.s25,y[1]+state.s32);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	    }
	    else{	
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
		MaestroG.special("2","a","","",g,state.font12,x[0]-state.s35,state.s59);
		g.setColor(Color.gray);
		g.drawLine(x[0]-state.s18,state.s55,x[0]-state.s5,state.s55);
		//MaestroG.drawArrow(x[0]-state.s12,state.s55,7,g);
                MaestroG.drawArrowScaled(x[0]-state.s12,state.s55, 3, state.sfactor, g);
		g.drawLine(x[0]+state.s20,state.s55,x[0]+state.s5,state.s55);
		//MaestroG.drawArrow(x[0]+state.s12,state.s55,8,g);
                MaestroG.drawArrowScaled(x[0]+state.s12,state.s55, 4, state.sfactor, g);
		//Arc for theta
		int xCenter = x[0];
		int yCenter = y[1];
		int Radius = state.s15;
		int beginAngle = 28;
		int Angle = 62;
		int raio = 2*Radius;
		
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setColor(Color.cyan);
		g.fillArc(xCenter-Radius,yCenter-Radius,raio,raio,beginAngle,Angle);
		g.setColor(Color.black);
		g.drawArc(xCenter-Radius,yCenter-Radius,raio,raio,beginAngle,Angle);
		g.setColor(Color.blue);
		g.setFont(new Font("Serif",Font.PLAIN,state.font15));
		g.drawString("\u03b8",x[0]+state.s5,y[1]-state.s16);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		g.setColor(Color.black);
		
		//Arc for phi
		xCenter = x[0];
		yCenter = y[1];
		Radius = state.s15;
		beginAngle = 225;
		Angle = 87;
		raio = 2*Radius;
		g.setColor(Color.yellow);
		g.fillArc(xCenter-Radius,yCenter-Radius,raio,raio,beginAngle,Angle);
		g.setColor(Color.black);
		g.drawArc(xCenter-Radius,yCenter-Radius,raio,raio,beginAngle,Angle);
		
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        
                g.setColor(Color.blue);
		g.setFont(new Font("Serif",Font.PLAIN,state.font15));
		g.drawString("\u03c6",x[0]-state.s14,y[1]+state.s25);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font11));
		
		//draw axes
		g.setColor(Color.gray);
		g.drawLine(x[0],state.s20,x[0],y[0]); 
		//MaestroG.drawArrow(x[0],state.s20,5,g);
                MaestroG.drawArrowScaled(x[0],state.s20, 1, state.sfactor, g);
		
		g.drawLine(x[0],y[1],x[0],y[1]+state.s15);
		g.setColor(Color.black);
		g.drawString("z",x[0]-state.s12,state.s20);
		g.setColor(Color.gray);
		
		g.drawLine(x[1],y[1],x[2]+state.s10,y[1]);
		//MaestroG.drawArrow(x[2]+state.s10,y[1],7,g);
		MaestroG.drawArrowScaled(x[2]+state.s10,y[1], 3, state.sfactor, g);
		
                g.setColor(Color.black);
		g.drawString("y",x[2]+state.s10,y[1]-state.s9);
		g.setColor(Color.gray);
		
		int tip = state.s87;
		g.drawLine(x[0],y[1],x[0]-tip,y[1]+tip);
		g.setColor(Color.black);
		g.drawString("x",x[0]-(tip-state.s12),y[1]+tip);
		g.setColor(Color.gray);
		//draw oblique arrow head
                //MaestroG.drawArrow(x[0]-tip+state.s5,y[1]+tip-state.s5,10,g);
                MaestroG.drawArrowScaled(x[0]-tip+state.s5,y[1]+tip-state.s5, 6, state.sfactor, g);
		
		Polygon p = new Polygon();
		p.addPoint(x[0]-tip,y[1]+tip);
		p.addPoint(x[0]-(tip-state.s2),y[1]+(tip-state.s6));
		p.addPoint(x[0]-(tip-state.s6),y[1]+(tip-state.s2));
		//g.drawPolygon(p);
		//g.fillPolygon(p);
						
	    //draw antenna  -------------------------------
		//draw measurement arrows
		g.setColor(Color.gray);
		int xarrow = x[0]-state.s70;
		
		g.drawLine(xarrow,y[1]-state.s50,xarrow,y[1]+state.s50);
		MaestroG.drawArrow(xarrow,y[1]-state.s40,5,g);
		MaestroG.drawArrow(xarrow,y[1]+state.s40,6,g);
		MaestroG.drawArrow(xarrow,y[1]-state.s10,6,g);
		MaestroG.drawArrow(xarrow,y[1]+state.s10,5,g);
                
                MaestroG.drawArrowScaled(xarrow,y[1]-state.s40, 1, state.sfactor, g);
		MaestroG.drawArrowScaled(xarrow,y[1]+state.s40, 2, state.sfactor, g);
		MaestroG.drawArrowScaled(xarrow,y[1]-state.s10, 2, state.sfactor, g);
		MaestroG.drawArrowScaled(xarrow,y[1]+state.s10, 1, state.sfactor, g);
		
		g.drawLine(xarrow-state.s5,y[1],xarrow+state.s5,y[1]);
		g.drawLine(xarrow-state.s5,y[1]-state.s50,xarrow+state.s5,y[1]-state.s50);
		g.drawLine(xarrow-state.s5,y[1]+state.s50,xarrow+state.s5,y[1]+state.s50);
		
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.setColor(Color.blue);
		g.setFont(new Font("Serif",Font.ITALIC,state.font15));
		g.drawString("l",xarrow+state.s10,y[1]-state.s25);
		
                g.setFont(new Font("Serif",Font.PLAIN,state.font12));
                g.drawString("__",xarrow+state.s7,y[1]-state.s25);
		g.drawString("2",xarrow+state.s10,y[1]-state.s12);
		//g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                g.setFont(new Font("Serif",Font.ITALIC,state.font15));
		g.drawString("l",xarrow+state.s10,y[1]+state.s25);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.setFont(new Font("Serif",Font.PLAIN,state.font12));
		g.drawString("__",xarrow+state.s7,y[1]+state.s25);
		g.drawString("2",xarrow+state.s10,y[1]+state.s38);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		
		//top dipole
		g.setColor(Color.white);
		g.drawLine(x[0],y[1]-state.s50,x[0],y[1]-state.s5);
                MaestroG.drawLineThick(g,x[0],y[1]-state.s50,x[0],y[1]-state.s5,state.s4,Color.red);
                
                g.setColor(Color.red);
		g.drawLine(x[0]-state.s1,y[1]-state.s50,x[0]-state.s1,y[1]-state.s5);
		g.drawLine(x[0]+state.s1,y[1]-state.s50,x[0]+state.s1,y[1]-state.s5);
		g.setColor(Color.gray);
		g.drawLine(x[0]+state.s2,y[1]-state.s50,x[0]+state.s2,y[1]-state.s5);
		//tips
		g.setColor(Color.black);
		g.drawLine(x[0]-state.s1,y[1]-state.s50,x[0]+state.s2,y[1]-state.s50);
		g.drawLine(x[0]-state.s1,y[1]-state.s5,x[0]+state.s2,y[1]-state.s5);
		
		//bottom dipole
		//top dipole
		
		//g.setColor(Color.white);
		//g.drawLine(x[0],y[1]+state.s50,x[0],y[1]+state.s16);
		//g.setColor(Color.red);
                MaestroG.drawLineThick(g,x[0],y[1]+state.s50,x[0],y[1]+state.s16,state.s4,Color.red);
                
		//g.drawLine(x[0]-state.s1,y[1]+state.s50,x[0]-state.s1,y[1]+state.s16);
		//g.drawLine(x[0]+state.s1,y[1]+state.s50,x[0]+state.s1,y[1]+state.s16);
		//g.setColor(Color.gray);
		//g.drawLine(x[0]+state.s2,y[1]+state.s50,x[0]+state.s2,y[1]+state.s16);
		//tip down
		g.setColor(Color.black);
		g.drawLine(x[0]-state.s1,y[1]+state.s50,x[0]+state.s2,y[1]+state.s50);
		
		//section behind yellow angle mark
		g.setColor(Color.white);
		g.drawLine(x[0],y[1]+state.s6,x[0],y[1]+state.s14);
		MaestroG.drawLineThick(g,x[0],y[1]+state.s6,x[0],y[1]+state.s12,state.s4,new Color(250,150,150));
                
                g.setColor(new Color(250,150,150));
		g.drawLine(x[0]-state.s1,y[1]+state.s6,x[0]-state.s1,y[1]+state.s14);
		g.drawLine(x[0]+state.s1,y[1]+state.s6,x[0]+state.s1,y[1]+state.s14);
		g.setColor(Color.gray);
		g.drawLine(x[0]+state.s2,y[1]+state.s6,x[0]+state.s2,y[1]+state.s14);
		//tip up
		g.setColor(Color.gray);
		g.drawLine(x[0]-state.s1,y[1]+state.s6,x[0]+state.s2,y[1]+state.s6);
	    //----------------------------------------------
		//Polar coordinates
		// a bit of white on vertical wire
		g.setColor(Color.white);
		g.drawLine(x[0]+state.s15,y[1]-state.s6,x[0]+state.s20,y[1]-state.s6);
		g.drawLine(x[0]+state.s15,y[1]-state.s7,x[0]+state.s20,y[1]-state.s7);
		g.drawLine(x[0]+state.s15,y[1]-state.s8,x[0]+state.s20,y[1]-state.s8);
		g.drawLine(x[0]+state.s15,y[1]-state.s9,x[0]+state.s20,y[1]-state.s9);
		g.drawLine(x[0]+state.s15,y[1]-state.s10,x[0]+state.s20,y[1]-state.s10);
		g.drawLine(x[0]+state.s15,y[1]-state.s11,x[0]+state.s20,y[1]-state.s11);
				
		g.setColor(Color.blue);
		//label r
		MaestroG.subscripter2("","R","",g,state.font14,x[0]+state.s20,y[1]-state.s27);
		//line down to x-y plane
		g.drawLine(x[0]+state.s50,y[1]-state.s30,x[0]+state.s50,y[1]+state.s70);
		//lines to x- and y-axis
		g.setColor(Color.lightGray);
		g.drawLine(x[0]+state.s50,y[1]+state.s70,x[0]-state.s70,y[1]+state.s70);
		g.drawLine(x[0]+state.s50,y[1]+state.s70,x[0]+state.s120,y[1]);
		//line connecting to center, broken to show I label
		//g.drawLine(x[0]+50,y[1]+70,x[0]+30,y[1]+42);
		
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(Color.red);
		//radial line to point P
		g.drawLine(x[0],y[1],x[0]+state.s50,y[1]-state.s30);
		
		//draw Field vectors
		g.setColor(Color.red);
		//E_r
		g.drawLine(x[0]+state.s50,y[1]-state.s30,x[0]+state.s62,y[1]-state.s10);
		g.drawLine(x[0]+state.s62,y[1]-state.s10,x[0]+state.s62,y[1]-state.s17);
		g.drawLine(x[0]+state.s62,y[1]-state.s10,x[0]+state.s55,y[1]-state.s13);
		
		//E_theta
		g.drawLine(x[0]+state.s50,y[1]-state.s30,x[0]+state.s70,y[1]-state.s42);
		g.drawLine(x[0]+state.s70,y[1]-state.s42,x[0]+state.s63,y[1]-state.s42);
		g.drawLine(x[0]+state.s70,y[1]-state.s42,x[0]+state.s67,y[1]-state.s36);
	
		g.setColor(Color.blue);
		//H_phi
		g.drawLine(x[0]+state.s50,y[1]-state.s28,x[0]+state.s65,y[1]-state.s32);
		g.drawLine(x[0]+state.s65,y[1]-state.s32,x[0]+state.s61,y[1]-state.s28);
		g.drawLine(x[0]+state.s65,y[1]-state.s32,x[0]+state.s60,y[1]-state.s33);
		
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                
                MaestroG.subscripter2("E","R","",g,state.font12,x[0]+state.s48,y[1]-state.s44);
                MaestroG.subscripter2("E","","",g,state.font12,x[0]+state.s65,y[1]-state.s5);
		g.setFont(new Font("Serif",Font.PLAIN,state.font12));
		g.drawString("\u03b8",x[0]+state.s73,y[1]);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		MaestroG.subscripter2("H","","",g,state.font12,x[0]+state.s68,y[1]-state.s22);
		g.setFont(new Font("Serif",Font.PLAIN,state.font12));
		g.drawString("\u03c6",x[0]+state.s77,y[1]-state.s17);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
		//point P
		g.setColor(Color.green);
		g.fillOval(x[0]+state.s47,y[1]-state.s33,state.s6,state.s6);
		g.setColor(Color.black);
		g.drawOval(x[0]+state.s47,y[1]-state.s33,state.s6,state.s6);
		
		//complete line connecting to center
		//a bit of white over wire
		g.setColor(Color.white);
		g.drawLine(x[0]+state.s15,y[1]+state.s18,x[0]+state.s20,y[1]+state.s18);
		g.drawLine(x[0]+state.s15,y[1]+state.s19,x[0]+state.s20,y[1]+state.s19);
		g.drawLine(x[0]+state.s15,y[1]+state.s20,x[0]+state.s20,y[1]+state.s20);
		g.drawLine(x[0]+state.s15,y[1]+state.s21,x[0]+state.s20,y[1]+state.s21);
		g.drawLine(x[0]+state.s15,y[1]+state.s22,x[0]+state.s20,y[1]+state.s22);
		g.drawLine(x[0]+state.s15,y[1]+state.s23,x[0]+state.s20,y[1]+state.s23);
				
		g.setColor(Color.gray);
		g.drawLine(x[0]+state.s50,y[1]+state.s70,x[0],y[1]);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        
		//Current label and arrow
		g.setFont(symbfont2);
		g.setColor(Color.red);
		g.drawLine(x[0]-state.s15,y[1]-state.s5,x[0]-state.s15,y[1]+state.s5);
		//MaestroG.drawArrow(x[0]-state.s15,y[1]-state.s5,5,g);
                MaestroG.drawArrowScaled(x[0]-state.s15,y[1]-state.s5, 1, state.sfactor, g);
		
		g.drawString("I",x[0]-state.s25,y[1]);
	    }
    }
    
    public synchronized void setFrequency(double frequency){
	this.frequency = frequency;
    }
    
    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 set3D(boolean Is3D){
	this.Is3D = Is3D;
    }
    
    public synchronized void setAntennaLength(double DipoleLength_lambda){
	this.DipoleLength_lambda = DipoleLength_lambda;
    }
    
    public synchronized void setAntennaRadius(double radius_lambda){
	this.radius_lambda = radius_lambda;
    }
    
    @Override
    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);
    }
    
    
    @Override
    public void update(Graphics g){
	paint(g);
    }
    
    public void mouseClicked(MouseEvent evt){
	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 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){}
}
