//package maestro.lib.graphics;
/**
 * NewGuideCanvas.java
 *
 *
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
//import maestro.lib.math.*;
//import maestro.lib.graphics.*;

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 static final int LeftMargin=20, RightMargin=10, TopMargin=10, BottomMargin=10;
    private static final Font labfont = new Font("SanSerif",Font.PLAIN,10);
    private static final Font symbfont= new Font("Serif",Font.PLAIN,12);
    private static final Font symbfont2= new Font("Serif",Font.PLAIN,14);
    private static final Font labfont2= new Font("Serif",Font.BOLD,16);
    private static final Font labfont4= new Font("Serif",Font.PLAIN,12);
    private static final 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, DipoleLength2_lambda, wavelength, radius_lambda, antenna_distance_lambda, antenna_phase_degree;
    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 Image im;
    private Graphics buf;
    private String stmp;
    private double temp;
    private boolean IsFocusOn, IsTopoOn;
    public boolean Is3D, IsEfield;
    
    public NewGuideCanvas(){
	super();
	IsFocusOn = false;
	IsTopoOn = false;
	IsEfield = true;
	
	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;
	DipoleLength2_lambda = 0.5;
	radius_lambda = 1.0E-5;
	antenna_distance_lambda = 0.5; //wavelengths
	antenna_phase_degree = 0.0;
	
	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){
	double theta = Math.PI/6.0;
	int ArrowPos1, ArrowPos2;
	int Lsize = 30;
	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)));
	//Draw labels for axes
	g.setFont(new Font("SanSerif",Font.PLAIN,12));
	FontMetrics fm = g.getFontMetrics();
	g.drawString("x",LeftMargin-fm.stringWidth("x")/2,getSize().height-BottomMargin-Lsize-10);
	g.drawString("y",LeftMargin+Lsize+10+fm.stringWidth("y"),
		getSize().height-BottomMargin+fm.getHeight()/3);
	
	g.drawString("z",LeftMargin+(int)(Lsize*Math.cos(theta))-15,getSize().height-BottomMargin-(int)(Lsize*Math.sin(theta))+2);
    }
    
    private void drawWire(int x1, int y1, int x2, int y2, int mode, Graphics g){
		//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] + 10;
		//g.setColor(Color.white);
		g.setColor(Color.gray);
		g.drawLine(x[4],ytmp,x[2]-5,ytmp);
		g.drawLine(x[2],ytmp-4,x[2],ytmp+4);
		g.drawLine(x[3],ytmp-4,x[3],ytmp+4);
		g.drawLine(x[2],y[0]+15,x[2],y[4]-30);
		//Arrowhead
		MaestroG.drawArrow(x[2],y[4]-30,5,g);
		MaestroG.drawArrow(x[4],ytmp,7,g);
		
		g.setFont(new Font("SanSerif",Font.BOLD,12));
		fm = g.getFontMetrics();
		g.setColor(Color.gray);
		g.drawString("0",x[2]-2,ytmp+fm.getHeight()+2);
		g.drawString("L",x[3]-3,ytmp+fm.getHeight()+2);
		g.setFont(new Font("SanSerif",Font.PLAIN,12));
		g.drawString("z",x[4]+5,ytmp+fm.getHeight());
		g.drawString("y",x[2]+8,y[4]-30);
		
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    
    }
	
    private void draw3D(Graphics g){    
	
	//Draw the background
		Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
		g.setColor(Color.black);
		//g.drawLine(0,getSize().height-1,getSize().width-1,getSize().height-1);
		g.drawLine(getSize().width-1,0,getSize().width-1,getSize().height-1);
		//g.setColor(Color.white);
		
		//g.drawLine(0,0,getSize().width-1,0);
		//g.drawLine(0,0,0,getSize().height-1);

		//set the arrays
		x[0] = getSize().width/2-30;
		x[1] = x[0] - 7;
		x[2] = getSize().width-50;
		
		y[0] = getSize().height-20;
		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,10));
		//---------------------------------------------------------
		if(IsTopoOn){
		    if(!IsFocusOn){
			g.setColor(Color.blue);
			g.drawString("Click to show ",getSize().width*2/3+2,getSize().height*3/4);
			g.drawString("angles reference",getSize().width*2/3+2,getSize().height*3/4+11);
		    }
		    else{
			g.setColor(Color.red);
			g.drawString("Click to show ",getSize().width*2/3+2,getSize().height*3/4);
			g.drawString("array diagram",getSize().width*2/3+2,getSize().height*3/4+11);
			
		    }
		}
		//----------------------------------------------------------
		g.setColor(Color.black);
		g.setFont(new Font("SanSerif",Font.PLAIN,12));
		int xf = x[0]+30;
		int yf = getSize().height-5;
		double f_normalized;
	
		if(frequency < 1.0E3){
		    f_normalized = frequency;
		    //g.drawString("f =  "+MaestroA.rounder(f_normalized,6)+"   [ Hz ]",xf,yf);	
                    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ Hz ]","",g,12,xf,yf);	
		}
		else if(frequency < 1.0E6 && frequency >= 1.0E3  ){
		    f_normalized = frequency/1.0E3;
		    //g.drawString("f  =  "+MaestroA.rounder(f_normalized,6)+"   [ kHz ]",xf,yf);	
                    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ kHz ]","",g,12,xf,yf);
		}
		else if(frequency < 1.0E9 && frequency >= 1.0E6 ){
		    f_normalized = frequency/1.0E6;
		    //g.drawString("f  =  "+MaestroA.rounder(f_normalized,6)+"   [ MHz ]",xf,yf);	
                    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ MHz ]","",g,12,xf,yf);
		}
		else if(frequency < 1.0E12 && frequency >= 1.0E9 ){
		    f_normalized = frequency/1.0E9;
		    //g.drawString("f  =  "+MaestroA.rounder(f_normalized,6)+"   [ GHz ]",xf,yf);
                    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ GHz ]","",g,12,xf,yf);
		}
		else if(frequency < 1.0E15 && frequency >= 1.0E12 ){
		    f_normalized = frequency/1.0E12;
		    //g.drawString("f  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]",xf,yf);	
                    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]","",g,12,xf,yf);
		}
		else{
		    f_normalized = frequency/1.0E12;
		    //g.drawString("f  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]",xf,yf);
                    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]","",g,12,xf,yf);
		}
		//-----------------------------------------------------------------------------------
		String lambda, rho, psi, deg, delta;
		g.setFont(symbfont2);
		g.getFontMetrics();
		lambda="\u03bb";
		rho ="\u03c1";
		psi ="\u03c8";
		deg="\u00ba";
                delta="\u03b4";
		
		/*
		if(radius_lambda > (DipoleLength_lambda/10.0)){
		  if(!IsFocusOn){
		    g.setColor(Color.red);
		    g.drawString("WARNING: "+rho+" > 0.1 L",x[0]+20,30);
		  }  
		}
		*/
		
	    if(IsFocusOn){
		//top piece of x-axis
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setColor(Color.gray);
		int tip = 37;
		g.drawLine(x[0]+tip-6,y[1]-tip+6,x[0]+tip,y[1]-tip);
		
		//Arc for phi
		int xCenter = x[0];
		int Radius = 65;
		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 = 65;
		xCenter = x[0] + Radius/2+2;
		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 = 62+56;
		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.drawArc(xCenter-Radius,yCenter-Radius,raio/2,raio,beginAngle,Angle);
		g.drawArc(xCenter-Radius,yCenter-Radius,raio/2-1,raio-1,beginAngle,Angle);
		
		g.drawLine(x[0]+38,y[1]+10,x[0]+31,y[1]+20);
		g.drawLine(x[0]+39,y[1]+10,x[0]+32,y[1]+20);
		g.drawLine(x[0]+28,y[1]+8,x[0]+31,y[1]+20);
		g.drawLine(x[0]+29,y[1]+8,x[0]+32,y[1]+20);			
				
		g.setColor(Color.black);
		
		//Arc for phi
		xCenter = x[0];
		yCenter = y[1]+ Radius/2+2;
		Radius = 65;
		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);
		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 = 65;
		xCenter = x[0] + Radius/2+2;
		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,15));
		g.drawString("\u03b8",x[0]+3,y[1]-13);
		g.drawString("\u03b8",x[0]+25,y[1]-68);
		g.setFont(new Font("SanSerif",Font.PLAIN,12));
		g.drawString("= 0",x[0]+37,y[1]-68);
		g.setColor(Color.red);
		g.drawLine(x[0],y[1]-67,x[0]+20,y[1]-72);
						
		g.setColor(Color.blue);
		g.setFont(new Font("Serif",Font.PLAIN,15));
		g.drawString("\u03c6",x[0]-9,y[1]+25);
		g.drawString("\u03c6",x[0]-49,y[1]+75);
		g.setFont(new Font("SanSerif",Font.PLAIN,12));
		g.drawString("= 0",x[0]-37,y[1]+77);
		g.setColor(Color.red);
		g.drawLine(x[0]-35,y[1]+67,x[0]-30,y[1]+34);
		
		g.setColor(Color.black);
	        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		//draw axes
		g.setColor(Color.gray);
		g.drawLine(x[0],20,x[0],y[0]); 
		MaestroG.drawArrow(x[0],20,5,g);
		g.setColor(new Color(210,210,210));
		g.drawLine(x[0],y[1],x[0],y[1]+35);
                
                g.setColor(Color.gray); 
		g.drawLine(x[1]-70,y[1],x[2]+10,y[1]);
		MaestroG.drawArrow(x[2]+10,y[1],7,g);
		g.setColor(new Color(210,210,210));
		g.drawLine(x[0],y[1],x[0]+33,y[1]);
		g.drawLine(x[0],y[1],x[0]+28,y[1]-28);
		
		g.setColor(Color.gray);
		tip = 87;
		g.drawLine(x[0],y[1],x[0]-tip,y[1]+tip);
		//draw oblique arrow head
		MaestroG.drawArrow(x[0]-tip+5,y[1]+tip-5,10,g);
                Polygon p = new Polygon();
		p.addPoint(x[0]-tip,y[1]+tip);
		p.addPoint(x[0]-(tip-2),y[1]+(tip-6));
		p.addPoint(x[0]-(tip-6),y[1]+(tip-2));
		//g.drawPolygon(p);
		//g.fillPolygon(p);
		
		g.setColor(Color.black);
		g.drawLine(x[0]+15,y[1]+29,x[0]+25,y[1]+32);
		g.drawLine(x[0]+15,y[1]+28,x[0]+25,y[1]+31);
		g.drawLine(x[0]+15,y[1]+37,x[0]+25,y[1]+31);
		g.drawLine(x[0]+15,y[1]+38,x[0]+25,y[1]+32);
		
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.setFont(new Font("SanSerif",Font.PLAIN,11));
                g.setColor(Color.black); 
		g.drawString("z",x[0]-12,20);
		g.drawString("x",x[0]-(tip-12),y[1]+tip);
		g.drawString("y",x[2]+10,y[1]-9);
		
	    }
	    else{	
		
		MaestroG.subscripterIT("l","A","",g,12,5,15);
                g.drawString("    = "+MaestroA.rounder(DipoleLength_lambda,4)+"  "+lambda,10,15);
		MaestroG.subscripterIT("l","B","",g,12,5,33);
                g.drawString("    = "+MaestroA.rounder(DipoleLength2_lambda,4)+"  "+lambda,10,33);
		
		MaestroG.subscripter("d","","",g,12,10,53);
		g.drawString("    = "+MaestroA.rounder(antenna_distance_lambda,4)+"  "+lambda,10,53);
		
		MaestroG.subscripter(""+delta,"","",g,12,10,71);
		g.drawString("    = "+MaestroA.rounder(antenna_phase_degree,4)+" "+deg,10,71);
		
		
                int xCenter = x[0];
		int yCenter = y[1];
		int Radius = 15;
		int beginAngle = 28;
		int Angle = 62;
		int raio = 2*Radius;
		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,15));
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.drawString("\u03b8",x[0]+5,y[1]-16);
		g.setFont(new Font("SanSerif",Font.PLAIN,12));
		g.setColor(Color.black);
		
		//Arc for phi
		xCenter = x[0];
		yCenter = y[1];
		Radius = 15;
		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_ON);
    
                g.setColor(Color.blue);
		g.setFont(new Font("Serif",Font.PLAIN,15));
		g.drawString("\u03c6",x[0]-14,y[1]+25);
		g.setFont(new Font("SanSerif",Font.PLAIN,12));
		
					
	    //draw antenna  -------------------------------
		//draw measurement arrows
		g.setColor(Color.gray);
		int xarrow = x[0]-70;
		
		//top dipole left
		int xleft = x[0] - 20;
		int xright = x[0] + 20;
		g.setColor(Color.blue);
		g.drawString("d",x[0]-3,y[1]+43);
		    
		//distance arrow
		g.setColor(Color.gray);
		g.drawLine(xleft+5, y[1]+45, xright-5, y[1]+45);
		MaestroG.drawArrow(xleft+12,y[1]+45,8,g);
		MaestroG.drawArrow(xright-12,y[1]+45,7,g);
			
		g.setColor(Color.white);
		g.drawLine(xleft,y[1]-50,xleft,y[1]-5);
		g.setColor(Color.red);
		g.drawLine(xleft-1,y[1]-50,xleft-1,y[1]-5);
		g.drawLine(xleft+1,y[1]-50,xleft+1,y[1]-5);
		g.setColor(Color.gray);
		g.drawLine(xleft+2,y[1]-50,xleft+2,y[1]-5);
		//tips
		g.setColor(Color.black);
		g.drawLine(xleft-1,y[1]-50,xleft+2,y[1]-50);
		g.drawLine(xleft-1,y[1]-5,xleft+2,y[1]-5);
		
		//bottom dipole
		//top dipole
		
		g.setColor(Color.white);
		g.drawLine(xleft,y[1]+50,xleft,y[1]+6);
		g.setColor(Color.red);
		g.drawLine(xleft-1,y[1]+50,xleft-1,y[1]+6);
		g.drawLine(xleft+1,y[1]+50,xleft+1,y[1]+6);
		g.setColor(Color.gray);
		g.drawLine(xleft+2,y[1]+50,xleft+2,y[1]+6);
		//tip down
		g.setColor(Color.black);
		g.drawLine(xleft-1,y[1]+50,xleft+2,y[1]+50);
		g.drawLine(xleft-1,y[1]+5,xleft+2,y[1]+5);
		
		//top dipole right
		g.setColor(Color.white);
		g.drawLine(xright,y[1]-50,xright,y[1]-5);
		g.setColor(Color.green);
		g.drawLine(xright-1,y[1]-50,xright-1,y[1]-5);
		g.drawLine(xright+1,y[1]-50,xright+1,y[1]-5);
		g.setColor(Color.gray);
		g.drawLine(xright+2,y[1]-50,xright+2,y[1]-5);
		//tips
		g.setColor(Color.black);
		g.drawLine(xright-1,y[1]-50,xright+2,y[1]-50);
		g.drawLine(xright-1,y[1]-5,xright+2,y[1]-5);
		
		//bottom dipole 
		
		g.setColor(Color.white);
		g.drawLine(xright,y[1]+50,xright,y[1]+6);
		g.setColor(Color.green);
		g.drawLine(xright-1,y[1]+50,xright-1,y[1]+6);
		g.drawLine(xright+1,y[1]+50,xright+1,y[1]+6);
		g.setColor(Color.gray);
		g.drawLine(xright+2,y[1]+50,xright+2,y[1]+6);
		//tips
		g.setColor(Color.blue);
		g.drawLine(xright-1,y[1]+50,xright+2,y[1]+50);
		g.drawLine(xright-1,y[1]+5,xright+2,y[1]+5);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		g.drawString("A",xleft-3,y[1]+65);
		g.drawString("B",xright-3,y[1]+65);
		
	    //----------------------------------------------
	    
		//draw axes 
                
                g.setColor(Color.gray);
		g.drawLine(x[0],20,x[0],y[1]+25); 
		MaestroG.drawArrow(x[0],20,5,g);
		
                g.setColor(new Color(210,210,210));
		g.drawLine(x[0],y[1],x[0],y[1]+15);
		
                g.setColor(Color.gray); 		
		g.drawLine(x[1]-15,y[1],x[2]+10,y[1]);
		MaestroG.drawArrow(x[2]+10,y[1],7,g);
		
		int tip = 87;
		g.setColor(Color.gray); 
                g.drawLine(x[0],y[1],x[0]-tip,y[1]+tip);
		//g.drawString("x'",x[0]-(tip-12),y[1]+tip);
		//draw oblique arrow head
                MaestroG.drawArrow(x[0]-tip+5,y[1]+tip-5,10,g);
                
		Polygon p = new Polygon();
		p.addPoint(x[0]-tip,y[1]+tip);
		p.addPoint(x[0]-(tip-2),y[1]+(tip-6));
		p.addPoint(x[0]-(tip-6),y[1]+(tip-2));
		//g.drawPolygon(p);
		//g.fillPolygon(p);
	    
		//Polar coordinates
		// a bit of white on vertical wire
		//g.setColor(Color.white);
		//g.drawLine(x[0]+15,y[1]-6,x[0]+20,y[1]-6);
		//g.drawLine(x[0]+15,y[1]-7,x[0]+20,y[1]-7);
		//g.drawLine(x[0]+15,y[1]-8,x[0]+20,y[1]-8);
		//g.drawLine(x[0]+15,y[1]-9,x[0]+20,y[1]-9);
		//g.drawLine(x[0]+15,y[1]-10,x[0]+20,y[1]-10);
		//g.drawLine(x[0]+15,y[1]-11,x[0]+20,y[1]-11);
		//g.drawLine(x[0]+15,y[1]-12,x[0]+20,y[1]-12);
				
		g.setColor(Color.black);
		//radial line to point P
		g.drawLine(x[0],y[1],x[0]+50,y[1]-30);
		g.setColor(Color.blue);
		//label r
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		MaestroG.subscripter("R","","",g,12,x[0]+30,y[1]-25);
		//line down to x-y plane
		g.drawLine(x[0]+50,y[1]-30,x[0]+50,y[1]+70);
		//lines to x- and y-axis
		g.setColor(Color.lightGray);
		g.drawLine(x[0]+50,y[1]+70,x[0]-70,y[1]+70);
		g.drawLine(x[0]+50,y[1]+70,x[0]+120,y[1]);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                
                g.setFont(labfont);
                g.setColor(Color.black); 
		g.drawString("x",x[0]-(tip-12),y[1]+tip);
                g.drawString("z",x[0]-12,20);
		g.drawString("y",x[2]+10,y[1]-9);
		
		//line connecting to center, broken to show I label
		//g.drawLine(x[0]+50,y[1]+70,x[0]+30,y[1]+42);
		
		
		//draw Field vectors
		
	    if(IsEfield){	
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
                g.setColor(Color.red);
		//E_r
		
                g.drawLine(x[0]+50,y[1]-30,x[0]+62,y[1]-10);
		g.drawLine(x[0]+62,y[1]-10,x[0]+62,y[1]-17);
		g.drawLine(x[0]+62,y[1]-10,x[0]+55,y[1]-13);
		
		//E_theta
		g.drawLine(x[0]+50,y[1]-30,x[0]+70,y[1]-42);
		g.drawLine(x[0]+70,y[1]-42,x[0]+63,y[1]-42);
		g.drawLine(x[0]+70,y[1]-42,x[0]+67,y[1]-36);
		
		//E_phi
		g.drawLine(x[0]+50,y[1]-28,x[0]+65,y[1]-32);
		g.drawLine(x[0]+65,y[1]-32,x[0]+61,y[1]-28);
		g.drawLine(x[0]+65,y[1]-32,x[0]+60,y[1]-33);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		MaestroG.subscripter2("E","R","",g,12,x[0]+44,y[1]-44);
		MaestroG.subscripter2("E","\u03b8","",g,12,x[0]+65,y[1]-5);
		//g.setFont(new Font("Serif",Font.PLAIN,12));
		//g.drawString("\u03b8",x[0]+73,y[1]);
		//g.setFont(new Font("SanSerif",Font.PLAIN,12));
                MaestroG.subscripter2("E","\u03c6","",g,12,x[0]+68,y[1]-22);
		//g.setFont(new Font("Serif",Font.PLAIN,12));
		//g.drawString("\u03c6",x[0]+77,y[1]-17);
		//g.setFont(new Font("SanSerif",Font.PLAIN,12));
	    }
	    else{
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
                g.setColor(Color.blue);
		//H_r
		g.drawLine(x[0]+50,y[1]-30,x[0]+62,y[1]-10);
		g.drawLine(x[0]+62,y[1]-10,x[0]+62,y[1]-17);
		g.drawLine(x[0]+62,y[1]-10,x[0]+55,y[1]-13);
		
		//H_theta
		g.drawLine(x[0]+50,y[1]-30,x[0]+70,y[1]-42);
		g.drawLine(x[0]+70,y[1]-42,x[0]+63,y[1]-42);
		g.drawLine(x[0]+70,y[1]-42,x[0]+67,y[1]-36);
				
		//H_phi
		g.drawLine(x[0]+50,y[1]-28,x[0]+65,y[1]-32);
		g.drawLine(x[0]+65,y[1]-32,x[0]+61,y[1]-28);
		g.drawLine(x[0]+65,y[1]-32,x[0]+60,y[1]-33);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		MaestroG.subscripter2("H","R","",g,12,x[0]+44,y[1]-44);
		
                MaestroG.subscripter2("H","\u03b8","",g,12,x[0]+65,y[1]-5);
		//g.setFont(new Font("Serif",Font.PLAIN,12));
		//g.drawString("\u03b8",x[0]+73,y[1]);
		//g.setFont(new Font("SanSerif",Font.PLAIN,12));
		
                MaestroG.subscripter2("H","\u03c6","",g,12,x[0]+68,y[1]-22);
		//g.setFont(new Font("Serif",Font.PLAIN,12));
		//g.drawString("\u03c6",x[0]+77,y[1]-17);
		//g.setFont(new Font("SanSerif",Font.PLAIN,12));
	    
            }
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                //point P
		g.setColor(Color.green);
		g.fillOval(x[0]+48,y[1]-32,5,5);
		g.setColor(Color.black);
		g.drawOval(x[0]+48,y[1]-32,5,5);
		
		g.setColor(Color.gray);
		g.drawLine(x[0]+50,y[1]+70,x[0],y[1]);
		
		//Current label and arrow
		g.setFont(labfont4);
		g.setColor(Color.red);
		g.drawLine(x[0]-30,y[1]-7,x[0]-30,y[1]+7);
                
                MaestroG.drawArrow(x[0]-30,y[1]-7,5,g);
		g.drawLine(x[0]+30,y[1]-7,x[0]+30,y[1]+7);
		MaestroG.drawArrow(x[0]+30,y[1]-7,5,g);
		
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                
		//g.drawString("I",x[0]-40,y[1]);
		MaestroG.subscripterS("I","1","",g,12,x[0]-43,y[1]-5);
		
		//g.drawString("I",x[0]-40,y[1]);
		MaestroG.subscripterS("I","2","",g,12,x[0]+37,y[1]-5);
                
            }
    }

    
    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 setField(boolean IsEfield){
	this.IsEfield = IsEfield;
    }
    
    public synchronized void setAntennaLength(double DipoleLength_lambda){
	this.DipoleLength_lambda = DipoleLength_lambda;
    }
    
    public synchronized void setAntennaLength2(double DipoleLength2_lambda){
	this.DipoleLength2_lambda = DipoleLength2_lambda;
    }
    
    public synchronized void setAntennaRadius(double radius_lambda){
	this.radius_lambda = radius_lambda;
    }
    
    public synchronized void setAntennaDistance(double antenna_distance_lambda){
	this.antenna_distance_lambda = antenna_distance_lambda;
    }
    
    public synchronized void setAntennaPhase(double antenna_phase_degree){
	this.antenna_phase_degree = antenna_phase_degree;
    }
       
    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){
	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){}
}
