//PlaneWavePolarCanvas.java
import java.awt.*;
import java.awt.event.*;
 
public class PlaneWavePolarCanvas extends Canvas{
	//private static final Color bgcolor = new Color(216,216,191);
	private static final Color bgcolor = new Color(216,230,230);
	private static final Color acolor = new Color(152,152,152);
	private static final Color ccolor = new Color(230,230,230);
        
        private static final Font TitleFont = new Font("SanSerif",Font.BOLD,11);
        private static final Font LabelFont = new Font("SanSerif",Font.PLAIN,11);
	
        private Image im;
	private static final String znames[] = {"A","B"};
	private Graphics buf;
	private int rcx, rcy, raioH, raioV;
	private PlaneWave_State state;
	private int nPlane;//Which plane to operate on
	private double ampAA, ampBB, phaseAA, phaseBB;
	private double ampA, ampB, phaseA, phaseB;
	private static final int LeftMargin=10, RightMargin=10, TopMargin=10, BottomMargin=10;
	public PlaneWavePolarCanvas(PlaneWave_State state, int nPlane){
		super();
		this.state = state;
                setBackground(bgcolor);
		this.nPlane = nPlane;
	}
	
	public void paint(Graphics g){
	    if(im == null){
		im = createImage(getSize().width,getSize().height);
		buf = im.getGraphics();
		drawCanvas(buf);
		drawState(buf);
		drawNames(buf);
	    }
	    else{
		drawCanvas(buf);
		drawState(buf);
		drawNames(buf);
	    }
	    g.drawImage(im,0,0,null);
	}
	
	private void drawCanvas(Graphics g){
		double resmag, resphase;
                Graphics2D g2d = (Graphics2D)g;
                
		g.setColor(Color.black);
		g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
		rcx=getSize().width/2;
		rcy=getSize().height/2;
		raioV=(getSize().height-2*TopMargin-2*BottomMargin)/2;
		raioH=(getSize().width-2*LeftMargin-2*RightMargin)/2;
		
		g.setColor(acolor);
		if(state.RESET){g.fillRect(0,0,getSize().width-1,getSize().height-1);}
		g.setColor(bgcolor);
		g.fillRect(0,0,getSize().width-1,getSize().height-1);
		//draw axis system
		g.setColor(Color.black);
		//vertical axis
		g.drawLine(getSize().width/2,TopMargin,getSize().width/2,getSize().height-BottomMargin);
		
		g.setColor(bgcolor);
		g.drawLine(getSize().width/2-1,TopMargin,getSize().width/2-1,getSize().height-BottomMargin);
		g.drawLine(getSize().width/2+1,TopMargin,getSize().width/2+1,getSize().height-BottomMargin);
		
		//Horizontal axis
		g.setColor(Color.black);
		g.drawLine(LeftMargin,getSize().height/2,getSize().width-RightMargin,getSize().height/2);
		
		g.setColor(bgcolor);
		g.drawLine(LeftMargin,getSize().height/2-1,getSize().width-RightMargin,getSize().height/2-1);
		g.drawLine(LeftMargin,getSize().height/2+1,getSize().width-RightMargin,getSize().height/2+1);
		
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
		g.setColor(Color.black);
		//Right Tickmark
		g.drawLine(getSize().width-2*(RightMargin),getSize().height/2+5,getSize().width-2*(RightMargin),getSize().height/2-5);
		//Top Tickmark
		g.drawLine(getSize().width/2-5,2*(TopMargin),getSize().width/2+5,2*(TopMargin));
		//Left Tickmark
		g.drawLine(2*(LeftMargin),getSize().height/2+5,2*(LeftMargin),getSize().height/2-5);
		//Bottom TickMark
		g.drawLine(getSize().width/2-5,getSize().height-2*(BottomMargin),getSize().width/2+5,getSize().height-2*(BottomMargin));
		
		//MaestroG.drawArrow(getSize().width-15,getSize().height/2,3,g);
		MaestroG.drawArrow(15,getSize().height/2,8,g);
		MaestroG.drawArrow(getSize().width/2,13,5,g);
		FontMetrics fm = g.getFontMetrics();
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
		double Efield, Hfield;
                int fonto = 12;
                int fonto2 = 11;
                String unitaE, unitaH, unita;
                unitaE = "  [ V / m ]";
                unitaH = "  [ A / m ]";
                unita = "";
                
                Efield = state.Ex;
                Hfield = state.Hy;
		
                MaestroG.subscripter("- H","0","",g,12,getSize().width-2*RightMargin-13,getSize().height/2+fm.getHeight()+5);
		MaestroG.subscripter("+ H","0","",g,12,LeftMargin-5,getSize().height/2+fm.getHeight()+5);
		printdata(Hfield,g,fonto,LeftMargin-5,getSize().height/2+fm.getHeight()+25,unita);
		
                MaestroG.subscripter("- E","0","",g,12,getSize().width/2+fm.stringWidth("+ Ex")/2,getSize().height-fm.getHeight());
		MaestroG.subscripter("+ E","0","",g,12,getSize().width/2+fm.stringWidth("+ Ex")/2,TopMargin*2+TopMargin/2);
		printdata(Efield,g,fonto,getSize().width/2+fm.stringWidth("+ Ex")/2+30,TopMargin*2+TopMargin/2,unita);
		
                MaestroG.subscripter(unitaE,"","",g,fonto2,getSize().width/2+fm.stringWidth("+ Ex")/2+5,TopMargin*2+TopMargin/2+fm.getHeight());
                MaestroG.subscripter(unitaH,"","",g,fonto2,LeftMargin,getSize().height/2+fm.getHeight()+25+fm.getHeight());
                
                g.setColor(Color.red);
		g.drawString("x",getSize().width/2-20,TopMargin*2+TopMargin/2);
		g.setColor(Color.blue);
		g.drawString("y",LeftMargin+7,getSize().height/2-fm.getHeight());
	}	
	
	public void drawState(Graphics g){
		int xVec, yVec, i;
		int cwidth=5;
	  	rcx=getSize().width/2;
		rcy=getSize().height/2;
		
		i = (int)(state.NPointsZ*state.zpos[nPlane]/(state.total_length));
		if(i>=state.NPointsZ){i = state.NPointsZ-1; }
		xVec = (int)MaestroA.mapper(state.efield[i],(double)(rcy-raioV),(double)(rcy+raioV),state.Ex,-state.Ex);
		yVec = (int)MaestroA.mapper(state.hfield[i],(double)(rcx+raioH),(double)(rcx-raioH),-state.Ex/state.wave_impedance_mag,state.Ex/state.wave_impedance_mag);
		g.setColor(Color.blue);
		g.setColor(Color.red);
		g.setColor(Color.red);
		g.drawLine(rcx,rcy,rcx,xVec);
		
		g.drawLine(rcx-1,rcy,rcx-1,xVec);
		g.drawLine(rcx+1,rcy,rcx+1,xVec);
		
		//draw E component
		g.setColor(Color.blue);
		g.drawLine(rcx,rcy,yVec,rcy);
		
		g.drawLine(rcx,rcy-1,yVec,rcy-1);
		g.drawLine(rcx,rcy+1,yVec,rcy+1);
		
	}
        
        public static void printdata(double field, Graphics g, int fonto, int xinit2, int y, String unita){
            
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
            if(field < 1.0e-15 && field != 0.0){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e16,5)+"  x  10","-16"," "+unita,g,fonto,xinit2,y);
	    }
            if(field == 0.0){
		MaestroG.superscripter("= 0.0",""," "+unita,g,fonto,xinit2,y);
	    }
            if(field >= 1.0e-15 && field < 1.0e-14){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e15,5)+"  x  10","-15"," "+unita,g,fonto,xinit2,y);
	    }
            if(field >= 1.0e-14 && field < 1.0e-13){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e14,5)+"  x  10","-14"," "+unita,g,fonto,xinit2,y);
	    }
            if(field >= 1.0e-13 && field < 1.0e-12){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e13,5)+"  x  10","-13"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-12 && field < 1.0e-11){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e12,5)+"  x  10","-12"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-11 && field < 1.0e-10){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e11,5)+"  x  10","-11"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-10 && field < 1.0e-9){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e10,5)+"  x  10","-10"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-9 && field < 1.0e-8){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e9,5)+"  x  10","-9"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-8 && field < .0e-7){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e8,5)+"  x  10","-8"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-7 && field < 1.0e-6){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e7,5)+"  x  10","-7"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-6 && field < 1.0e-5){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e6,5)+"  x  10","-6"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-5 && field < 1.0e-4){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e5,5)+"  x  10","-5"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-4 && field < 1.0e-3){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e4,5)+"  x  10","-4"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-3 && field < 1.0e-2){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e3,5)+"  x  10","-3"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-2 && field < 1.0e-1){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e2,5)+"  x  10","-2"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e-1 && field < 1.0){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e1,5)+"  x  10","-1"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0 && field < 1.0e1){
		MaestroG.superscripter("= "+MaestroA.rounder(field,5)+"  ",""," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e1 && field < 1.0e2){
		MaestroG.superscripter("= "+MaestroA.rounder(field,5)+"  ",""," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e2 && field < 1.0e3){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-2,5)+"  x  10","2"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e3 && field < 1.0e4){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-3,5)+"  x  10","3"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e4 && field < 1.0e5){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-4,5)+"  x  10","4"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e5 && field < 1.0e6){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-5,5)+"  x  10","5"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e6 && field < 1.0e7){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-6,5)+"  x  10","6"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e7 && field < 1.0e8){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-7,5)+"  x  10","7"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e8 && field < 1.0e9){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-8,5)+"  x  10","8"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e9 && field < 1.0e10){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-9,5)+"  x  10","9"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e10 && field < 1.0e11){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-10,5)+"  x  10","10"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e11 && field < 1.0e12){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-11,5)+"  x  10","11"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e12 && field < 1.0e13){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-12,5)+"  x  10","12"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e13 && field < 1.0e14){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-13,5)+"  x  10","13"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e14 && field < 1.0e15){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-14,5)+"  x  10","14"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e15 && field < 1.0e16){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-15,5)+"  x  10","15"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e16 && field < 1.0e17){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-16,5)+"  x  10","16"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e17 && field < 1.0e18){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-17,5)+"  x  10","17"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e18 && field < 1.0e19){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-18,5)+"  x  10","18"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e19 && field < 1.0e20){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-19,5)+"  x  10","19"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e20 && field < 1.0e21){
		MaestroG.superscripter("= "+MaestroA.rounder(field*1.0e-20,5)+"  x  10","20"," "+unita,g,fonto,xinit2,y);
	    }
	    if(field >= 1.0e21){
		MaestroG.superscripter("= "+field+"",""," "+unita,g,fonto,xinit2,y);
	    }
        }
	
	public void drawNames(Graphics g){
	    if(nPlane == 0){g.setColor(Color.black);}
            else{g.setColor(Color.red.darker());}
            g.setFont(TitleFont);
	    g.drawString(znames[nPlane],LeftMargin,2*TopMargin);
	    
	}
	
	public void update(Graphics g){
		paint(g);
	}
}	

