//NewGuideOutputPanel.java
import java.awt.*;
import java.awt.event.*;

public class NewGuideOutputPanel extends Panel implements ItemListener{
	NewGuide_State state;
	private static final Color bgcolor = new Color(236,236,221);
	private Font labfont;
	private Font labfont2;
	private Font labfont3;
        private Font titlefont;
	private Font symbolfont;
	
	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);
            
            labfont = new Font("SanSerif",Font.PLAIN,state.font12);
            labfont2 = new Font("SanSerif",Font.PLAIN,state.font14);
            labfont3 = new Font("SanSerif",Font.ITALIC,state.font12);
            
            titlefont = new Font("SanSerif",Font.BOLD,state.font15);
            symbolfont = new Font("Serif",Font.PLAIN,state.font12);
	
	    titlelabel = new Label("Output",Label.LEFT);
	    add(titlelabel);
	    titlelabel.setBounds(state.s20,state.s5,state.s60,state.s20);    
	    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(state.s113,state.s6,state.s170,state.s25);
	    c1.setBackground(Color.white);
            c1.setFont(labfont3);
	    //==================================================================
	    
	    rwocA = new NewOutputCanvasA(state);
	    add(rwocA);
	    rwocA.setBounds(state.s5,state.s30,state.s200+state.s85,state.s200+state.s60);
	    
	    rwocB = new NewOutputCanvasB(state);
	    add(rwocB);
	    rwocB.setBounds(state.s5,state.s30,state.s200+state.s85,state.s200+state.s60);
	    
	    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 Font normalfont;
        private Font normalfont2;
	private Font subfont;
	private Font symbolfont;
	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);
            normalfont = new Font("SanSerif",Font.PLAIN,state.font12);
            normalfont2 = new Font("SanSerif",Font.PLAIN,state.font14);
            subfont    = new Font("SanSerif",Font.PLAIN,state.font10);
            symbolfont=new Font("Serif",Font.PLAIN,state.font12);
	
	    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 = state.s10;
	    y = state.s20;
	    dx = state.s80;
	    dxx = state.s15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    dy = state.s12;
	    dyy = dy+state.s2;
	    
	    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,state.font13));
	    g.drawString("Wavelength", x,y);
	    y +=dyy+state.s1;
	    
	    g.setColor(Color.black);
	    MaestroG.special4("\u03bb","  =  "+MaestroA.rounder(state.lambda,8)+" [ m ]","","",g,state.font13,x,y);
	    y +=dyy+state.s9;
	    
	    g.setColor(Color.blue.darker());
            g.setFont(new Font("SanSerif",Font.BOLD,state.font13));
	    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+state.s2;
            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,state.font13,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,state.font13,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,state.font13,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,state.font13,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,state.font13,x,y);
		}
		else{
		    f_normalized = state.frequency/1.0E12;
		    MaestroG.special("","f","  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]","",g,state.font13,x,y);
		}
	    y +=dyy+state.s8;
	    
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,state.font13));
	    g.drawString("Direct Path", x,y);
	    y +=dyy+state.s1;	    
	    g.setColor(Color.black);    
	    MaestroG.special3("AC = "+MaestroA.rounder(state.DirectPath_wavelength,3)," "+lambda,"  =  "
                    +MaestroA.rounder(state.DirectPath,3)+" [ m ]","",g,state.font13,x,y);
            y +=dyy+state.s7;
            
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,state.font13));
	    g.drawString("Reflected Path", x,y);
	    y +=dyy+state.s2;
	    g.setColor(Color.black);    
	    MaestroG.special3("AP = "+MaestroA.rounder(state.BounceAP_wavelength,3)," "+lambda,
                    "  =  "+MaestroA.rounder(state.BounceAP,3)+" [ m ]","",g,state.font13,x,y);
	    y +=dyy+2;
            MaestroG.special3("PC = "+MaestroA.rounder(state.BouncePC_wavelength,3)," "+lambda,
                    "  =  "+MaestroA.rounder(state.BouncePC,3)+" [ m ]","",g,state.font13,x,y);
            y +=dyy+2;
            MaestroG.special3("APC = "+MaestroA.rounder(state.BounceAP_wavelength+state.BouncePC_wavelength,3)," "+lambda,
                    "  =  "+MaestroA.rounder(state.BounceAP+state.BouncePC,3)+" [ m ]","",g,state.font13,x,y);
            y +=dyy+state.s7;
            
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,state.font13));
	    g.drawString("Ground Reflection Point", x,y);
	    y +=dyy+state.s1;
	    g.setColor(Color.black);    
	    MaestroG.special3("BP = "+MaestroA.rounder(state.BounceBP_wavelength,3)," "+lambda,
                    "  =  "+MaestroA.rounder(state.BounceBP,3)+" [ m ]","",g,state.font13,x,y);
	                
	}
}

    class NewOutputCanvasB extends Canvas{
	private Font normalfont;
	private Font subfont;
	private Font symbolfont;
	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);
            
            normalfont = new Font("SanSerif",Font.PLAIN,state.font12);
            subfont    = new Font("SanSerif",Font.PLAIN,state.font10);
            symbolfont = new Font("Serif",Font.PLAIN,state.font12);
	
	    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 = state.s10;
	    y = state.s20;
	    dx = state.s80;
	    dxx = state.s15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    //dy = fm.getHeight();
	    dy = state.s12;
	    dyy = dy+state.s2;
	    
	    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,state.font13));
	    g.drawString("Path Length Difference", x,y);
	    y +=dyy+state.s2;
	    g.setColor(Color.black);    
	    MaestroG.special4("\u0394 r"," = "+MaestroA.rounder(state.PathDifference_wavelength,3),""+lambda,"  =  "+MaestroA.rounder(state.PathDifference_meters,3)+" [ m ]",g,state.font13,x,y);g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,state.font13));
            y+=dyy+state.s8;
            
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,state.font13));
	    g.drawString("Phase Shift due to Path Difference", x,y);
	    y +=dyy+state.s2;
	    g.setColor(Color.black);           
            MaestroG.special4sub("\u0394 \u03a6","1"," = 2 \u03c0 \u0394 r / \u03bb"," = "+MaestroA.rounder(state.PhaseDifferencePath*Math.PI/180,4)+" rad",g,state.font13,x,y);
            y +=dyy+state.s2;
	    g.setColor(Color.black);           
            MaestroG.special4("         = ",""+MaestroA.rounder(state.PhaseDifferencePath,4)+" \u00ba","","",g,state.font13,x,y);
            y+=dyy+state.s8;
            
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,state.font13));
	    g.drawString("Phase Shift at Reflection Point P", x,y);
	    y +=dyy+state.s2;
	    g.setColor(Color.black);           
            if(state.GroundChoose == 1){
                if(state.IsTE){MaestroG.special4sub("\u0394 \u03a6","2",""," = - 180.0 \u00ba",g,state.font13,x,y);}
                else{MaestroG.special4sub("\u0394 \u03a6","2","","  =  0.0 \u00ba",g,state.font13,x,y);}
            }
            else{
                if(state.fase > 0.0 && state.fase < Math.PI){
                    MaestroG.special4sub("\u0394 \u03a6","2",""," = "+MaestroA.rounder(-360 + state.fase*180/Math.PI,4)+"\u00ba",g,state.font13,x,y);
                }
                else{
                    MaestroG.special4sub("\u0394 \u03a6","2",""," = "+MaestroA.rounder(state.fase*180/Math.PI,4)+"\u00ba",g,state.font13,x,y);
                }
            }
            
            y +=dyy+state.s8;
            g.setColor(Color.black);
            g.setFont(new Font("SanSerif",Font.BOLD,state.font13));
	    g.drawString("Total Phase Shift", x,y);
	    y +=dyy+state.s2;
	    g.setColor(Color.black);           
            if(state.GroundChoose == 1){
                MaestroG.special4("\u0394 \u03a6"," = "+MaestroA.rounder(state.phi_angle*180/Math.PI,4)+"\u00ba","","",g,state.font13,x,y);
            }
            else{
                MaestroG.special4("\u0394 \u03a6"," = "+MaestroA.rounder(state.phi_angle*180/Math.PI,4)+"\u00ba","","",g,state.font13,x,y);
            }
            
            y+=dyy+state.s8;
            
            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,state.font13));
                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,state.font13,x,y);
                y +=dyy+state.s3;
                MaestroG.special5("\u0393","  changes sign at this angle","","",g,state.font13,x,y);
            }
    }
}
   