//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,16);
	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(" Antenna Data ");
	    c1.addItem(" Field Scanner ");
	    //c1.addItem(" Discretization ");
	    //add(c1);
	    c1.setBounds(155,8,110,15);
	    
	    c1.setBackground(bgcolor.brighter());
	    
	    
	    Panel ps1 = new Panel();
	    ps1.setBackground(Color.cyan);
	    //add(ps1);
	    ps1.setBounds(153,4,104,24);
	    
	    Panel ps2 = new Panel();
	    ps2.setBackground(Color.black);
	    //add(ps2);
	    ps2.setBounds(152,3,106,26);
	    
	    rwocA = new NewOutputCanvasA(state);
	    //pane1.add(rwocA);
	    add(rwocA);
	    rwocA.setBounds(5,10,285,230);
	    
	    rwocB = new NewOutputCanvasB(state);
	    //pane2.add(rwocB);
	    add(rwocB);
	    rwocB.setBounds(5,30,285,202);
	    
	    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]==" Antenna Data "){
		    rwocA.setVisible(true);
		    rwocB.setVisible(false);
		}
		else if(ie.getSelectedObjects()[0]==" Discretization "){
		    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;
	    double tempcap, Cond1, Res1, tempind, f_normalized;
	    //Color bgcolor = Color.lightGray;
	    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 = 10;
	    dx = 80;
	    dxx = 15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    dy = 12;
	    dyy = dy+5;
	    
	    g.setFont(symbolfont);
	    g.getFontMetrics();
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    infinity="\u221e";
	    g.setFont(normalfont2);
	    g.getFontMetrics();
	    
	    g.setColor(bgcolor);
	    g.fillRect(0,0,getSize().width,getSize().height);
	    
	    //g.setColor(Color.black);
	    //g.drawLine(5,5,getSize().width-5,5);
	    
	    g.setColor(Color.blue.darker());
	    g.drawString("Wavelength", x,y);
	    y +=dyy+3;
	    
	    g.setColor(Color.black);
	    g.setFont(new Font("Serif",Font.PLAIN,15));
	    g.drawString("\u03bb",x,y);
	    g.setFont(new Font("SanSerif",Font.PLAIN,14));
	    g.drawString("   =  "+MaestroA.rounder(state.lambda,8)+"  [ m ]",x,y);
	    
	    y +=dyy+3;
	    
	    g.setColor(Color.blue.darker());
            g.setFont(new Font("SanSerif",Font.PLAIN,14));
	    g.drawString("Antenna Aperture", x,y);
	    y +=dyy+3;
	    	    
	    g.setColor(Color.black);    
	    g.setFont(new Font("Serif",Font.ITALIC,15));
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString("d",x,y);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g.setFont(new Font("SanSerif",Font.PLAIN,14));
	    g.drawString("   =  "+MaestroA.rounder(state.a,8)+"  "+lambda+"  =  "+MaestroA.rounder(state.a * state.wavelength,8)+"  [ m ]",x+5,y);
	    
	    y +=dyy-6;
	    g.setColor(Color.red);
	    g.drawLine(5,y,getSize().width-5,y);
	    y+=dyy+4;
	    
            g.setColor(Color.black);    
	    g.setFont(new Font("Serif",Font.ITALIC,15));
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(" \u03b8 ",x,y);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g.setFont(new Font("SanSerif",Font.PLAIN,14));
	    g.drawString("      =  "+MaestroA.rounder(state.theta_angle-90.0,6)+" \u00ba",x,y);
	    y+=dyy+3;
	    
            g.setColor(Color.blue.darker());
	    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            MaestroG.subscripterSymbolSup("|E (\u03b8)|","","/|E (0)|","","",g,14,x,y);
            g.setFont(new Font("SanSerif",Font.PLAIN,14));
	    y+=dyy+3;
            g.setColor(Color.black);    
	    g.drawString("=  "+MaestroA.rounder(state.ethetaS,8)+"  =  "+MaestroA.rounder(state.ethetaS_dB,4)+" dB",x,y);
	    
            y +=dyy+3;
	    
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g.setColor(Color.blue.darker());
	    MaestroG.subscripterSymbolSup("F(\u03b8)","","","","",g,14,x,y);
            MaestroG.subscripterSymbolSup("|E (\u03b8)|","","\u00b2/|E (0)|","","\u00b2",g,14,x+45,y);
            g.setFont(new Font("SanSerif",Font.PLAIN,14));
            g.drawString("   =  ",x+20,y);
            
            g.setColor(Color.black);
	    g.setFont(new Font("SanSerif",Font.PLAIN,14));
	    
            y+=dyy+3;
            if(state.ethetaS2 < 1.0E4){
                g.drawString("=  "+MaestroA.rounder(state.ethetaS2,8)+"  =  "+MaestroA.rounder(state.ethetaS_dB,4)+" dB",x,y);
            }
            else{
                g.drawString("=  "+MaestroA.rounder(state.ethetaS2,2)+"  =  "+MaestroA.rounder(state.ethetaS_dB,4)+" dB",x,y);
            }
	    //y+=dyy+3;
            //g.drawString("=  "+MaestroA.rounder(state.ethetaS_dB,4)+" dB",x+145,y);
            y+=dyy+6;
            
            g.setColor(Color.red.darker());    
	    g.setFont(new Font("SanSerif",Font.PLAIN,13));
	    g.drawString("Directivity  =  "+MaestroA.rounder(state.Directivity,6), x,y);
	               
            g.setColor(Color.black);    
	    
            
            y +=dyy+3;
	    g.setColor(Color.blue.darker());    
	    g.setFont(new Font("SanSerif",Font.PLAIN,13));
	    g.drawString("Beam width  =  "+MaestroA.rounder(state.BeamWidth,2)+"\u00b0", x,y);
	    y +=dyy+3;
	    
	}
}

    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){
	    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 = 25;
	    dx = 80;
	    dxx = 15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    //dy = fm.getHeight();
	    dy = 12;
	    dyy = dy+1;
	    
	    g.setFont(symbolfont);
	    g.getFontMetrics();
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    g.setFont(normalfont);
	    g.getFontMetrics();
	    	    
	    g.setColor(bgcolor);
	    g.fillRect(0,0,getSize().width,getSize().height);
	    
	    g.setColor(Color.black);
	    g.drawLine(5,5,getSize().width-5,5);
	    
	    g.setColor(Color.blue);
	    g.drawString("Wavelength", x,y);
	    y +=dyy+3;
	    
	    g.setColor(Color.black);
	    g.setFont(new Font("Serif",Font.PLAIN,12));
	    g.drawString("\u03bb",x+25,y);
	    g.setFont(new Font("SanSerif",Font.PLAIN,12));
	    g.drawString("      =  "+MaestroA.rounder(state.lambda,8)+"  [ m ]",x+25,y);
	    //g.drawString("[ m ]",x+185,y);
	    y +=dyy+3;
	    
	    g.setColor(Color.blue);
	    g.drawString("Antenna Length", x,y);
	    y +=dyy+3;
	    	    
	    g.setColor(Color.black);    
	    g.drawString(" L ",x+20,y);
	    g.drawString("      = "+MaestroA.rounder(state.a,8)+"  "+lambda,x+25,y);
	    y+=dyy+3;
	     
	    g.drawString("      = "+MaestroA.rounder(state.a * state.wavelength,8)+"  [ m ]",x+25,y);
	    
	    y +=dyy-8;
	    
	    g.setColor(Color.red);
	    g.drawLine(5,y,getSize().width-5,y);
	    y+=dyy;
	    y+=2;
	    g.setColor(Color.blue);
	    g.drawString("Discretization intervals for 3-D fields", x,y);
	    y +=dyy+3;
	    g.setColor(Color.black);
	    
	    MaestroG.subscripter("N","s"," = "+state.Nsections,g,12,x+25,y);
	    y+=dyy+5;
	    
	    g.setColor(Color.blue);
	    g.drawString("Length of discretization intervals", x,y);
	    y +=dyy+3;
	    g.setColor(Color.black);
	    
	    g.setFont(new Font("Serif",Font.PLAIN,12));
	    g.drawString("\u0394",x+25,y);
	    g.setFont(new Font("SanSerif",Font.PLAIN,12));
	    
	    MaestroG.subscripter("    z",""," = "+(MaestroA.rounder(state.DeltaZ,8))+"  [ m ]",g,12,x+25,y);
	    
	    y+=dyy+3;	    
	    g.setColor(Color.blue);
	    g.drawString("Discretization intervals for current plot", x,y);
	    y +=dyy+3;
	    
	    g.setColor(Color.black);
	    MaestroG.subscripter("N","c"," = "+(state.NsectionsLarge-1),g,12,x+25,y);
	    
	    y+=dyy-5;
	    
	    g.setColor(Color.red);
	    g.drawLine(5,y,getSize().width-5,y);
	    y+=dyy;
	    y+=2;
	    
	    g.setColor(Color.blue);
	    g.drawString("Assumptions", x,y);
	    y +=dyy+3;
	    
	    g.setColor(Color.black);
	    g.drawString("- Gap between radiating elements is neglected.", x,y);
	    y +=dyy+3;
	    
	    g.drawString("- Wire radius used only for reactance calculations.", x,y);
    }

}

   