//Position.java
/* A Java class for
 * LossyWide.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - All Rights Reserved
 */ 
import java.awt.*;
import java.awt.event.*;

public class Position extends Canvas implements MouseListener{
	
	private Trans_State state;
	private Image im;
	private Graphics buf;
	private static final Color bgcolor = Color.gray;
	private static final Color bgcolor2 = new Color(246,246,246);
	private static final Color ccolor1 = new Color(50,204,153);
	//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 static final Font normalfont = new Font("SanSerif",Font.PLAIN,12);
	public static  Font normalfont;
	private static String alpha, Ohm, lambda, infinity, Gamma, epsilon;
	
	private boolean IsFocusOn, IsTopoOn, IsDark;
	public Position(Trans_State state){
		super();
		this.state = state;
		setBackground(bgcolor2);
		this.addMouseListener(this);
		IsFocusOn = false;
		IsDark = true;
	}

	public void update(Graphics g){
		paint(g);
	}

	public void paint(Graphics g){
		
		if(im == null){
			im = createImage(getSize().width,getSize().height);
			buf = im.getGraphics();
			drawStrings(buf);
		}
		else{
			drawStrings(buf);
		}
		g.drawImage(im,0,0,null);
	}
	
	private void drawStrings(Graphics g){ 
		int xd, yd, dx;
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
		g.clearRect(0,0,getSize().width,getSize().height);
		
		g.setColor(Color.white);
		FontMetrics fm = g.getFontMetrics();
		
		//g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\uu0393"; 
		epsilon="\u03b5";
		g.setFont(normalfont);
		
	    xd = 2;
	    yd = 12;
	    dx = 1;
	
	    double testlength = state.xpos*state.wavelength;
	    int range = 7;
	    if(state.lineLength < 1.0){
		range = 7;
	    }
	    else if(state.lineLength >= 1.0 && state.lineLength < 10.0){
		range = 6;
	    }
	    else if(state.lineLength >= 10.0 && state.lineLength < 100.0){
		range = 5;
	    }
	    else if(state.lineLength >= 100.0 && state.lineLength < 1000.0){
		range = 4;
	    }
	    else if(state.lineLength >= 1000.0 && state.lineLength < 10000.0){
		range = 3;
	    }
	    else if(state.lineLength >= 10000.0 && state.lineLength < 100000.0){
		range = 2;
	    }
	    else if(state.lineLength >= 100000.0){
		range = 1;
	    }
	    
	    dx = 0;
	    
	    
		g.setColor(Color.black);
          if(state.xpos == 0.0){
            MaestroG.subscripterSymMiddle2("d   = "+MaestroA.rounder(state.xpos,range)+" ","","",lambda,"  = "+MaestroA.rounder(state.xpos*state.wavelength,6)+" m",g,11,xd+dx,yd);	
          }
          else{ 
            
            if(testlength < 1.0E-9 && testlength > 0.0){
		MaestroG.subscripterSymMiddle2("d   = "+MaestroA.rounder(state.xpos,range),"","",lambda,"    = "+MaestroA.rounder(state.xpos*state.wavelength*1.0e9,8)+" nm",g,11,xd+dx,yd);	
            }
	    else if(testlength < 1.0E-6 && testlength >= 1.0E-9){
		MaestroG.subscripterSymMiddle2("d   = "+MaestroA.rounder(state.xpos,range),"","",lambda,"    = "+MaestroA.rounder(state.xpos*state.wavelength*1.0e9,4)+" nm",g,11,xd+dx,yd);	
            }
	    else if(testlength < 1.0E-3 && testlength >= 1.0E-6){
		MaestroG.subscripterSymMiddle2("d   = "+MaestroA.rounder(state.xpos,range),"","",lambda,"    = "+MaestroA.rounder(state.xpos*state.wavelength*1.0e6,4)+" \u00b5m",g,11,xd+dx,yd);	
	    }
	    else if(testlength < 1.0 && testlength >= 1.0E-3){
		MaestroG.subscripterSymMiddle2("d   = "+MaestroA.rounder(state.xpos,range),"","",lambda,"    = "+MaestroA.rounder(state.xpos*state.wavelength*1000.0,4)+" mm",g,11,xd+dx,yd);
            }
	    else if((testlength < 1000.0 && testlength >= 1.0 ) || testlength == 0.0){
		MaestroG.subscripterSymMiddle2("d   = "+MaestroA.rounder(state.xpos,range),"","",lambda,"    = "+MaestroA.rounder(state.xpos*state.wavelength,6)+" m",g,11,xd+dx,yd);	
            }
	    else if(testlength >= 1000.0 && testlength < 1.0E6){
		MaestroG.subscripterSymMiddle2("d   = "+MaestroA.rounder(state.xpos,range),"","",lambda,"    = "+MaestroA.rounder(state.xpos*state.wavelength/1.0e3,4)+" km",g,11,xd+dx,yd);	
            }
	    else if(testlength >= 1.0E6 && testlength < 1.0E9){
		MaestroG.superscripterSymFirst("d   = "+MaestroA.rounder(state.xpos,range)," "+lambda,"    = "+MaestroA.rounder(state.xpos*state.wavelength/1.0e6,4)+" x 10","3"," km",g,11,xd+dx,yd);
            }
	    else if(testlength >= 1.0E9){
		MaestroG.superscripterSymFirst("d   = "+MaestroA.rounder(state.xpos,range)," "+lambda,"    = "+MaestroA.rounder(state.xpos*state.wavelength/1.0e9,4)+" x 10","6"," km",g,11,xd+dx,yd);
            }
          }	
	}
	
	public void mouseClicked(MouseEvent evt){
	    if(IsFocusOn){
		IsFocusOn = false;
		repaint();
	    }
	
	    else{
		IsFocusOn = true;
		repaint();
	    }
    
	}
	
	public void setDark(boolean IsDark){
	    this.IsDark = IsDark;
	}
    
	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){}
    
}//End of Position

