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

public class NewGuideCanvas extends Canvas {
    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 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, 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;
    NewGuide_State state;
    
    public NewGuideCanvas(NewGuide_State state){
	super();
        this.state = state;
        
        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);
    
	IsFocusOn = false;
	IsTopoOn = false;
	IsEfield = true;
	
	setBackground(bgcolor);
	x = new int[5];
	y = new int[5];
	
	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 = 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)));
	
        //Draw labels for axes
	g.setFont(new Font("SanSerif",Font.PLAIN,state.s12));
	FontMetrics fm = g.getFontMetrics();
	g.drawString("x",LeftMargin-fm.stringWidth("x")/2,getSize().height-BottomMargin-Lsize-state.s10);
	g.drawString("y",LeftMargin+Lsize+10+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){
		//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.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);
		
		g.setFont(new Font("SanSerif",Font.BOLD,state.font12));
		fm = g.getFontMetrics();
		g.setColor(Color.gray);
		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);
		
		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);
		//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("array 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;
                int fonto = state.font12;
	
		if(frequency < 1.0E3){
		    f_normalized = frequency;
		    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ Hz ]","",g,fonto,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,fonto,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,fonto,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,fonto,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,fonto,xf,yf);
		}
		else{
		    f_normalized = frequency/1.0E12;
		    MaestroG.special("","f"," =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]","",g,fonto,xf,yf);
		}
		//-----------------------------------------------------------------------------------
		String lambda, rho, psi, deg, delta;
		g.setFont(symbfont2);
		g.getFontMetrics();
		lambda="\u03bb";
		rho ="\u03c1";
		psi ="\u03c8";
		deg="\u00ba";
                delta="\u03b4";
		
	    if(IsFocusOn){
		//top piece of x-axis
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                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 + state.s2;
		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 = 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.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]+state.s38,y[1]+state.s10,x[0]+state.s31,y[1]+state.s20);
		g.drawLine(x[0]+state.s39,y[1]+state.s10,x[0]+state.s32,y[1]+state.s20);
		g.drawLine(x[0]+state.s28,y[1]+state.s8,x[0]+state.s31,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+2;
		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);
		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));
		g.drawString("\u03b8",x[0]+state.s3,y[1]-state.s13);
		g.drawString("\u03b8",x[0]+state.s25,y[1]-state.s68);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		g.drawString("= 0",x[0]+state.s37,y[1]-state.s68);
		g.setColor(Color.red);
		g.drawLine(x[0],y[1]-state.s67,x[0]+state.s20,y[1]-state.s72);
						
		g.setColor(Color.blue);
		g.setFont(new Font("Serif",Font.PLAIN,state.font15));
		g.drawString("\u03c6",x[0]-state.s9,y[1]+state.s25);
		g.drawString("\u03c6",x[0]-state.s49,y[1]+state.s75);
		g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		g.drawString("= 0",x[0]-state.s37,y[1]+state.s77);
		g.setColor(Color.red);
		g.drawLine(x[0]-state.s35,y[1]+state.s67,x[0]-state.s30,y[1]+state.s34);
		
		g.setColor(Color.black);
	        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		//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.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(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);
		//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);
		
		g.setColor(Color.black);
		g.drawLine(x[0]+state.s15,y[1]+state.s29,x[0]+state.s25,y[1]+state.s32);
		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.s37,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_ON);
                g.setFont(new Font("SanSerif",Font.PLAIN,state.font11));
                g.setColor(Color.black); 
		g.drawString("z",x[0]-state.s12,state.s20);
		g.drawString("x",x[0]-(tip-state.s12),y[1]+tip);
		g.drawString("y",x[2]+state.s10,y[1]-state.s9);
		
	    }
	    else{	
		
		MaestroG.subscripterIT("l","A","",g,state.font12,state.s5,state.s15);
                g.drawString("    = "+MaestroA.rounder(DipoleLength_lambda,4)+"  "+lambda,state.s10,state.s15);
		MaestroG.subscripterIT("l","B","",g,state.font12,state.s5,state.s33);
                g.drawString("    = "+MaestroA.rounder(DipoleLength2_lambda,4)+"  "+lambda,state.s10,state.s33);
		
		MaestroG.subscripter("d","","",g,state.font12,state.s10,state.s53);
		g.drawString("    = "+MaestroA.rounder(antenna_distance_lambda,4)+"  "+lambda,state.s10,state.s53);
		
		MaestroG.subscripter(""+delta,"","",g,state.font12,state.s10,state.s71);
		g.drawString("    = "+MaestroA.rounder(antenna_phase_degree,4)+" "+deg,state.s10,state.s71);
		
		
                int xCenter = x[0];
		int yCenter = y[1];
		int Radius = state.s15;
		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,state.font15));
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		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_ON);
    
                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.font12));
		
					
	    //draw antenna  -------------------------------
		//draw measurement arrows
		g.setColor(Color.gray);
		int xarrow = x[0]-state.s70;
		
		//top dipole left
		int xleft = x[0] - state.s20;
		int xright = x[0] + state.s20;
		g.setColor(Color.blue);
		g.drawString("d",x[0]-state.s3,y[1]+state.s43);
		    
		//distance arrow
		g.setColor(Color.gray);
		g.drawLine(xleft+5, y[1]+state.s45, xright-state.s5, y[1]+state.s45);
		//MaestroG.drawArrow(xleft+state.s12,y[1]+state.s45,8,g);
		//MaestroG.drawArrow(xright-state.s12,y[1]+state.s45,7,g);
		MaestroG.drawArrowScaled(xleft+state.s12,y[1]+state.s45,4,state.sfactor,g);
		MaestroG.drawArrowScaled(xright-state.s12,y[1]+state.s45,3,state.sfactor,g);
			
		//g.setColor(Color.white);
		//g.drawLine(xleft,y[1]-state.s50,xleft,y[1]-state.s5);
		//g.setColor(Color.red);
		//g.drawLine(xleft-state.s1,y[1]-state.s50,xleft-state.s1,y[1]-state.s5);
		//g.drawLine(xleft+state.s1,y[1]-state.s50,xleft+state.s1,y[1]-state.s5);
		//g.setColor(Color.gray);
		//g.drawLine(xleft+state.s2,y[1]-state.s50,xleft+state.s2,y[1]-state.s5);
		//tips
		g.setColor(Color.black);
		g.drawLine(xleft-state.s1,y[1]-state.s50,xleft+state.s2,y[1]-state.s50);
		g.drawLine(xleft-state.s1,y[1]-state.s5,xleft+state.s2,y[1]-state.s5);
		
		//bottom dipole
		//top dipole
		
                MaestroG.drawLineThick(g, xleft,y[1]+state.s50,xleft,y[1]+state.s6, 5, Color.red);
                MaestroG.drawLineThick(g, xleft,y[1]-state.s50,xleft,y[1]-state.s6, 5, Color.red);
                
                MaestroG.drawLineThick(g, xright,y[1]+state.s50,xright,y[1]+state.s6, 5, Color.green.darker());
                MaestroG.drawLineThick(g, xright,y[1]-state.s50,xright,y[1]-state.s6, 5, Color.green.darker());
                
                g.setColor(Color.black);
		g.setFont(new Font("SanSerif",Font.BOLD,state.font12));
		g.drawString("A",xleft-state.s3,y[1]+state.s65);
		g.drawString("B",xright-state.s3,y[1]+state.s65);
                //----------------------------------------------
                //draw axes 
                g.setColor(Color.gray);
		g.drawLine(x[0],state.s20,x[0],y[1]+state.s25); 
		//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.s15);
		
                g.setColor(Color.gray); 		
		g.drawLine(x[1]-state.s15,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);
                
		int tip = state.s87;
		g.setColor(Color.gray); 
                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);
                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);
	    
		//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]+state.s50,y[1]-state.s30);
		g.setColor(Color.blue);
		//label r
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		MaestroG.subscripter("R","","",g,state.font12,x[0]+state.s30,y[1]-state.s25);
		//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]);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                
                g.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
                //g.setFont(labfont);
                g.setColor(Color.black); 
		g.drawString("x",x[0]-(tip-state.s12),y[1]+tip);
                g.drawString("z",x[0]-state.s12,state.s20);
		g.drawString("y",x[2]+state.s10,y[1]-state.s9);
		
		//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]+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);
		
		//E_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_ON);
    
		MaestroG.subscripter2("E","R","",g,state.font12,x[0]+state.s44,y[1]-state.s44);
		MaestroG.subscripter2("E","\u03b8","",g,state.font12,x[0]+state.s65,y[1]-state.s5);
		MaestroG.subscripter2("E","\u03c6","",g,state.font12,x[0]+state.s68,y[1]-state.s22);
	    }
	    else{
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
                g.setColor(Color.blue);
		//H_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);
		
		//H_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);
				
		//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_ON);
    
		MaestroG.subscripter2("H","R","",g,state.font12,x[0]+state.s44,y[1]-state.s44);
		
                MaestroG.subscripter2("H","\u03b8","",g,state.font12,x[0]+state.s65,y[1]-state.s5);
		MaestroG.subscripter2("H","\u03c6","",g,state.font12,x[0]+state.s68,y[1]-state.s22);
            }
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                //point P
		g.setColor(Color.green);
		g.fillOval(x[0]+state.s48,y[1]-state.s32,state.s5,state.s5);
		g.setColor(Color.black);
		g.drawOval(x[0]+state.s48,y[1]-state.s32,state.s5,state.s5);
		
		g.setColor(Color.gray);
		g.drawLine(x[0]+state.s50,y[1]+state.s70,x[0],y[1]);
		
		//Current label and arrow
		g.setFont(labfont4);
		g.setColor(Color.red);
		g.drawLine(x[0]-state.s30,y[1]-state.s7,x[0]-state.s30,y[1]+state.s7);
                //MaestroG.drawArrow(x[0]-state.s30,y[1]-state.s7,5,g);
		MaestroG.drawArrowScaled(x[0]-state.s30,y[1]-state.s7,1,state.sfactor,g);
		
                g.drawLine(x[0]+state.s30,y[1]-state.s7,x[0]+state.s30,y[1]+state.s7);
		//MaestroG.drawArrow(x[0]+state.s30,y[1]-state.s7,5,g);
		MaestroG.drawArrowScaled(x[0]+state.s30,y[1]-state.s7,1,state.sfactor,g);
		
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                MaestroG.subscripter2("I","1","",g,state.font13,x[0]-state.s45,y[1]-state.s5);
		
		//g.drawString("I",x[0]-40,y[1]);
		MaestroG.subscripter2("I","2","",g,state.font13,x[0]+state.s37,y[1]-state.s5);
            }
    }

    
    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);
    }
    
}
