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


public class RadarCanvas extends Canvas{
	private Image im;
	private Graphics buf;
	Rectangle r;
	DipoleAnt ant;
	boolean is_theta_plane;
	RadarCanvas(DipoleAnt ant, boolean is_theta_plane){
		super();
		this.ant=ant;
		this.is_theta_plane=is_theta_plane;
	}

	public void drawCanvas(Graphics g){
	//public void paint(Graphics g){
		int i;
		float theta;
		r=getBounds();	
		g.setColor(Color.lightGray);
		g.fill3DRect(0,0,r.width-1,r.height-1,true);
                
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		
                g.setColor(Color.white);
		//draw concentric circles
		g.drawOval((int)(r.width*0.4),(int)(r.width*0.4),(int)(r.width*0.20),(int)(r.width*0.20));
		g.drawOval((int)(r.width*0.3),(int)(r.width*0.3),(int)(r.width*0.40),(int)(r.width*0.40));
		g.drawOval((int)(r.width*0.2),(int)(r.width*0.2),(int)(r.width*0.60),(int)(r.width*0.60));
		g.drawOval((int)(r.width*0.1),(int)(r.width*0.1),(int)(r.width*0.80),(int)(r.width*0.80));

		//draw radial lines
		g.drawLine((int)(r.width*0.1),(int)(r.width*0.5),(int)(r.width*0.9),(int)(r.width*0.5));
		g.drawLine((int)(r.width*0.5),(int)(r.width*0.1),(int)(r.width*0.5),(int)(r.width*0.9));
		g.drawLine( (int)(r.width*(0.5+0.4*(0.866))), (int)(r.width*(0.5+0.4*(0.5))), 
                            (int)(r.width*(0.5-0.4*(0.866))), (int)(r.width*(0.5-0.4*(0.5))));
		g.drawLine( (int)(r.width*(0.5-0.4*(0.866))), (int)(r.width*(0.5+0.4*(0.5))), 
                            (int)(r.width*(0.5+0.4*(0.866))), (int)(r.width*(0.5-0.4*(0.5))));
		g.drawLine( (int)(r.width*(0.5+0.4*(0.5))), (int)(r.width*(0.5+0.4*(0.866))), 
                            (int)(r.width*(0.5-0.4*(0.5))), (int)(r.width*(0.5-0.4*(0.866))));
		g.drawLine( (int)(r.width*(0.5-0.4*(0.5))), (int)(r.width*(0.5+0.4*(0.866))), 
                            (int)(r.width*(0.5+0.4*(0.5))), (int)(r.width*(0.5-0.4*(0.866))));

                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
		
                g.setColor(Color.black);
		//write the angles
		if(this.is_theta_plane==true)
		{	//g.setColor(Color.red);
			//g.setFont(new Font("Symbol",Font.PLAIN,18));
			//g.drawString("\u03b8",20,20);
			g.setColor(Color.black);
			g.setFont(new Font("Helvetica",Font.PLAIN,11));
			g.drawString("0\u00ba",(int)(r.width*  (0.5-0.42*(0.030))),(int)(r.width*(0.5+0.42*(-1.00))));
			g.drawString("30\u00ba",(int)(r.width* (0.5+0.42*(0.500))),(int)(r.width*(0.5+0.42*(-0.866))));
			g.drawString("30\u00ba",(int)(r.width* (0.5-0.46*(0.500))-3),(int)(r.width*(0.5+0.42*(-0.866))));
			g.drawString("60\u00ba",(int)(r.width* (0.5+0.42*(0.866))),(int)(r.width*(0.5+0.44*(-0.50))));
			g.drawString("60\u00ba",(int)(3+r.width* (0.5-0.44*(1.0))),(int)(r.width*(0.5+0.44*(-0.47))));
			g.drawString("90\u00ba",(int)(r.width* (0.5+0.42*(1.000))),(int)(r.width*(0.5+0.42*(0.05))));
			g.drawString("90\u00ba",(int)(3+r.width* (0.5-0.46*(1.050))),(int)(r.width*(0.5+0.42*(0.05))));
			g.drawString("120\u00ba",(int)(r.width*(0.5+0.43*(0.866))),(int)(r.width*(0.5+0.48*(0.50))));
			g.drawString("120\u00ba",(int)(3+r.width*(0.5-0.48*(0.95))),(int)(r.width*(0.5+0.48*(0.50))));
			g.drawString("150\u00ba",(int)(r.width*(0.5+0.42*(0.450))),(int)(r.width*(0.5+0.47*(0.866))));
			g.drawString("150\u00ba",(int)(r.width*(0.5-0.47*(0.550))),(int)(r.width*(0.5+0.47*(0.866))));
			g.drawString("180\u00ba",(int)(r.width*(0.5-0.42*(0.070))),(int)(r.width*(0.5+0.44*(1.05))));
			g.setColor(Color.red.darker());
			g.setFont(new Font("Serif",Font.PLAIN,18));
			g.drawString("\u03b8",20,20);
			g.setColor(Color.black);

		}
		else{ g.setColor(Color.blue);
			g.setFont(new Font("Serif",Font.PLAIN,13));
			g.drawString("= 90 \u00ba",r.width-43,20);
			g.setFont(new Font("Serif",Font.PLAIN,18));
			g.setColor(Color.red.darker());
			g.drawString("\u03c6",20,20);
			g.setColor(Color.blue);
			g.setFont(new Font("Serif",Font.PLAIN,13));
			g.drawString("\u03b8",r.width-55,20);
			//g.setFont(new Font("Serif",Font.PLAIN,13));
			
			//g.drawString("\u03b8=90 \u00ba",r.width-45,20);
			
			g.setFont(new Font("Helvetica",Font.PLAIN,11));
			g.setColor(Color.black);
			g.drawString("90\u00ba",(int)(r.width*  (0.5-0.42*(0.050))),(int)(r.width*(0.5+0.42*(-1.00))));
			g.drawString("60\u00ba",(int)(r.width* (0.5+0.42*(0.500))),(int)(r.width*(0.5+0.42*(-0.866))));
			g.drawString("120\u00ba",(int)(r.width* (0.5-0.47*(0.550))),(int)(r.width*(0.5+0.42*(-0.866))));
			g.drawString("30\u00ba",(int)(r.width* (0.5+0.42*(0.866))),(int)(r.width*(0.5+0.42*(-0.50))));
			g.drawString("150\u00ba",(int)(r.width* (0.5-0.44*(1.0))),(int)(r.width*(0.5+0.44*(-0.47))));
			g.drawString("0\u00ba",(int)(3+r.width* (0.5+0.42*(1.000))),(int)(r.width*(0.5+0.42*(0.05))));
			g.drawString("180\u00ba",(int)(3+r.width* (0.5-0.45*(1.10))),(int)(r.width*(0.5+0.45*(0.05))));
			g.drawString("330\u00ba",(int)(r.width*(0.5+0.43*(0.866))),(int)(r.width*(0.5+0.48*(0.50))));
			g.drawString("210\u00ba",(int)(r.width*(0.5-0.48*(0.95))),(int)(r.width*(0.5+0.48*(0.50))));
			g.drawString("300\u00ba",(int)(r.width*(0.5+0.42*(0.450))),(int)(r.width*(0.5+0.47*(0.866))));
			g.drawString("240\u00ba",(int)(r.width*(0.5-0.47*(0.550))),(int)(r.width*(0.5+0.47*(0.866))));
			g.drawString("270\u00ba",(int)(r.width*(0.5-0.42*(0.070))),(int)(r.width*(0.5+0.44*(1.05))));
		}


                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		
                if(this.is_theta_plane==true){//This is the theta plane
		//draw a dipole antenna in the center
		  g.setColor(Color.red);
		  g.drawLine((int)(r.width*(0.5)),(int)(r.width*(0.5-0.1)),
		           (int)(r.width*(0.5)),(int)(r.width*(0.5-0.03)));
		  g.drawLine((int)(r.width*(0.5)),(int)(r.width*(0.5+0.1)),
		           (int)(r.width*(0.5)),(int)(r.width*(0.5+0.03)));


		}
		else{//This is phi plane
		  g.setColor(Color.red);
		  g.fillOval(
			(int)(r.width*(0.5)-r.width*(0.00625)-2),
			(int)(r.width*(0.5)-r.width*(0.00625)-1),
			(int)(r.width*(0.025)),
			(int)(r.width*(0.025)));

		}
		if(ant.dipolelength!=0.0){
		//Now draw the radiation diagram
		g.setColor(Color.blue);
		if(ant.rcons.c1.getState()==true){
			for(i=0;i<360;i++){		
				theta=(float)(Math.PI*i/360.0f-Math.PI/2.0);
				if(this.is_theta_plane==true){//theta-plane
				   g.drawLine(
					(int)(r.width*(0.5+0.4*ant.efield[i]*Math.cos(theta))),
					(int)(r.width*(0.5+0.4*ant.efield[i]*Math.sin(theta))),
					(int)(r.width*(0.5+0.4*ant.efield[i+1]*Math.cos(theta))),
					(int)(r.width*(0.5+0.4*ant.efield[i+1]*Math.sin(theta))));
				   g.drawLine(
					(int)(r.width*(0.5-0.4*ant.efield[i]*Math.cos(theta))),
					(int)(r.width*(0.5-0.4*ant.efield[i]*Math.sin(theta))),
					(int)(r.width*(0.5-0.4*ant.efield[i+1]*Math.cos(theta))),
					(int)(r.width*(0.5-0.4*ant.efield[i+1]*Math.sin(theta))));
				}
				else{
				   g.drawLine(//phi-plane
					(int)(r.width*(0.5+0.4*ant.eefield[i]*Math.cos(theta))),
					(int)(r.width*(0.5+0.4*ant.eefield[i]*Math.sin(theta))),
					(int)(r.width*(0.5+0.4*ant.eefield[i+1]*Math.cos(theta))),
					(int)(r.width*(0.5+0.4*ant.eefield[i+1]*Math.sin(theta))));
				   g.drawLine(
					(int)(r.width*(0.5-0.4*ant.eefield[i]*Math.cos(theta))),
					(int)(r.width*(0.5-0.4*ant.eefield[i]*Math.sin(theta))),
					(int)(r.width*(0.5-0.4*ant.eefield[i+1]*Math.cos(theta))),
					(int)(r.width*(0.5-0.4*ant.eefield[i+1]*Math.sin(theta))));
				}
			}

			
		}
                
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
		
                if(ant.rcons.c3.getState()==true){
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

			for(i=0;i<360;i++){		
				theta=(float)(Math.PI*i/360.0f-Math.PI/2.0);
				if(this.is_theta_plane==true){//theta-plane
				   g.drawLine(
					(int)(r.width*(0.5+0.4*ant.power[i]*Math.cos(theta))),
					(int)(r.width*(0.5+0.4*ant.power[i]*Math.sin(theta))),
					(int)(r.width*(0.5+0.4*ant.power[i+1]*Math.cos(theta))),
					(int)(r.width*(0.5+0.4*ant.power[i+1]*Math.sin(theta))));
				}
				else{//phi-plane
				   g.drawLine(
					(int)(r.width*(0.5+0.4*ant.ppower[i]*Math.cos(theta))),
					(int)(r.width*(0.5+0.4*ant.ppower[i]*Math.sin(theta))),
					(int)(r.width*(0.5+0.4*ant.ppower[i+1]*Math.cos(theta))),
					(int)(r.width*(0.5+0.4*ant.ppower[i+1]*Math.sin(theta))));
				}
			}
			for(i=0;i<360;i++){		
				theta=(float)(Math.PI*i/360.0f-Math.PI/2.0);
				if(this.is_theta_plane==true){//theta-plane
				   g.drawLine(
					(int)(r.width*(0.5-0.4*ant.power[i]*Math.cos(theta))),
					(int)(r.width*(0.5-0.4*ant.power[i]*Math.sin(theta))),
					(int)(r.width*(0.5-0.4*ant.power[i+1]*Math.cos(theta))),
					(int)(r.width*(0.5-0.4*ant.power[i+1]*Math.sin(theta))));
				}
				else{//phi-plane
				   g.drawLine(
					(int)(r.width*(0.5-0.4*ant.ppower[i]*Math.cos(theta))),
					(int)(r.width*(0.5-0.4*ant.ppower[i]*Math.sin(theta))),
					(int)(r.width*(0.5-0.4*ant.ppower[i+1]*Math.cos(theta))),
					(int)(r.width*(0.5-0.4*ant.ppower[i+1]*Math.sin(theta))));
				}
			}
                        
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
		}
	     }
	}

	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);
	    
	    //drawCanvas(g);
	}
    
	//Addition to reduce flicker new routine
	public void update(Graphics g){		// added to avoid clearing
	    paint(g);
	}


	public void redraw(){
                repaint();
        }
}//End of RadarCanvas
