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

class
DrawCanvas extends Canvas{
	Antenna ant;
	Rectangle r;
	private static final Color bgcolor = new Color(236,236,221);
	

	DrawCanvas(Antenna ant){
		super();
		this.ant=ant;
		setBackground(bgcolor);
	}
	public void paint(Graphics g){
                
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
		double distancia, escala;
		int i, numpointsA, numpointsB;
		int x1, y1, x2, y2, x3, y3;
		r=getBounds();
		g.setColor(Color.white);
		g.fill3DRect(0,0,r.width-1,r.height-1,true);
		g.setColor(Color.black);	
		escala=r.width*0.7f;
		distancia=ant.distancia * escala;
		
		//Left Transmission Line Wires
		g.drawLine((int)((r.width-distancia)*0.5), (int)(r.height*0.88),(int)((r.width-distancia)*0.5-10),(int)(r.height*0.88));
		g.drawLine((int)((r.width-distancia)*0.5), (int)(r.height*0.92),(int)((r.width-distancia)*0.5-10),(int)(r.height*0.92));
		//g.setColor(Color.white);		
		g.drawLine((int)((r.width-distancia)*0.5), (int)(r.height*0.88)-1,(int)((r.width-distancia)*0.5-10),(int)(r.height*0.88)-1);
		g.drawLine((int)((r.width-distancia)*0.5), (int)(r.height*0.92)-1,(int)((r.width-distancia)*0.5-10),(int)(r.height*0.92)-1);
			
				
		//Left Dipole Wires
		g.setColor(Color.red);		
		g.drawLine((int)((r.width-distancia)*0.5),(int)(r.height*0.88),(int)((r.width-distancia)*0.5),(int)(r.height*0.82));
		g.drawLine((int)((r.width-distancia)*0.5),(int)(r.height*0.92),(int)((r.width-distancia)*0.5),(int)(r.height*0.98));
		
		//g.setColor(Color.white);		
		g.drawLine((int)((r.width-distancia)*0.5-1),(int)(r.height*0.88),(int)((r.width-distancia)*0.5-1),(int)(r.height*0.82));
		g.drawLine((int)((r.width-distancia)*0.5-1),(int)(r.height*0.92),(int)((r.width-distancia)*0.5-1),(int)(r.height*0.98));
		
		//Right Transmission Line Wires
		g.setColor(Color.black);
		g.drawLine((int)((r.width+distancia)*0.5), (int)(r.height*0.88),(int)((r.width+distancia)*0.5+10),(int)(r.height*0.88));
		g.drawLine((int)((r.width+distancia)*0.5), (int)(r.height*0.92),(int)((r.width+distancia)*0.5+10),(int)(r.height*0.92));
			
		g.drawLine((int)((r.width+distancia)*0.5), (int)(r.height*0.88)-1,(int)((r.width+distancia)*0.5+10),(int)(r.height*0.88)-1);
		g.drawLine((int)((r.width+distancia)*0.5), (int)(r.height*0.92)-1,(int)((r.width+distancia)*0.5+10),(int)(r.height*0.92)-1);
		
		//axis
                g.setColor(Color.blue);
		g.drawLine(15,(int)(r.height*0.80)-3,(r.width-15),(int)(r.height*0.80)-3);
		// center
                g.setColor(Color.magenta);
		g.drawLine((int)(r.width*0.5-1),(int)(r.height*0.80-8),(int)(r.width*0.5-1),(int)(r.height*0.80+2));
		
                g.setColor(Color.blue);
		// -0.5 wavelength
                g.drawLine(44,(int)(r.height*0.80-8),44,(int)(r.height*0.80+2));
		// +0.5 wavelength
                g.drawLine(r.width-46,(int)(r.height*0.80-8),r.width-46,(int)(r.height*0.80+2));
		// -0.25 wavelength
                g.drawLine((int)(r.width*0.25+22),(int)(r.height*0.80-8),(int)(r.width*0.25+22),(int)(r.height*0.80+2));
		// +0.25 wavelength
                g.drawLine((int)(r.width*0.75-23),(int)(r.height*0.80-8),(int)(r.width*0.75-23),(int)(r.height*0.80+2));


		//Right Dipole Wires
		g.setColor(Color.green);		
		g.drawLine((int)((r.width+distancia)*0.5),(int)(r.height*0.88),(int)((r.width+distancia)*0.5),(int)(r.height*0.82));
		g.drawLine((int)((r.width+distancia)*0.5),(int)(r.height*0.92),(int)((r.width+distancia)*0.5),(int)(r.height*0.98));
		
		//g.setColor(Color.white);		
		g.drawLine((int)((r.width+distancia)*0.5-1),(int)(r.height*0.88),(int)((r.width+distancia)*0.5-1),(int)(r.height*0.82));
		g.drawLine((int)((r.width+distancia)*0.5-1),(int)(r.height*0.92),(int)((r.width+distancia)*0.5-1),(int)(r.height*0.98));
		
		
		//x3 = (int) (r.width*0.30+10);
		//y3 = (int) (r.height*0.5);
		//g.drawString("Dipole",x3,y3);

		numpointsB=(int)(r.height*0.30*ant.dipolelengthB/(ant.dipolelengthmaxB));

		g.setColor(Color.green);

		if(ant.dipolelengthB>=0.026){
		  for(i=-numpointsB;i<numpointsB;i++){
			y2=(int)(r.height*(0.35)+i);
			y1=y2-1;
			x2=(int)(r.width*0.75+50*ant.scalingB*Math.sin(Math.PI*ant.dipolelengthB
                                                       -2.0*Math.PI*Math.abs(i+1)*ant.dipolelengthB/(2.0*numpointsB)));
			x1=(int)(r.width*0.75+50*ant.scalingB*Math.sin(Math.PI*ant.dipolelengthB
  						       -2.0*Math.PI*Math.abs(i)*ant.dipolelengthB/(2.0*numpointsB)));
		  	g.drawLine(x1, y1, x2, y2);
		  }
		}
		else{
			x1=(int)(r.width*0.75);
			x2=x1+50;
			y1=(int)(r.height*0.35);
			y2=y1;
			g.drawLine(x1,y1,x2,y2);
		}
		
		
		//draw the axis
		g.setColor(Color.black);		
		y2=(int)(r.height*(0.35)+numpointsB);
		y1=(int)(r.height*(0.35)-numpointsB);
		x2=(int)(r.width*0.75);
		x1=(int)(r.width*0.75);
		g.drawLine(x1,y1,x2,y2);
		g.setColor(Color.black);
		//g.setColor(Color.white);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.drawString("Current B",x2-20,y2+15);
		//g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		
                numpointsA=(int)(r.height*0.30*ant.dipolelengthA/(ant.dipolelengthmaxA));

		g.setColor(Color.red);

		if(ant.dipolelengthA>=0.026){
		  for(i=-numpointsA;i<numpointsA;i++){
			y2=(int)(r.height*(0.35)+i);
			y1=y2-1;
			x2=(int)(r.width*0.25+50*ant.scalingA*Math.sin(Math.PI*ant.dipolelengthA
                                                       -2.0*Math.PI*Math.abs(i+1)*ant.dipolelengthA/(2.0*numpointsA)));
			x1=(int)(r.width*0.25+50*ant.scalingA*Math.sin(Math.PI*ant.dipolelengthA
  						       -2.0*Math.PI*Math.abs(i)*ant.dipolelengthA/(2.0*numpointsA)));
		  	g.drawLine(x1, y1, x2, y2);
		  }
		}
		else{
			x1=(int)(r.width*0.25);
			x2=x1+50;
			y1=(int)(r.height*0.35);
			y2=y1;
			g.drawLine(x1,y1,x2,y2);
		}
		//draw the axis
		g.setColor(Color.black);		
		y2=(int)(r.height*(0.35)+numpointsA);
		y1=(int)(r.height*(0.35)-numpointsA);
		x2=(int)(r.width*0.25);
		x1=(int)(r.width*0.25);
		g.drawLine(x1,y1,x2,y2);
		g.setColor(Color.black);
		//g.setColor(Color.white);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		
                g.drawString("Current A",x2-20,y2+15);

	}
	public void redraw(){
		repaint();
	}
}
