//NewGuideOutputPanel.java
/* A Java class for
 * TwoWire.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.*;
import java.applet.*;
import java.lang.*;  
import java.awt.image.*;
import java.net.*;

public class NewGuideOutputPanel extends Panel {
	NewGuide_State state;
	//private static final Color bgcolor = Color.lightGray;
	private static final Color bgcolor = new Color(236,236,236);
    //private static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
    //private static final Font titlefont=new Font("SanSerif",Font.BOLD,16);
    private static  Font labfont;
    private static  Font titlefont;
	Label titlelabel;	
	public NewOutputCanvasA rwocA; 
	//public ScrollPane pane1, pane2;
	
	public NewGuideOutputPanel(NewGuide_State state){
	    super();
	    setLayout(null);
	    this.state = state;

            if (MaestroG.useBiggerFont) {
                labfont = state.sanSerifFont.deriveFont(14F);
                titlefont = state.sanSerifFont.deriveFont(Font.BOLD,18F);
            } else {
                labfont = state.sanSerifFont.deriveFont(12F);
                titlefont = state.sanSerifFont.deriveFont(Font.BOLD,16F);
            }

            setFont(labfont);  //??

	    setBackground(bgcolor);
	    titlelabel = new Label("   Output",Label.LEFT);
	    add(titlelabel);
	    titlelabel.setBounds(5,10,95,20);    
	    titlelabel.setFont(titlefont);
	    
	    //(frequency = ...).setBounds(155,6,100,20);
	    //
	    //MAYBE.setBackground(bgcolor.brighter());  // ABOVE
	    
	    
            // don't use -----------------------------
	    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,40,285,242);
	    rwocA.setBounds(5,25,285,257);
	    
	    state.ignition();
	}
	
    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);
	
    }
}

class NewOutputCanvasA 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  Font normalfont;
    private static  Font subfont;
	NewGuide_State state;
	private Image im;
	private Graphics buf;
	public NewOutputCanvasA(NewGuide_State state){
	    super();
	    this.state = state;
            if (MaestroG.useBiggerFont) {
                normalfont = state.sanSerifFont.deriveFont(14f);
                subfont = state.sanSerifFont.deriveFont(12f);
            } else {
                normalfont = state.sanSerifFont.deriveFont(12f);
                subfont = state.sanSerifFont.deriveFont(10f);
            }
	}
	
	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 tempcap, Cond1, Res1, tempind, f_normalized;
	    Color bgcolor = new Color(236,236,236);
	    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);
	    
	    x = 10;
	    y = 20;
	    dx = 80;
	    dxx = 15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    //dy = 15;
	    //dyy = dy+2;
	    dy = 13;
	    dyy = dy+2;
	    
	    g.setColor(bgcolor);
	    g.fillRect(0,0,getSize().width,getSize().height);
	    
	    g.setColor(Color.blue);
	    MaestroG.subscripter("Structure Data","","",g,12,x,y);
	    //g.drawString("Structure data:",x,y);
	   
	    g.setFont(normalfont);
            g.setColor(Color.black);
		
            //y+=dyy;
            y+=dyy+3;
		
            g.setColor(Color.black);
            g.drawString("d",x,y);
            g.drawString(" = "+MaestroA.rounder(state.w,5),x+15,y);
            g.drawString("[mm]",x+85,y);
            
            g.drawString("D / d",x+150,y);
            g.drawString("  = "+MaestroA.rounder(state.a/state.w,5),x+175,y);
            
            y+=dyy;
	    
            g.drawString("D",x,y);
            g.drawString(" = "+MaestroA.rounder(state.a,5),x+15,y);
            g.drawString("[mm]",x+85,y);
            
	    y+=4;
	    
	    g.setColor(Color.red);
	    g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	    //y+=9;
	    y+=5;
	    y+=dyy;
	    g.setColor(Color.blue);
	    stmp = "";
	    MaestroG.subscripter("Z","0",stmp,g,12,x,y);
	    
	    if(state.w < state.a && state.w > 0.0){
		if(Complex.Imaginary(state.Z0) >= 0.0){
		    g.drawString(" =  "+MaestroA.rounder(Complex.Real(state.Z0),6)+
				" + j "+MaestroA.rounder(Complex.Imaginary(state.Z0),6),x+25,y);
		    
		}
		else{
		    g.drawString(" =  "+MaestroA.rounder(Complex.Real(state.Z0),6)+
				" - j "+Math.abs(MaestroA.rounder(Complex.Imaginary(state.Z0),6)),x+25,y);
		    
		}
	    }
	    else if(state.w == state.a){
		g.drawString(" =  0.0    ",x+25,y);
	    } 
	    else if(state.w == 0.0 && state.a > 0.0){
		//g.drawString(" =  ",x+25,y);
		//g.setFont(new Font("Symbol",Font.PLAIN,12));
		//g.drawString("\u221e",x+45,y);
                MaestroG.subscripterInfinityThree(" = ","","","","",g,12,x+25,y);

                if (MaestroG.useBiggerFont) {
                    g.setFont(state.sanSerifFont.deriveFont(14f));
                } else {
                    g.setFont(state.sanSerifFont.deriveFont(12f));
                }
	    }
	    
            if (MaestroG.useBiggerFont) {
                g.setFont(state.serifFont.deriveFont(15f));
            } else {
                g.setFont(state.serifFont.deriveFont(13f));
            }
	    g.drawString("\u03a9",x+235,y);
            if (MaestroG.useBiggerFont) {
                g.setFont(state.sanSerifFont.deriveFont(14f));
            } else {
                g.setFont(state.sanSerifFont.deriveFont(12f));
            }
	    g.drawString("[ ",x+225,y);
	    g.drawString(" ]",x+250,y);
	        
	    y+=dyy;
	    y+=5;
	    g.setColor(Color.red);

            stmp=Character.toString((char)0x00b4);

	    if(state.Capacitance < 1.0E-9){
		tempcap = state.Capacitance/1.0E-12;
                MaestroG.subscripter("C","",stmp,g,12,x,y);
                g.drawString(" =  "+MaestroA.rounder(tempcap,6),x+25,y);
		g.drawString("[ pF/m ]",x+225,y);
	    }
	    else if(state.Capacitance < 1.0E-6 && state.Capacitance >= 1.0E-9){
		tempcap = state.Capacitance/1.0E-9;
                MaestroG.subscripter("C","",stmp,g,12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempcap,6),x+25,y);
		g.drawString("[ nF/m ]",x+225,y);
	    }
	    else if(state.Capacitance >= 1.0E-6 && state.a !=state.w){
		tempcap = state.Capacitance/1.0E-6;
                MaestroG.subscripter("C","",stmp,g,12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempcap,6),x+25,y);
		g.drawString("[   F/m ]",x+225,y);
                if (MaestroG.useBiggerFont) {
                    g.setFont(state.serifFont.deriveFont(14f));
                } else {
                    g.setFont(state.serifFont.deriveFont(12f));
                }
		g.drawString("\u03bc",x+230,y);
                if (MaestroG.useBiggerFont) {
                    g.setFont(state.sanSerifFont.deriveFont(14f));
                } else {
                    g.setFont(state.sanSerifFont.deriveFont(12f));
                }
	    }
            else if(state.a == state.w){
                MaestroG.subscripter("C","",stmp,g,12,x,y);
                MaestroG.subscripterInfinityThree(" = ","","","","",g,12,x+25,y);
                g.drawString("[ F/m ]",x+225,y);
            }


	    
	    y+=dyy;
	    y+=2;
	    
	    if(state.Inductance < 1.0E-9){
		tempind = state.Inductance/1.0E-12;
                MaestroG.subscripter("L","",stmp,g,12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempind,6),x+25,y);
		g.drawString("[ pH/m ]",x+225,y);
	    }	
	    else if(state.Inductance < 1.0E-6 && state.Inductance >= 1.0E-9){
		tempind = state.Inductance/1.0E-9;
		MaestroG.subscripter("L","",stmp,g,12,x,y);
                g.drawString(" =  "+MaestroA.rounder(tempind,6),x+25,y);
		g.drawString("[ nH/m ]",x+225,y);
	    }	
	    else if(state.Inductance < 1.0E-3 && state.Inductance >= 1.0E-6){
		tempind = state.Inductance/1.0E-6;
                MaestroG.subscripter("L","",stmp,g,12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempind,6),x+25,y);
		g.drawString("[   H/m ]",x+225,y);
                if (MaestroG.useBiggerFont) {
                    g.setFont(state.serifFont.deriveFont(14f));
                } else {
                    g.setFont(state.serifFont.deriveFont(12f));
                }
		g.drawString("\u03bc",x+230,y);
                if (MaestroG.useBiggerFont) {
                    g.setFont(state.sanSerifFont.deriveFont(14f));
                } else {
                    g.setFont(state.sanSerifFont.deriveFont(12f));
                }
	    }
	    else if(state.Inductance >= 1.0E-3 && state.w != 0.0){
		tempind = state.Inductance/1.0E-3;
                MaestroG.subscripter("L","",stmp,g,12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempind,6),x+25,y);
		g.drawString("[ mH/m ]",x+225,y);
	    }
            
            else if(state.w == 0.0){
                MaestroG.subscripter("L","",stmp,g,12,x,y);
		MaestroG.subscripterInfinityThree(" = ","","","","",g,12,x+25,y);
                //g.drawString(" =  "+MaestroA.rounder(tempind,6),x+25,y);
		g.drawString("[ H/m ]",x+225,y);
	    }

            
            	    
	    y+=dyy;
	    y+=2;
            
            if(state.w!=0.0){
                MaestroG.subscripter("R","",stmp,g,12,x,y);
                g.drawString(" =  "+MaestroA.rounder(state.Resistance,6),x+25,y);
                
            }
            else if(state.w == 0.0){
                MaestroG.subscripter("R","",stmp,g,12,x,y);
		MaestroG.subscripterInfinityThree(" = ","","","","",g,12,x+25,y);
        
	    }
            g.drawString("[     /m ]",x+225,y);
            if (MaestroG.useBiggerFont) {
                g.setFont(state.serifFont.deriveFont(15f));
            } else {
                g.setFont(state.serifFont.deriveFont(13f));
            }
            g.drawString("\u03a9",x+230,y);
            if (MaestroG.useBiggerFont) {
                g.setFont(state.sanSerifFont.deriveFont(14f));
            } else {
                g.setFont(state.sanSerifFont.deriveFont(12f));
            }
            
	    y+=dyy;
	    y+=2;
            MaestroG.subscripter("G","",stmp,g,12,x,y);
	    g.drawString(" =  "+MaestroA.rounder(state.Conductance,6),x+25,y);
	    g.drawString("[ S/m ]",x+225,y);
	    
	    //y+=9;
	    y+=5;
	    g.setColor(Color.red);
	    g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	    //y+=9;   
	    y+=5;
	    y+=dyy;
	    
	    g.setColor(Color.blue);

	    //wavelength in vacuum
		    g.setColor(Color.blue);
	    //wavelength in vacuum
		double temp;
		double temp2 = MaestroA.rounder(light_velocity0/state.frequency,9);
		if(state.frequency == 0.0){
                    if (MaestroG.useBiggerFont) {
                        g.setFont(state.serifFont.deriveFont(14f));
                    } else {
                        g.setFont(state.serifFont.deriveFont(12f));
                    }
                    MaestroG.subscripterInfinitySymFirst("\u03bb","0"," = ","","",g,12,x+15,y);
		    g.drawString("[ m ]",x+95,y);
		}
		else if(temp2 >= 1.0E6 && state.frequency > 0.0){
		    temp = temp2/1.0E6;
		    MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,3)+"",g,12,x+5,y);
		    g.drawString("[ 1,000 km ]",x+95,y);
		}
		else if(temp2 < 1.0E6 && temp2 >= 1.0E3){
		    temp = temp2/1.0E3;
		    MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[ km ]",x+95,y);
		}
		else if(temp2 < 1.0E3 && temp2 >= 1.0E-1){
		    MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp2,4)+"",g,12,x+5,y);
		    g.drawString("[ m ]",x+95,y);
		}
		else if(temp2 <1.0E-1 && temp2 >= 1.0E-2){
		    temp = temp2*1.0E2;
		    MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[ cm ]",x+95,y);
		}
		else if(temp2 <1.0E2 && temp2 >= 1.0E-4){
		    temp = temp2*1.0E3;
		    MaestroG.subscripterSymFirst2("","0"," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[ mm ]",x+95,y);
		}
		else if(temp2 < 1.0E-4 && temp2 >=1.0E-6 ){
		    temp = temp2*1.0E6;
		    MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[  \u03bc m ]",x+95,y);
		}
		else if(temp2 < 1.0E-6 ){
		    temp = temp2*1.0E9;
		    MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[ nm ]",x+95,y);
		}
		    g.drawString(" in vacuum ",x+175,y);
		
		//wavelength in material
		y+=dyy;
		g.setColor(Color.red);
		temp2 = MaestroA.rounder(light_velocity/state.frequency,9);
		
		if(state.frequency == 0.0){
		    MaestroG.subscripterInfinitySymFirst("\u03bb",""," = ","","",g,12,x+15,y);		    
                    g.drawString("[ m ]",x+95,y);
		}
		else if(temp2 >= 1.0E6 && state.frequency > 0.0){
		    temp = temp2/1.0E6;
		    MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,3)+"",g,12,x+5,y);
		    g.drawString("[ 1,000 km ]",x+95,y);
		}
		else if(temp2 < 1.0E6 && temp2 >= 1.0E3){
		    temp = temp2/1.0E3;
		    MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[ km ]",x+95,y);
		}
		else if(temp2 < 1.0E3 && temp2 >= 1.0E-1){
		    MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp2,4)+"",g,12,x+5,y);
		    g.drawString("[ m ]",x+95,y);
		}
		else if(temp2 <1.0E-1 && temp2 >= 1.0E-2){
		    temp = temp2*1.0E2;
		    MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[ cm ]",x+95,y);
		}
		else if(temp2 <1.0E2 && temp2 >= 1.0E-4){
		    temp = temp2*1.0E3;
		    MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[ mm ]",x+95,y);
		}
		else if(temp2 < 1.0E-4 && temp2 >=1.0E-6 ){
		    temp = temp2*1.0E6;
		    MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[ \u03bc m ]",x+95,y);
		}
		else if(temp2 < 1.0E-6 ){
		    temp = temp2*1.0E9;
		    MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,12,x+5,y);
		    g.drawString("[ nm ]",x+95,y);
		}
                g.drawString(" in guide ",x+175,y);
                
                // Janice NEW STUFF.......... START ........


            // frequency stuff
            g.setColor(Color.black);
            //double f_normalized;
            double frequency = state.frequency;
            int freqX = 155;
            int freqY = 11;
            if(frequency < 1.0E3){
                f_normalized = frequency;
                g.drawString("    f =  "+MaestroA.rounder(f_normalized,6)+"   [ Hz ]",freqX,freqY);	
            }
            else if(frequency < 1.0E6 && frequency >= 1.0E3  ){
                f_normalized = frequency/1.0E3;
                g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ kHz ]",freqX,freqY);	
            }
            else if(frequency < 1.0E9 && frequency >= 1.0E6 ){
                f_normalized = frequency/1.0E6;
                g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ MHz ]",freqX,freqY);	
            }
            else if(frequency < 1.0E12 && frequency >= 1.0E9 ){
                f_normalized = frequency/1.0E9;
                g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ GHz ]",freqX,freqY);	
            }
            else if(frequency < 1.0E15 && frequency >= 1.0E12 ){
                f_normalized = frequency/1.0E12;
                g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]",freqX,freqY);	
            }
            else{
                f_normalized = frequency/1.0E12;
                g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]",freqX,freqY);
            }

            // Stuff at bottom
            y+=4;
	    g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	    //y+=9;   
	    y+=5;
	    y+=dyy;
	    
	    g.setColor(Color.blue);
            Complex alphaBeta = getAlphaBetaStuff();
            double alpha = Complex.Real(alphaBeta);
            double beta = Complex.Imaginary(alphaBeta);
            g.drawString("\u03b1",x+5,y);
		g.drawString(" =  "+MaestroA.rounder(alpha,6),x+25,y);
		g.drawString("[ Np/m ]",x+225,y);
            	    
	    g.setColor(Color.red);
	    y+=dyy;
	    y+=2;
            g.drawString("\u03b2",x+5,y);
		g.drawString(" =  "+MaestroA.rounder(beta,6),x+25,y);
		g.drawString("[rad/m ]",x+225,y);

		// Janice NEW STUFF ...............END .............
                /*
	    g.setColor(Color.blue);
	    MaestroG.subscripter("Approximate cut-off for TE","11"," mode",g,12,x,y);
	    //g.drawString("Frequency Limit: Cut-off of TE11 mode",x,y);
	    g.setColor(Color.black);
	    y+=dyy;
	    //if(state.a > 0.0){
	    g.setColor(Color.black);
	    
	    if(state.frequency_c < 1.0E3){
		    f_normalized = state.frequency_c;
		    stmp= " =  "+MaestroA.rounder(f_normalized,6)+"   [ Hz ]";
		    MaestroG.subscripter("f","c",stmp,g,12,x,y);
		}
		else if(state.frequency_c < 1.0E6 && state.frequency_c >= 1.0E3  ){
		    f_normalized = state.frequency_c/1.0E3;
		    stmp = " =  "+MaestroA.rounder(f_normalized,6)+"   [ kHz ]";
		    MaestroG.subscripter("f","c",stmp,g,12,x,y);
		}
		else if(state.frequency_c < 1.0E9 && state.frequency_c >= 1.0E6 ){
		    f_normalized = state.frequency_c/1.0E6;
		    stmp = " =  "+MaestroA.rounder(f_normalized,6)+"   [ MHz ]";
		    MaestroG.subscripter("f","c",stmp,g,12,x,y);
		}
		else if(state.frequency_c < 1.0E12 && state.frequency_c >= 1.0E9 ){
		    f_normalized = state.frequency_c/1.0E9;
		    stmp = " =  "+MaestroA.rounder(f_normalized,6)+"   [ GHz ]";
		    MaestroG.subscripter("f","c",stmp,g,12,x,y);
		}
		else if(state.frequency_c < 1.0E200 && state.frequency_c >= 1.0E12 ){
		    f_normalized = state.frequency_c/1.0E12;
		    stmp = " =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]";
		    MaestroG.subscripter("f","c",stmp,g,12,x,y);
		}
		else{
		    stmp = " =       [ Hz ]";
		    MaestroG.subscripter("f","c",stmp,g,12,x,y);
		    g.setFont(state.symbolFont.deriveFont(12f));
		    g.drawString("\u221e",x+40,y);
		}
	    */
	}


    private Complex getAlphaBetaStuff() {
        Double n;
        n = 2*Math.PI*state.frequency;
        Complex c1 = new Complex(state.Resistance,n*state.Inductance);
        Complex c2 = new Complex(state.Conductance,n*state.Capacitance);
        Complex val = Complex.Multiply(c1,c2);
        Complex finalVal = Complex.sqrt(val);
        return finalVal;
    }
    
    
}
