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


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 int LeftMargin=20, RightMargin=10, TopMargin=10, BottomMargin=10;
    private Font labfont = new Font("SanSerif",Font.PLAIN,10);
    private Font symbolfont= new Font("Serif",Font.PLAIN,16);
    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 Font Jfont = new Font("Serif",Font.ITALIC,26);
    private 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;
    Trans_State state;
    
    public NewGuideCanvas2(Trans_State state){
	super();
        this.state = state;
	IsFocusOn = false;
	IsTopoOn = false;
	setBackground(bgcolor2);
        
        LeftMargin = state.s20;
        RightMargin = state.s10;
        TopMargin = state.s10;
        BottomMargin = state.s10;
        labfont = new Font("SanSerif",Font.PLAIN,state.font10);
        symbolfont= new Font("Serif",Font.PLAIN,state.font16);
        labfont2= new Font("Serif",Font.BOLD,state.font16);
        labfont4= new Font("Serif",Font.PLAIN,state.font12);
        labfont3 = new Font("SanSerif",Font.PLAIN,state.font12);
        Jfont = new Font("Serif",Font.ITALIC,state.font26);
        Jfont2 = new Font("SanSerif",Font.ITALIC,state.font18);
    
	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 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.drawRect(0,0,getSize().width-1,getSize().height-1);
		///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));
		lambda="\u03bb";
		theta="\u03b8";
		phi="\u03c6";
		deg="\u00ba";
		
                g.setFont(labfont4);
		g.getFontMetrics();
		
		int xc = x[0]+x[0]*3/4;
		int yc = state.s30;
	
		xc = x[0]+x[0]*3/4;
		
                g.setFont(new Font("SanSerif",Font.PLAIN,state.s12));
                
		int radius = getSize().height/2-state.s40;
		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-state.s3);
		p.addPoint(x[0]+tip,y[0]+ytip-radius1-state.s3);
                p.addPoint(x[0]+tip,y[0]+ytip+radius1+state.s3);
                p.addPoint(x[0]-tip,y[0]+-ytip+radius1+state.s3);
                
                Polygon p2 = new Polygon();
		p2.addPoint(x[0]-35*ytip/10-radius1, y[0]-tip/2-state.s3);
		p2.addPoint(x[0]+radius1-ytip, y[0]-tip/2-state.s3);
		p2.addPoint(x[0]+35*ytip/10+radius1, y[0]+tip/2+state.s3);
		p2.addPoint(x[0]-radius1+ytip, y[0]+tip/2+state.s3);
		
                    //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+state.s1,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],state.s15,x[0],y[0]);
		MaestroG.drawArrow(x[0],state.s15,5,g);
		g.setFont(Jfont2);
                g.drawString("z",x[0]+state.s9,state.s20);
		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 = state.s60;
                int udx = state.s5;
                int udy = state.s10;
		
                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-state.s3);
                MaestroG.drawArrow(x[0]-3*udx-state.s5,y[0]-updown+4*udy-state.s3,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-state.s5),y[0]+tip+state.s10);
		//draw oblique arrow head
                MaestroG.drawArrow(x[0]-tip+state.s5,y[0]+tip-state.s5,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]-state.s27,2*radius1,2*state.s27,180,180);
		g.drawArc(x[0]-state.s50,y[0]-radius1,2*state.s50,2*radius1,90,-180);
		
		
		//Scanned point P (green circle)
		if(phi_angle <=90 || phi_angle >=270){
		    fillCircle(x3,y3,state.s4,Color.green,g);
		    drawCircle(x3,y3,state.s4,Color.black,g);
		}
                
                // draw electric field vector components
                //H_theta
                MaestroG.drawLineThick(g,x3,y3,x3+state.s32,y3-state.s5,state.s3,Color.blue.darker());
		MaestroG.drawLineThick(g,x3+state.s32,y3-state.s5,x3+state.s26,y3-state.s6,state.s3,Color.blue.darker());
		MaestroG.drawLineThick(g,x3+state.s32,y3-state.s5,x3+state.s26,y3-state.s1,state.s3,Color.blue.darker());
		
                //E_theta
                MaestroG.drawLineThick(g,x3,y3,x3-state.s3,y3+state.s38,state.s3,Color.red.darker());
		MaestroG.drawLineThick(g,x3-state.s3,y3+state.s38,x3-state.s5,y3+state.s31,state.s3,Color.red.darker());
		MaestroG.drawLineThick(g,x3-state.s3,y3+state.s38,x3,y3+state.s31,state.s3,Color.red.darker());
		
                //E_r
                MaestroG.drawLineThick(g,x3,y3,x3+state.s35,y3+state.s14,state.s3,Color.green.darker());
		MaestroG.drawLineThick(g,x3+state.s35,y3+state.s14,x3+state.s30,y3+state.s9,state.s3,Color.green.darker());
		MaestroG.drawLineThick(g,x3+state.s35,y3+state.s14,x3+state.s28,y3+state.s14,state.s3,Color.green.darker());
		
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(Color.blue.darker());
                MaestroG.subscripterItalic("H","\u03c6","",g,state.font14,x3+state.s37,y3-state.s1);
		g.setColor(Color.red.darker());
                MaestroG.subscripterItalic("E","\u03b8","",g,state.font14,x3+state.s1,y3+state.s43);
		g.setColor(Color.green.darker());
                MaestroG.subscripterItalic("E","R","",g,state.font14,x3+state.s32,y3+state.s29);
		
                //draw phi angle
                alpha = 0.8f;
                g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
		g.setColor(Color.yellow);
		g.fillArc(x[0]-state.s23,y[0]-state.s10,state.s46,state.s20,-108,108+(int)(phinew));
		g.setColor(new Color(155,5,155));
		g.drawArc(x[0]-state.s23,y[0]-state.s10,state.s46,state.s20,-108,108+(int)(phinew));
		
                //alpha = 1.0f;
                //g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
		//draw theta angle
		g.setColor(Color.cyan);
		g.fillArc(x[0]-state.s13,y[0]-state.s22,state.s28,state.s46,90,-102);
		g.setColor(new Color(155,5,155));
		g.drawArc(x[0]-state.s13,y[0]-state.s22,state.s28,state.s46,90,-102);
                
                
                
                //draw phi symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("\u03c6",x[0]+state.s6,y[0]+state.s22);

                //draw theta symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("\u03b8",x[0]+state.s18,y[0]-state.s7);
                
                //draw r symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("R",x[0]+state.s35,y[0]+state.s15);
                
	}
        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]-state.s10);
		
                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-state.s3);
		p.addPoint(x[0]+tip,y[0]+ytip-radius1-state.s3);
                p.addPoint(x[0]+tip,y[0]+ytip+radius1+state.s3);
                p.addPoint(x[0]-tip,y[0]+-ytip+radius1+state.s3);
                
                Polygon p2 = new Polygon();
		p2.addPoint(x[0]-35*ytip/10-radius1, y[0]-tip/2-state.s3);
		p2.addPoint(x[0]+radius1-ytip, y[0]-tip/2-state.s3);
		p2.addPoint(x[0]+35*ytip/10+radius1, y[0]+tip/2+state.s3);
		p2.addPoint(x[0]-radius1+ytip, y[0]+tip/2+state.s3);
		
                //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+state.s1,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],state.s15,x[0],y[0]); 
		MaestroG.drawArrow(x[0],state.s15,5,g);
		g.setFont(Jfont2);
                g.drawString("z",x[0]+state.s9,state.s20);
		g.drawLine(x[0],y[0]+state.s10,x[0],y[0]+radius+state.s10); 
                
                g.drawLine(x[0],y[0]+state.s10,x[0],y[0]);
                
                int updown = state.s60;
                int udx = state.s5;
                int udy = state.s10;
		
                MaestroG.drawLineThick(g,x[0],y[0]-updown,x[0],y[0]+updown,state.s5,Color.red);
		MaestroG.drawLineThick(g,x[0],y[0]-updown,x[0]-udx,y[0]-updown+udy,state.s5,Color.red);
                MaestroG.drawLineThick(g,x[0],y[0]-updown,x[0]+udx,y[0]-updown+udy,state.s5,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-state.s3,x[0]-3*udx,y[0]-updown+4*udy-state.s3);
                MaestroG.drawArrow(x[0]-3*udx-state.s5,y[0]-updown+4*udy-state.s3,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-state.s5),y[0]+tip+state.s10);
		//draw oblique arrow head
                MaestroG.drawArrow(x[0]-tip+state.s5,y[0]+tip-state.s5,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,state.s4,Color.green,g);
		    drawCircle(x2,y2,state.s4,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-state.s15);
		
		//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.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-state.s15,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]-state.s27,2*radius1,2*state.s27,180,180);
		g.drawArc(x[0]-state.s50,y[0]-radius1,2*state.s50,2*radius1,90,-180);
		
		//Scanned point P (green circle)
		if(phi_angle <=90 || phi_angle >=270){
		    fillCircle(x2,y2-state.s15,state.s4,Color.green,g);
		    drawCircle(x2,y2-state.s15,state.s4,Color.black,g);
		}
                
                // draw electric field vector components
                //H_theta
                MaestroG.drawLineThick(g,x2,y2-state.s15,x2+state.s32,y2-state.s27,state.s3,Color.blue.darker());
		MaestroG.drawLineThick(g,x2+state.s32,y2-state.s27,x2+state.s25,y2-state.s27,state.s3,Color.blue.darker());
		MaestroG.drawLineThick(g,x2+state.s32,y2-state.s27,x2+state.s26,y2-state.s22,state.s3,Color.blue.darker());
                		
                //E_theta
                MaestroG.drawLineThick(g,x2,y2-state.s15,x2+state.s23,y2+state.s20,state.s3,Color.red.darker());
		MaestroG.drawLineThick(g,x2+state.s23,y2+state.s20,x2+state.s17,y2+state.s15,state.s3,Color.red.darker());
		MaestroG.drawLineThick(g,x2+state.s23,y2+state.s20,x2+state.s21,y2+state.s12,state.s3,Color.red.darker());
				
                //E_r
                MaestroG.drawLineThick(g,x2,y2-state.s15,x2+state.s20,y2-state.s47,state.s3,Color.green.darker());
		MaestroG.drawLineThick(g,x2+state.s15,y2-state.s43,x2+state.s20,y2-state.s47,state.s3,Color.green.darker());
		MaestroG.drawLineThick(g,x2+state.s18,y2-state.s40,x2+state.s20,y2-state.s47,state.s3,Color.green.darker());
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setColor(Color.blue.darker());
                MaestroG.subscripterItalic("H","\u03c6","",g,state.font14,x2+state.s35,y2-state.s21);
                g.setColor(Color.red.darker());
                MaestroG.subscripterItalic("E","\u03b8","",g,state.font14,x2+state.s27,y2+state.s19);
                g.setColor(Color.green.darker());
                MaestroG.subscripterItalic("E","R","",g,state.font14,x2+state.s24,y2-state.s45);
                		                
                //draw phi angle
                alpha = 0.8f;
                g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
		g.setColor(Color.yellow);
		g.fillArc(x[0]-state.s23,y[0]-state.s10,state.s46,state.s20,-108,108+(int)(phinew));
		g.setColor(new Color(155,5,155));
		g.drawArc(x[0]-state.s23,y[0]-state.s10,state.s46,state.s20,-108,108+(int)(phinew));
		
                //alpha = 1.0f;
                //g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
		//draw theta angle
		g.setColor(Color.cyan);
		g.fillArc(x[0]-state.s10,y[0]-state.s20,state.s20,state.s46,90,-45);
		g.setColor(new Color(155,5,155));
		g.drawArc(x[0]-state.s10,y[0]-state.s20,state.s20,state.s46,90,-45);
                
                //draw phi symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("\u03c6",x[0]+state.s6,y[0]+state.s22);

                //draw theta symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("\u03b8",x[0]+state.s3,y[0]-state.s24);
                
                //draw r symbol
                g.setColor(Color.black);
                g.setFont(Jfont2);
                g.drawString("R",x[0]+state.s22,y[0]-state.s25);
        }
      }
    }

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