//DrawCanvas.java
//Singh T. Junior and Umberto Ravaioli


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

class DrawCanvas extends Canvas{
	private Image im;
	private Graphics buf;
	DipoleAnt ant;
	Rectangle r;
	

	DrawCanvas(DipoleAnt ant){
		super();
		this.ant=ant;
	}
	public void drawCanvas(Graphics g){
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
            int i, numpoints, length_monopole, ycenter, deltay;
		int x1, y1, x2, y2, x3, y3;
		r=getBounds();
		g.setColor(Color.white);
		g.fill3DRect(0,0,r.width-1,r.height-1,true);
                ycenter = (int)(r.height*0.48)-1;
                deltay = (int)(r.height*0.20*0.05);
                length_monopole = (int)(r.height*((0.44)*ant.dipolelength/ant.dipolelengthmax));
		
                if((int)ant.dipolelengthmax < 6){
                    deltay = (int)(r.height*0.30*0.05);
                    length_monopole = (int)(r.height*((0.43)*ant.dipolelength/ant.dipolelengthmax));
                }
                else if((int)ant.dipolelengthmax >= 6 && (int)ant.dipolelengthmax < 11){
                    deltay = (int)(r.height*0.20*0.05);
                    length_monopole = (int)(r.height*((0.44)*ant.dipolelength/ant.dipolelengthmax));
                }
                else if((int)ant.dipolelengthmax >= 11 && (int)ant.dipolelengthmax <= 20){
                    deltay = (int)(r.height*0.15*0.05);
                    length_monopole = (int)(r.height*((0.45)*ant.dipolelength/ant.dipolelengthmax));
                }
                
                g.setColor(Color.black);		
		g.drawLine((int)(r.width*0.1-10), ycenter-deltay,(int)(r.width*0.35-10),ycenter-deltay);
		g.drawLine((int)(r.width*0.1-10), ycenter+deltay,(int)(r.width*0.35-10),ycenter+deltay);
		//g.drawLine((int)(r.width*0.1-10), ycenter-deltay-1,(int)(r.width*0.35-10),ycenter-deltay-1);
		//g.drawLine((int)(r.width*0.1-10), ycenter+deltay+1,(int)(r.width*0.35-10),ycenter+deltay+1);
				
		g.setColor(Color.red);	
                //g.drawLine((int)(r.width*0.35-10),(int)(r.height*0.46),
                //           (int)(r.width*0.35-10),(int)(r.height*(0.46))-length_monopole);
		//g.drawLine((int)(r.width*0.35-10),(int)(r.height*0.50),
                //           (int)(r.width*0.35-10),(int)(r.height*(0.50))+length_monopole);

                g.drawLine((int)(r.width*0.35-10),ycenter-deltay,
                           (int)(r.width*0.35-10),ycenter-deltay -length_monopole);
		g.drawLine((int)(r.width*0.35-10),ycenter+deltay,
                           (int)(r.width*0.35-10),ycenter+deltay +length_monopole);
		
                //g.drawLine((int)(r.width*0.35-11),(int)(r.height*0.46),
                //           (int)(r.width*0.35-11),(int)(r.height*(0.46))-length_monopole);
		//g.drawLine((int)(r.width*0.35-11),(int)(r.height*0.50),
                //           (int)(r.width*0.35-11),(int)(r.height*(0.50))+length_monopole);
		
                g.drawLine((int)(r.width*0.35-11),ycenter-deltay,
                           (int)(r.width*0.35-11),ycenter-deltay -length_monopole);
		g.drawLine((int)(r.width*0.35-11),ycenter+deltay,
                           (int)(r.width*0.35-11),ycenter+deltay +length_monopole);
		
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    
		x3 = (int) (r.width*0.30+10);
		y3 = (int) (r.height*0.5);
		g.drawString("Dipole",x3,y3);

		numpoints=(int)(r.height*0.45f*ant.dipolelength/(ant.dipolelengthmax));
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
		g.setColor(Color.green);
                
		if(ant.dipolelength>=(ant.dipolelengthmax/100.0f)){
		  for(i=-numpoints;i<numpoints;i++){
			y2=(int)(r.height*(0.48f)+i);
			y1=y2-1;
			x2=(int)(r.width*0.70+50*ant.scaling*Math.sin(Math.PI*ant.dipolelength
                                                       -2.0*Math.PI*Math.abs(i+1)*ant.dipolelength/(2.0*numpoints)));
			x1=(int)(r.width*0.70+50*ant.scaling*Math.sin(Math.PI*ant.dipolelength
  						       -2.0*Math.PI*Math.abs(i)*ant.dipolelength/(2.0*numpoints)));
		  	g.drawLine(x1, y1, x2, y2);
                        
		  }
                  
                    //draw the axis
                    g.setColor(Color.black);		
                    y2=(int)(r.height*(0.48f)+numpoints);
                    y1=(int)(r.height*(0.48f)-numpoints)-1;
                    x2=(int)(r.width*0.70);
                    x1=(int)(r.width*0.70);
                    g.drawLine(x1,y1,x2,y2);
		}
		else{
			x1=(int)(r.width*0.70);
			x2=x1+50;
			y1=(int)(r.height*0.48f);
			y2=y1;
			g.drawLine(x1,y1,x2,y2);
                        
                        //draw the axis
                        g.setColor(Color.black);		
                        y2=(int)(r.height*(0.48f)+numpoints);
                        y1=(int)(r.height*(0.48f)-numpoints);
                        x2=(int)(r.width*0.70);
                        x1=(int)(r.width*0.70);
                        g.drawLine(x1,y1,x2,y2);
		}
		
	
                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    
                
                g.setColor(Color.black);
		g.drawString("Current",x2-20,y2+12);
	}
	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();
	}
}
