import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;

public class NewGuideCanvas2 extends Canvas implements MouseListener{
    private static final Color ccolor1 = new Color(50,204,153);
    private static final Color bgcolor = Color.white;
    private static final Color bgcolor2 = new Color(255,255,250);
    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 symbolfont= new Font("Serif",Font.PLAIN,16);
    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 static final Font Jfont = new Font("Serif",Font.ITALIC,26);
    private static final Font Jfont2 = new Font("SanSerif",Font.ITALIC,18);
    
    private int yBottom;
    private int x[], y[];
    private double frequency, epsilon_r, epsilon_r0, mu_r, phase_velocity, phase_velocity0;
    private double wavelength, distance, point_distance, theta_angle, phi_angle;
    
    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;
    private boolean planeH = false;
    
    public boolean Is3D;
   
    
    public NewGuideCanvas2(){
	super();
	IsFocusOn = false;
	IsTopoOn = false;
	setBackground(bgcolor2);
	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;
	phi_angle = 45.0;//degrees
	theta_angle = 45.0;//degrees
	point_distance = 10.0;//wavelengths
	
	Is3D = true;
		
	wavelength = light_velocity/frequency/Math.sqrt(epsilon_r);
	distance = point_distance * wavelength;
    }

    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,1,g);
	//Horizontal axis
	g.drawLine(LeftMargin,getSize().height-BottomMargin,LeftMargin+Lsize,getSize().height-BottomMargin);
	//Draw up arrow
	MaestroG.drawArrow(LeftMargin+Lsize,getSize().height-BottomMargin,3,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){
		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);
		}
		g.setColor(Color.white);
	}

	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){
		Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(c);
		g.drawOval(x-radius,y-radius,2*radius,2*radius);
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    
	}
	private void fillCircle(int x, int y, int radius, Color c, Graphics g){
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.setColor(c);
		g.fillOval(x-radius,y-radius,2*radius,2*radius);
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	}
	
	private void drawEllipse(int x, int y, int radius1, int radius2, Color c, Graphics g){
		Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(c);
		g.drawOval(x-radius1,y-radius2,2*radius1,2*radius2);
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    
	}

	
	
	private void draw3D(Graphics g){    
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		wavelength = light_velocity/frequency/Math.sqrt(epsilon_r);
		distance = point_distance * wavelength;
		
                g.clearRect(0,0,getSize().width-1,getSize().height-1);
		
		//Draw the background	
		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;
		y[0] = getSize().height/2;

		
		
	    //if(IsFocusOn){
		
	    //}
	    //else
		{	
		
		String phi, theta, deg, lambda;
		
		//g.setColor(Color.red);
		g.setColor(new Color(150,0,0));
		g.setFont(symbolfont);
		g.getFontMetrics();
		lambda="\u03bb";
		theta="\u03b8";
		phi="\u03c6";
		deg="\u00ba";
		g.setFont(labfont4);
		g.getFontMetrics();
		
		int xc = x[0]+x[0]*3/4;
		int yc = 30;
	
		xc = x[0]+x[0]*3/4;
		
                g.setFont(new Font("SanSerif",Font.PLAIN,12));
                
		int radius = getSize().height/2-40;
		int radius1 = radius;
		int axis = getSize().height*5/12;
		int radius2 = radius/3;
		Color colore1 = new Color(230,230,230);
		Color colore2 = new Color(120,120,120);
		Color colore3 = new Color(250,10,10);
		Color colore4 = new Color(250,190,190);
		Color colore5 = Color.red;
		Color colore6 = new Color(190,190,190);
		Color colore7 = new Color(200,255,255);
		Color colore8 = new Color(220,255,255);
		Color colore9 = new Color(155,155,255);
		Color colore10 = new Color(255,220,0);
		Color colore11 = new Color(200,200,200);
		
        if(planeH){
                
                //draw Equator
		drawEllipse(x[0],y[0],radius1,radius2,colore1,g);
		
                //draw other axes
		g.setColor(Color.black);
		// y axis
		g.drawLine(x[0],y[0],x[0]+axis,y[0]);	
		MaestroG.drawArrow(x[0]+axis,y[0],7,g);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setFont(Jfont2);
                g.drawString("y",x[0]+axis,y[0]-10);
		
                int tip = y[0]*3/7;
		int ytip= y[0]*2/11;
                
                int rule; float alpha;
                rule = AlphaComposite.SRC_OVER;
                alpha = 0.3f;
                g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
                Polygon p = new Polygon();
		p.addPoint(x[0]-tip,y[0]-ytip-radius1-3);
		p.addPoint(x[0]+tip,y[0]+ytip-radius1-3);
                p.addPoint(x[0]+tip,y[0]+ytip+radius1+3);
                p.addPoint(x[0]-tip,y[0]+-ytip+radius1+3);
                
                Polygon p2 = new Polygon();
		p2.addPoint(x[0]-35*ytip/10-radius1, y[0]-tip/2-3);
		p2.addPoint(x[0]+radius1-ytip, y[0]-tip/2-3);
		p2.addPoint(x[0]+35*ytip/10+radius1, y[0]+tip/2+3);
		p2.addPoint(x[0]-radius1+ytip, y[0]+tip/2+3);
		
                    //g.setColor(Color.yellow);
                    g.setColor(new Color(250,250,150));
                    g.fillPolygon(p2);
                    g.setColor(Color.red);
                    g.drawPolygon(p2);
                
                g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
        
		//draw circular meridian (outer circle of picture) 
		drawCircle(x[0],y[0],radius+1,colore2,g);
		
		// draw latitude ellipse
		int latitude;
		double costheta = Math.cos(theta_angle*Math.PI/180.);
		double sintheta = Math.sin(theta_angle*Math.PI/180.);
		latitude = getSize().height/2 - (int)(radius * costheta);
		//drawEllipse(x[0],latitude,(int)(radius1 * sintheta),(int)(radius2 * sintheta),colore4,g);
		
		// draw longitude
		double phiref0, phiref1, phiref2, phiref3, phinew;
		int newradius1;
		phiref0 = -108.0;
		phiref1 = - 70.0;
		phiref2 = 70.0;
		phiref3 = 180.0;
		phinew = 0.0;
			
		if(phi_angle >= 0.0 && phi_angle <= 90.0){
		    phinew = phiref0 + phi_angle *108.0/90.0;
		}
		
		else if(phi_angle > 90.0 && phi_angle <= 180.0){
		    phinew = (phi_angle-90.0) *70.0/90.0;
		}
		else if(phi_angle > 180.0 && phi_angle <= 270.0){
		    phinew = phiref2 + (phi_angle-180.0) *110.0/90.0;
		}
		else if(phi_angle > 270.0 && phi_angle <= 360.0){
		    phinew = phiref3 + (phi_angle-270.0) *72.0/90.0;
		}
		newradius1 =(int)(radius * Math.cos(phinew*Math.PI/180.));
		
		int x2, y2, x3, y3, x4, y4, x7, y7;
		double phinew_rad = phinew*Math.PI/180.0;
		x2 = (int)(x[0] + radius1 * sintheta * Math.cos(phinew_rad));
		y2 = (int)(latitude + radius2 * sintheta * Math.sin(-phinew_rad));
		x3 = (int)(x[0] + radius1 * Math.cos(phinew_rad));
		y3 = (int)(y[0] + radius2 * Math.sin(-phinew_rad));
		x4 = x2;
		y4 = (int)((double)x4/(double)x3*(double)y3);
		
                x7 = (int)(x[0] - radius1 * Math.cos(phinew_rad));
		y7 = (int)(y[0] - radius2 * Math.sin(-phinew_rad));
		
                g.setColor(Color.black);
		
		//draw z axis
		g.drawLine(x[0],15,x[0],y[0]); 
		MaestroG.drawArrow(x[0],15,5,g);
		g.setFont(Jfont2);
                g.drawString("z",x[0]+9,20);
		g.drawLine(x[0],y[0]+10,x[0],y[0]+radius+10); 
                
                g.drawLine(x[0],y[0]+10,x[0],y[0]);
                
                int updown = 60;
                int udx = 5;
                int udy = 10;
		
                MaestroG.drawLineThick(g,x[0],y[0]-updown,x[0],y[0]+updown,5,Color.red);
		MaestroG.drawLineThick(g,x[0],y[0]-updown,x[0]-udx,y[0]-updown+udy,5,Color.red);
                MaestroG.drawLineThick(g,x[0],y[0]-updown,x[0]+udx,y[0]-updown+udy,5,Color.red);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setColor(Color.red);
                g.setFont(Jfont);
                g.drawString("I",x[0]-6*udx,y[0]-updown+6*udy);
                g.drawLine(x[0]-6*udx,y[0]-updown+4*udy-3,x[0]-3*udx,y[0]-updown+4*udy-3);
                MaestroG.drawArrow(x[0]-3*udx-5,y[0]-updown+4*udy-3,7,g);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                //draw other axes
		g.setColor(Color.black);
		
		// x axis
		g.drawLine(x[0],y[0],x[0]-tip,y[0]+tip);
		g.setFont(Jfont2);
                g.drawString("x",x[0]-(tip-5),y[0]+tip+10);
		//draw oblique arrow head
                MaestroG.drawArrow(x[0]-tip+5,y[0]+tip-5,10,g);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		//connect center to scanned point P (green dot)	
		g.setColor(Color.blue);
		g.drawLine(x[0],y[0],x3,y3);
		
			
		g.setColor(colore2);
		g.drawArc(x[0]-radius1,y[0]-27,2*radius1,2*27,180,180);
		g.drawArc(x[0]-50,y[0]-radius1,2*50,2*radius1,90,-180);
		
		
		//Scanned point P (green circle)
		if(phi_angle <=90 || phi_angle >=270){
		    fillCircle(x3,y3,4,Color.green,g);
		    drawCircle(x3,y3,4,Color.black,g);
		}
                
                // draw electric field vector components
                //H_theta
                MaestroG.drawLineThick(g,x3,y3,x3+32,y3-5,3,Color.blue.darker());
		MaestroG.drawLineThick(g,x3+32,y3-5,x3+26,y3-6,3,Color.blue.darker());
		MaestroG.drawLineThick(g,x3+32,y3-5,x3+26,y3-1,3,Color.blue.darker());
		
                //E_theta
                MaestroG.drawLineThick(g,x3,y3,x3-3,y3+38,3,Color.red.darker());
		MaestroG.drawLineThick(g,x3-3,y3+38,x3-5,y3+31,3,Color.red.darker());
		MaestroG.drawLineThick(g,x3-3,y3+38,x3-0,y3+31,3,Color.red.darker());
		
                //E_r
                MaestroG.drawLineThick(g,x3,y3,x3+35,y3+14,3,Color.green.darker());
		MaestroG.drawLineThick(g,x3+35,y3+14,x3+30,y3+9,3,Color.green.darker());
		MaestroG.drawLineThick(g,x3+35,y3+14,x3+28,y3+14,3,Color.green.darker());
		
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(Color.blue.darker());
                MaestroG.subscripterItalic("H","\u03c6","",g,14,x3+37,y3-1);
		g.setColor(Color.red.darker());
                MaestroG.subscripterItalic("E","\u03b8","",g,14,x3+1,y3+43);
		g.setColor(Color.green.darker());
                MaestroG.subscripterItalic("E","R","",g,14,x3+32,y3+29);
		
                //draw phi angle
                alpha = 0.8f;
                g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
		g.setColor(Color.yellow);
		g.fillArc(x[0]-23,y[0]-10,46,20,-108,108+(int)(phinew));
		g.setColor(new Color(155,5,155));
		g.drawArc(x[0]-23,y[0]-10,46,20,-108,108+(int)(phinew));
		
                //alpha = 1.0f;
                //g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
		//draw theta angle
		g.setColor(Color.cyan);
		g.fillArc(x[0]-13,y[0]-22,28,46,90,-102);
		g.setColor(new Color(155,5,155));
		g.drawArc(x[0]-13,y[0]-22,28,46,90,-102);
                
                
                
                //draw phi symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("\u03c6",x[0]+6,y[0]+22);

                //draw theta symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("\u03b8",x[0]+18,y[0]-7);
                
                //draw r symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("R",x[0]+35,y[0]+15);
                
	}
        else{
        
            //draw Equator
		drawEllipse(x[0],y[0],radius1,radius2,colore1,g);
		
                //draw other axes
		g.setColor(Color.black);
		// y axis
		g.drawLine(x[0],y[0],x[0]+axis,y[0]);	
		MaestroG.drawArrow(x[0]+axis,y[0],7,g);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setFont(Jfont2);
                g.drawString("y",x[0]+axis,y[0]-10);
		
                int tip = y[0]*3/7;
		int ytip= y[0]*2/11;
                
                int rule; float alpha;
                rule = AlphaComposite.SRC_OVER;
                alpha = 0.3f;
                g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
                Polygon p = new Polygon();
		p.addPoint(x[0]-tip,y[0]-ytip-radius1-3);
		p.addPoint(x[0]+tip,y[0]+ytip-radius1-3);
                p.addPoint(x[0]+tip,y[0]+ytip+radius1+3);
                p.addPoint(x[0]-tip,y[0]+-ytip+radius1+3);
                
                Polygon p2 = new Polygon();
		p2.addPoint(x[0]-35*ytip/10-radius1, y[0]-tip/2-3);
		p2.addPoint(x[0]+radius1-ytip, y[0]-tip/2-3);
		p2.addPoint(x[0]+35*ytip/10+radius1, y[0]+tip/2+3);
		p2.addPoint(x[0]-radius1+ytip, y[0]+tip/2+3);
		
                //g.setColor(Color.yellow);
                g.setColor(new Color(250,250,150));
                g.fillPolygon(p);
                g.setColor(Color.red);
                
                g.drawPolygon(p);
                g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
        
		//draw circular meridian (outer circle of picture) 
		drawCircle(x[0],y[0],radius+1,colore2,g);
		
		// draw latitude ellipse
		int latitude;
		double costheta = Math.cos(theta_angle*Math.PI/180.);
		double sintheta = Math.sin(theta_angle*Math.PI/180.);
		latitude = getSize().height/2 - (int)(radius * costheta);
		//drawEllipse(x[0],latitude,(int)(radius1 * sintheta),(int)(radius2 * sintheta),colore4,g);
		
		// draw longitude
		double phiref0, phiref1, phiref2, phiref3, phinew;
		int newradius1;
		phiref0 = -108.0;
		phiref1 = - 70.0;
		phiref2 = 70.0;
		phiref3 = 180.0;
		phinew = 0.0;
			
		if(phi_angle >= 0.0 && phi_angle <= 90.0){
		    phinew = phiref0 + phi_angle *108.0/90.0;
		}
		
		else if(phi_angle > 90.0 && phi_angle <= 180.0){
		    phinew = (phi_angle-90.0) *70.0/90.0;
		}
		else if(phi_angle > 180.0 && phi_angle <= 270.0){
		    phinew = phiref2 + (phi_angle-180.0) *110.0/90.0;
		}
		else if(phi_angle > 270.0 && phi_angle <= 360.0){
		    phinew = phiref3 + (phi_angle-270.0) *72.0/90.0;
		}
		newradius1 =(int)(radius * Math.cos(phinew*Math.PI/180.));
		
		int x2, y2, x3, y3, x4, y4, x7, y7;
		double phinew_rad = phinew*Math.PI/180.0;
		x2 = (int)(x[0] + radius1 * sintheta * Math.cos(phinew_rad));
		y2 = (int)(latitude + radius2 * sintheta * Math.sin(-phinew_rad));
		x3 = (int)(x[0] + radius1 * Math.cos(phinew_rad));
		y3 = (int)(y[0] + radius2 * Math.sin(-phinew_rad));
		x4 = x2;
		y4 = (int)((double)x4/(double)x3*(double)y3);
		
                x7 = (int)(x[0] - radius1 * Math.cos(phinew_rad));
		y7 = (int)(y[0] - radius2 * Math.sin(-phinew_rad));
		
                g.setColor(Color.black);
		
		//draw z axis
		g.drawLine(x[0],15,x[0],y[0]); 
		MaestroG.drawArrow(x[0],15,5,g);
		g.setFont(Jfont2);
                g.drawString("z",x[0]+9,20);
		g.drawLine(x[0],y[0]+10,x[0],y[0]+radius+10); 
                
                g.drawLine(x[0],y[0]+10,x[0],y[0]);
                
                int updown = 60;
                int udx = 5;
                int udy = 10;
		
                MaestroG.drawLineThick(g,x[0],y[0]-updown,x[0],y[0]+updown,5,Color.red);
		MaestroG.drawLineThick(g,x[0],y[0]-updown,x[0]-udx,y[0]-updown+udy,5,Color.red);
                MaestroG.drawLineThick(g,x[0],y[0]-updown,x[0]+udx,y[0]-updown+udy,5,Color.red);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setColor(Color.red);
                g.setFont(Jfont);
                g.drawString("I",x[0]-6*udx,y[0]-updown+6*udy);
                g.drawLine(x[0]-6*udx,y[0]-updown+4*udy-3,x[0]-3*udx,y[0]-updown+4*udy-3);
                MaestroG.drawArrow(x[0]-3*udx-5,y[0]-updown+4*udy-3,7,g);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                //draw other axes
		g.setColor(Color.black);
		
		// x axis
		g.drawLine(x[0],y[0],x[0]-tip,y[0]+tip);
		g.setFont(Jfont2);
                g.drawString("x",x[0]-(tip-5),y[0]+tip+10);
		//draw oblique arrow head
                MaestroG.drawArrow(x[0]-tip+5,y[0]+tip-5,10,g);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
                //Scanned point P (green circle)
		if(phi_angle >90 && phi_angle <270){
		    fillCircle(x2,y2,4,Color.green,g);
		    drawCircle(x2,y2,4,Color.black,g);
		}
                if(phi_angle >=270 || phi_angle <=90){
		}
		else{
		    //drawEllipse(x[0],latitude,(int)(radius1 * sintheta),(int)(radius2 * sintheta),colore4,g);
		}
                
		//redraw front arc of latitude ellipse
		//g.setColor(Color.red);
		//g.drawArc(x[0]-(int)(radius1 * sintheta),latitude-(int)(radius2 * sintheta),
		//	  2*(int)(radius1 * sintheta),2*(int)(radius2 * sintheta),180,180);
		
		//connect center to scanned point P (green dot)	
		g.setColor(Color.blue);
		g.drawLine(x[0],y[0],x2,y2-15);
		
		//projection of P on equatorial plane
		int y5 = y2+y[0]-latitude;
		//connect center to equator along edge of phi angle arc
		g.setColor(Color.lightGray);
		g.drawLine(x[0],y[0],x4,y5);
		//g.drawLine(x[0],y[0],x7,y7);
		
                g.setColor(Color.black);
		g.drawLine(x4,y5,x3,y3);
		
		//perpendicular line from point P to equatorial plane
		g.setColor(Color.lightGray);
		g.drawLine(x2,y2-15,x4,y5);
		//back drawing of theta circle, not needed
                //g.drawArc(x[0]-50,y[0]-radius1,2*50,2*radius1,90,180);
			
		g.setColor(colore2);
		g.drawArc(x[0]-radius1,y[0]-27,2*radius1,2*27,180,180);
		g.drawArc(x[0]-50,y[0]-radius1,2*50,2*radius1,90,-180);
		
		//Scanned point P (green circle)
		if(phi_angle <=90 || phi_angle >=270){
		    fillCircle(x2,y2-15,4,Color.green,g);
		    drawCircle(x2,y2-15,4,Color.black,g);
		}
                
                // draw electric field vector components
                //H_theta
                MaestroG.drawLineThick(g,x2,y2-15,x2+32,y2-27,3,Color.blue.darker());
		MaestroG.drawLineThick(g,x2+32,y2-27,x2+25,y2-27,3,Color.blue.darker());
		MaestroG.drawLineThick(g,x2+32,y2-27,x2+26,y2-22,3,Color.blue.darker());
                		
                //E_theta
                MaestroG.drawLineThick(g,x2,y2-15,x2+23,y2+20,3,Color.red.darker());
		MaestroG.drawLineThick(g,x2+23,y2+20,x2+17,y2+15,3,Color.red.darker());
		MaestroG.drawLineThick(g,x2+23,y2+20,x2+21,y2+12,3,Color.red.darker());
				
                //E_r
                MaestroG.drawLineThick(g,x2,y2-15,x2+20,y2-47,3,Color.green.darker());
		MaestroG.drawLineThick(g,x2+15,y2-43,x2+20,y2-47,3,Color.green.darker());
		MaestroG.drawLineThick(g,x2+18,y2-40,x2+20,y2-47,3,Color.green.darker());
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setColor(Color.blue.darker());
                MaestroG.subscripterItalic("H","\u03c6","",g,14,x2+35,y2-21);
                g.setColor(Color.red.darker());
                MaestroG.subscripterItalic("E","\u03b8","",g,14,x2+27,y2+19);
                g.setColor(Color.green.darker());
                MaestroG.subscripterItalic("E","R","",g,14,x2+24,y2-45);
                		                
                //draw phi angle
                alpha = 0.8f;
                g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
		g.setColor(Color.yellow);
		g.fillArc(x[0]-23,y[0]-10,46,20,-108,108+(int)(phinew));
		g.setColor(new Color(155,5,155));
		g.drawArc(x[0]-23,y[0]-10,46,20,-108,108+(int)(phinew));
		
                //alpha = 1.0f;
                //g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
		//draw theta angle
		g.setColor(Color.cyan);
		g.fillArc(x[0]-10,y[0]-20,20,46,90,-45);
		g.setColor(new Color(155,5,155));
		g.drawArc(x[0]-10,y[0]-20,20,46,90,-45);
                
                //draw phi symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("\u03c6",x[0]+6,y[0]+22);

                //draw theta symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("\u03b8",x[0]+4,y[0]-22);
                
                //draw r symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("R",x[0]+22,y[0]-25);
        }
      }
    }

    public synchronized void setPlane(boolean planeH){
	this.planeH = planeH;
    }
    
    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 mousePressed(MouseEvent evt){}
    public void mouseReleased(MouseEvent evt){}
}
