//NewGuideOutputPanel.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//import java.lang.*;  
import java.awt.image.*;
import java.net.*;

public class NewGuideOutputPanel extends Panel implements ItemListener{
	NewGuide_State state;
	//private static final Color bgcolor = Color.lightGray;
	private static final Color bgcolor = new Color(236,236,221);
	private static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
	private static final Font labfont2=new Font("SanSerif",Font.PLAIN,14);
	
        private static final Font titlefont=new Font("SanSerif",Font.BOLD,15);
	private static final Font symbolfont=new Font("Serif",Font.PLAIN,12);
	
	Label titlelabel;	
	public Choice c1;
	public NewOutputCanvasA rwocA; 
	public NewOutputCanvasB rwocB; 
	//public ScrollPane pane1, pane2;
	
	public NewGuideOutputPanel(NewGuide_State state){
	    super();
	    setLayout(null);
	    this.state = state;
	    setBackground(bgcolor);
	    //titlelabel = new Label("Data",Label.LEFT);
	    //add(titlelabel);
	    //titlelabel.setBounds(220,5,60,20);    
	    //titlelabel.setFont(titlefont);
	    
	    //pane1 = new ScrollPane();
	    //pane1.setBounds(2,30,297,285);
	    //add(pane1);
	    
	    //pane2  = new ScrollPane();
	    //pane2.setBounds(2,30,297,205);
	    //add(pane2);

	    c1 = new Choice();
            c1.addItem(" Output Data 1");
	    c1.addItem(" Output Data 2");
	    add(c1);
            // CHANGE ==========================================================
	    //c1.setBounds(155,8,130,15);
	    //c1.setBackground(new Color(240,255,240));
            c1.setBounds(135,6,150,25);
	    c1.setBackground(Color.white);
	    //==================================================================
	    Panel ps1 = new Panel();
	    ps1.setBackground(Color.cyan);
	    //add(ps1);
	    ps1.setBounds(154,7,132,21);
	    
	    Panel ps2 = new Panel();
	    ps2.setBackground(Color.black);
	    //add(ps2);
	    ps2.setBounds(154,7,134,17);
	    
	    rwocA = new NewOutputCanvasA(state);
	    //pane1.add(rwocA);
	    add(rwocA);
	    rwocA.setBounds(5,30,285,260);
	    
	    rwocB = new NewOutputCanvasB(state);
	    //pane2.add(rwocB);
	    add(rwocB);
	    rwocB.setBounds(5,30,285,260);
	    
	    rwocA.setVisible(true);
	    rwocB.setVisible(false);
	    
	    //Listeners
	    c1.addItemListener(this);
	    
            state.ignition();
	}
	
    @Override
    public void paint(Graphics g){
	
	g.setColor(bgcolor.darker());
	g.fillRect(0,getSize().height-2,getSize().width,2);
	g.fillRect(getSize().width-2,0,2,getSize().height);
	g.setColor(bgcolor.brighter());
	g.fillRect(0,0,2,getSize().height-1);
	g.fillRect(0,0,getSize().width-2,2);
	
    }

    public void itemStateChanged(ItemEvent evt){
	ItemSelectable ie = evt.getItemSelectable();
	    if(evt.getSource()==c1){
		if(ie.getSelectedObjects()[0]==" Output Data 1"){
		    rwocA.setVisible(true);
		    rwocB.setVisible(false);
		}
		else if(ie.getSelectedObjects()[0]==" Output Data 2"){
		    rwocA.setVisible(false);
		    rwocB.setVisible(true);
		}
	    }
    }	
}

class NewOutputCanvasA extends Canvas{
	private static final Font normalfont = new Font("SanSerif",Font.PLAIN,12);
        private static final Font normalfont2 = new Font("SanSerif",Font.PLAIN,14);
	private static final Font subfont    = new Font("SanSerif",Font.PLAIN,10);
	private static final Font symbolfont=new Font("Serif",Font.PLAIN,12);
	public String string_field, unit_field; 
	int x, y;
	public Complex field;
	public double fieldfar;
	NewGuide_State state;
	private Image im;
	private Graphics buf;
	public NewOutputCanvasA(NewGuide_State state){
	    super();
	    this.state = state;
	}
	
    @Override
	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);
	}

	//Addition to reduce flicker new routine
    @Override
	public void update(Graphics g){		// added to avoid clearing
	    paint(g);
	}
	
	public void clear(){
	    this.getGraphics().clearRect(0,0,getSize().width,getSize().height);
	    repaint();
	}

	public void drawCanvas(Graphics g){
            
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
	    int x, y, dx, dxx, dy, dyy;
	    Color bgcolor = new Color(236,236,221);
	    String stmp;
	    FontMetrics fm;
	    double light_velocity = 1.0/Math.sqrt(state.epsilon0*state.mu0*state.epsilon_r);
	    double light_velocity0 = 1.0/Math.sqrt(state.epsilon0*state.mu0);
	    String lambda, Ohm, infinity;
	    x = 10;
	    y = 20;
	    dx = 80;
	    dxx = 15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    dy = 12;
	    dyy = dy+3;
	    
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    infinity="\u221e";
	    
	    g.setColor(bgcolor);
	    g.fillRect(0,0,getSize().width,getSize().height);
	    
	    g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    g.drawString("Wavelength", x,y);
	    y +=dyy+3;
	    
	    g.setColor(Color.black);
	    MaestroG.special4("\u03bb","  =  "+MaestroA.rounder(state.lambda,8)+" [ m ]","","",g,13,x,y);
	    y +=dyy+10;
	    
	    g.setColor(Color.blue.darker());
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    if(state.frequency < 300.0E6){
                g.setColor(Color.blue);
                g.drawString("Very High Frequency (VHF) Band", x,y);
            }
            else{
                g.setColor(Color.red);
                g.drawString("Ultra High Frequency (UHF) Band", x,y);
            }
	    y +=dyy+3;
            double f_normalized;
            g.setColor(Color.black);
		if(state.frequency < 1.0E3){
		    f_normalized = state.frequency;
		    MaestroG.special("","f","  =  "+MaestroA.rounder(f_normalized,6)+"   [ Hz ]","",g,13,x,y);	
		}
		else if(state.frequency < 1.0E6 && state.frequency >= 1.0E3  ){
		    f_normalized = state.frequency/1.0E3;
		    MaestroG.special("","f","  =  "+MaestroA.rounder(f_normalized,6)+"   [ kHz ]","",g,13,x,y);
		}
		else if(state.frequency < 1.0E9 && state.frequency >= 1.0E6 ){
		    f_normalized = state.frequency/1.0E6;
		    MaestroG.special("","f","  =  "+MaestroA.rounder(f_normalized,6)+"   [ MHz ]","",g,13,x,y);
		}
		else if(state.frequency < 1.0E12 && state.frequency >= 1.0E9 ){
		    f_normalized = state.frequency/1.0E9;
		    MaestroG.special("","f","  =  "+MaestroA.rounder(f_normalized,6)+"   [ GHz ]","",g,13,x,y);
		}
		else if(state.frequency < 1.0E15 && state.frequency >= 1.0E12 ){
		    f_normalized = state.frequency/1.0E12;
		    MaestroG.special("","f","  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]","",g,13,x,y);
		}
		else{
		    f_normalized = state.frequency/1.0E12;
		    MaestroG.special("","f","  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]","",g,13,x,y);
		}
	    y +=dyy+10;
	    
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    g.drawString("Direct Path", x,y);
	    y +=dyy+3;	    
	    g.setColor(Color.black);    
	    MaestroG.special3("AC = "+MaestroA.rounder(state.DirectPath_wavelength,3)," "+lambda,"  =  "
                    +MaestroA.rounder(state.DirectPath,3)+" [ m ]","",g,13,x,y);
            y +=dyy+10;
            
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    g.drawString("Reflected Path", x,y);
	    y +=dyy+3;
	    g.setColor(Color.black);    
	    MaestroG.special3("AP = "+MaestroA.rounder(state.BounceAP_wavelength,3)," "+lambda,
                    "  =  "+MaestroA.rounder(state.BounceAP,3)+" [ m ]","",g,13,x,y);
	    y +=dyy+3;
            MaestroG.special3("PC = "+MaestroA.rounder(state.BouncePC_wavelength,3)," "+lambda,
                    "  =  "+MaestroA.rounder(state.BouncePC,3)+" [ m ]","",g,13,x,y);
            y +=dyy+3;
            MaestroG.special3("APC = "+MaestroA.rounder(state.BounceAP_wavelength+state.BouncePC_wavelength,3)," "+lambda,
                    "  =  "+MaestroA.rounder(state.BounceAP+state.BouncePC,3)+" [ m ]","",g,13,x,y);
            y +=dyy+10;
            
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    g.drawString("Ground Reflection Point", x,y);
	    y +=dyy+3;
	    g.setColor(Color.black);    
	    MaestroG.special3("BP = "+MaestroA.rounder(state.BounceBP_wavelength,3)," "+lambda,
                    "  =  "+MaestroA.rounder(state.BounceBP,3)+" [ m ]","",g,13,x,y);
	                
	}
}

    class NewOutputCanvasB extends Canvas{
	private static final Font normalfont = new Font("SanSerif",Font.PLAIN,12);
	private static final Font subfont    = new Font("SanSerif",Font.PLAIN,10);
	private static final Font symbolfont=new Font("Serif",Font.PLAIN,12);
	NewGuide_State state;
	private Image im;
	private Graphics buf;
	public NewOutputCanvasB(NewGuide_State state){
	    super();
	    this.state = state;
	}
	
	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);
	}

	//Addition to reduce flicker new routine
	public void update(Graphics g){		// added to avoid clearing
	    paint(g);
	}
	
	public void clear(){
	    this.getGraphics().clearRect(0,0,getSize().width,getSize().height);
	    repaint();
	}


	
	public void drawCanvas(Graphics g){
            
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
	    int x, y, dx, dxx, dy, dyy;
	    double v_normalized;
	    //Color bgcolor = Color.lightGray;
	    Color bgcolor = new Color(236,236,221);
	    String stmp;
	    String lambda, Ohm;
	    FontMetrics fm;
	    x = 10;
	    y = 20;
	    dx = 80;
	    dxx = 15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    //dy = fm.getHeight();
	    dy = 12;
	    dyy = dy+3;
	    
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    g.setColor(bgcolor);
	    g.fillRect(0,0,getSize().width,getSize().height);
            
            /*
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    g.drawString("Grazing Angle", x,y);
	    y +=dyy+6; // Bigger space because of symbol below
            g.setColor(Color.black);
            MaestroG.special5("\u0398"," = "+MaestroA.rounder(state.bounce_angle,3)+" \u00ba","","",g,13,x,y);
	    
            y +=dyy+10;
            */
            
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    g.drawString("Path Length Difference", x,y);
	    y +=dyy+3;
	    g.setColor(Color.black);    
	    MaestroG.special4("\u0394 r"," = "+MaestroA.rounder(state.PathDifference_wavelength,3),""+lambda,"  =  "+MaestroA.rounder(state.PathDifference_meters,3)+" [ m ]",g,13,x,y);g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
            y+=dyy+10;
            
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    g.drawString("Phase Shift due to Path Difference", x,y);
	    y +=dyy+3;
	    g.setColor(Color.black);           
            MaestroG.special4sub("\u0394 \u03d5","1"," = 2 \u03c0 \u0394 r / \u03bb"," = "+MaestroA.rounder(state.PhaseDifferencePath*Math.PI/180,4)+" rad",g,13,x,y);
            y +=dyy+3;
	    g.setColor(Color.black);           
            MaestroG.special4("         = ",""+MaestroA.rounder(state.PhaseDifferencePath,4)+" \u00ba","","",g,13,x,y);
            y+=dyy+10;
            
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    g.drawString("Phase Shift at Reflection Point P", x,y);
	    y +=dyy+3;
	    g.setColor(Color.black);           
            if(state.GroundChoose == 1){
                if(state.IsTE){MaestroG.special4sub("\u0394 \u03d5","2",""," = - 180.0 \u00ba",g,13,x,y);}
                else{MaestroG.special4sub("\u0394 \u03d5","2","","  =  0.0 \u00ba",g,13,x,y);}
            }
            else{
                if(state.fase > 0.0 && state.fase < Math.PI){
                    MaestroG.special4sub("\u0394 \u03d5","2",""," = "+MaestroA.rounder(-360 + state.fase*180/Math.PI,4)+"\u00ba",g,13,x,y);
                }
                else{
                    MaestroG.special4sub("\u0394 \u03d5","2",""," = "+MaestroA.rounder(state.fase*180/Math.PI,4)+"\u00ba",g,13,x,y);
                }
            }
            
            y +=dyy+10;
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,13));
	    g.drawString("Total Phase Shift", x,y);
	    y +=dyy+3;
	    g.setColor(Color.black);           
            if(state.GroundChoose == 1){
                MaestroG.special4("\u0394 \u03d5"," = "+MaestroA.rounder(state.phi_angle*180/Math.PI,4)+"\u00ba","","",g,13,x,y);
            }
            else{
                MaestroG.special4("\u0394 \u03d5"," = "+MaestroA.rounder(state.phi_angle*180/Math.PI,4)+"\u00ba","","",g,13,x,y);
            }
            
            y+=dyy+10;
            
            if(!state.IsTE && state.GroundChoose != 1){ // There is an angle of max transmission at which the sign of the reflection coefficient flips for TM
                g.setColor(Color.black);
                g.setFont(new Font("SanSerif",Font.BOLD,13));
                g.drawString("Grazing Angle for max transmission ", x,y);
                y +=dyy+6; // Bigger space because of symbol below
                g.setColor(Color.black);
                MaestroG.special5("\u0398"," = "+MaestroA.rounder(state.Max_Grazing_Angle,3)+" \u00ba","","",g,13,x,y);
                y +=dyy+5;
                MaestroG.special5("\u0393","  changes sign at this angle","","",g,13,x,y);
                
            y +=dyy+10;
            }
            
            /*
            if(state.IsTE){
                g.setColor(Color.red.darker());
                g.setFont(new Font("SanSerif",Font.PLAIN,13));
                g.drawString("Transverse Electric Field has reflection", x,y);
                y +=dyy+3;
                MaestroG.special3("coefficient  ","\u0393 =  \u2212"," 1  at the conducting","",g,13,x,y);
                y +=dyy+3;
                g.drawString("surface", x,y);
                
            }
            else{
                g.setColor(Color.blue.darker());
                g.setFont(new Font("SanSerif",Font.PLAIN,13));
                g.drawString("Transverse Magnetic Field has reflection", x,y);
                y +=dyy+3;
                MaestroG.special3("coefficient  ","\u0393 = ","1  at  the  conducting","",g,13,x,y);
                y +=dyy+3;
                g.drawString("surface", x,y);
            }
            */
    }

}
   