//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.*;

public class NewGuideOutputPanel extends Panel {
    NewGuide_State state;
    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 Font labfont;
    private 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((float)state.font14);
            titlefont = state.sanSerifFont.deriveFont(Font.BOLD,(float)state.font16);
        } else {
            labfont = state.sanSerifFont.deriveFont((float)state.font12);
            titlefont = state.sanSerifFont.deriveFont(Font.BOLD,(float)state.font14);
        }
        //Font labfont=new Font("SanSerif",Font.PLAIN,state.font12);
        //Font titlefont=new Font("SanSerif",Font.BOLD,state.font16);
    
        setBackground(bgcolor);
        titlelabel = new Label(" Output",Label.LEFT);
        add(titlelabel);
        titlelabel.setBounds(state.s5,state.s5,state.s95,state.s20);    
        titlelabel.setFont(titlefont);

        // 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(state.s5,state.s25,state.s285,state.s257);

        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);
        
        g.setColor(Color.black);
        g.drawRect(0,0,getSize().width-1,getSize().height-1);
        
    }
}

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 Font normalfont;
    private 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((float)state.font14);
                subfont = state.sanSerifFont.deriveFont((float)state.font12);
            } else {
                normalfont = state.sanSerifFont.deriveFont((float)state.font12);
                subfont = state.sanSerifFont.deriveFont((float)state.font10);
            }
	}
	
	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, delta, 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 = state.s10;
            delta = state.s20;
	    y = state.s20;
	    dx = state.s80;
	    dxx = state.s15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    dy = state.s13;
	    dyy = dy+state.s2;
	    
	    g.setColor(bgcolor);
	    g.fillRect(0,0,getSize().width,getSize().height);
	    
	    g.setColor(Color.blue);
	    MaestroG.subscripter("Structure Data","","",g,state.font12,x,y);
	    
	    g.setFont(normalfont);
            g.setColor(Color.black);
		
            y+=dyy+state.s3;
            
            // frequency stuff
            g.setColor(Color.black);
            
            double frequency = state.frequency;
            int freqX = state.s150;
            int freqY = state.s11;
            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);
            }
		
            g.setColor(Color.black);
            g.drawString("d",x,y);
            g.drawString(" = "+MaestroA.rounder(state.w,5),x+state.s15,y);
            g.drawString("[mm]",x+state.s85,y);
            
            g.drawString("D / d",x+state.s150,y);
            g.drawString("  = "+MaestroA.rounder(state.a/state.w,5),x+state.s175,y);
            
            y+=dyy;
	    
            g.drawString("D",x,y);
            g.drawString(" = "+MaestroA.rounder(state.a,5),x+state.s15,y);
            g.drawString("[mm]",x+state.s85,y);
            
	    y+=state.s4;
	    
	    g.setColor(Color.red);
	    g.drawLine(state.s5,y+dyy/4,getSize().width-state.s15,y+dyy/4);
	    
	    y+=state.s5;
	    y+=dyy;
	    g.setColor(Color.blue);
	    stmp = "";
	    
            MaestroG.subscripter("Z","0",stmp,g,state.font12,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+delta,y);
		}
		else{
		    g.drawString(" =  "+MaestroA.rounder(Complex.Real(state.Z0),6)+
				" - j "+Math.abs(MaestroA.rounder(Complex.Imaginary(state.Z0),6)),x+delta,y);
		}
	    }
	    else if(state.w == state.a){
		g.drawString(" =  0.0    ",x+delta,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,state.font12,x+delta,y);

                if (MaestroG.useBiggerFont) {
                    g.setFont(state.sanSerifFont.deriveFont((float)state.font14));
                } else {
                    g.setFont(state.sanSerifFont.deriveFont((float)state.font12));
                }
	    }
	    
            if (MaestroG.useBiggerFont) {
                g.setFont(state.serifFont.deriveFont((float)state.font15));
            } else {
                g.setFont(state.serifFont.deriveFont((float)state.font13));
            }
	    g.drawString("\u03a9",x+state.s235,y);
            if (MaestroG.useBiggerFont) {
                g.setFont(state.sanSerifFont.deriveFont((float)state.font14));
            } else {
                g.setFont(state.sanSerifFont.deriveFont((float)state.font12));
            }
	    g.drawString("[ ",x+state.s225,y);
	    g.drawString(" ]",x+state.s250,y);
	        
	    y = y + dyy + state.s5;
            
	    g.setColor(Color.red);
            //stmp=Character.toString((char)0x00b4);
            stmp = "'";
	    if(state.Capacitance < 1.0E-9){
		tempcap = state.Capacitance/1.0E-12;
                MaestroG.subscripter("C","",stmp,g,state.font12,x,y);
                g.drawString(" =  "+MaestroA.rounder(tempcap,6),x+delta,y);
		g.drawString("[ pF/m ]",x+state.s225,y);
	    }
	    else if(state.Capacitance < 1.0E-6 && state.Capacitance >= 1.0E-9){
		tempcap = state.Capacitance/1.0E-9;
                MaestroG.subscripter("C'","",stmp,g,state.font12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempcap,6),x+delta,y);
		g.drawString("[ nF/m ]",x+state.s225,y);
	    }
	    else if(state.Capacitance >= 1.0E-6 && state.a != state.w){
		tempcap = state.Capacitance/1.0E-6;
                MaestroG.subscripter("C'","",stmp,g,state.font12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempcap,6),x+delta,y);
		g.drawString("[   F/m ]",x+state.s225,y);
                if (MaestroG.useBiggerFont) {
                    g.setFont(state.serifFont.deriveFont((float)state.font14));
                } else {
                    g.setFont(state.serifFont.deriveFont((float)state.font12));
                }
		g.drawString("\u03bc",x+state.s230,y);
                if (MaestroG.useBiggerFont) {
                    g.setFont(state.sanSerifFont.deriveFont((float)state.font14));
                } else {
                    g.setFont(state.sanSerifFont.deriveFont((float)state.font12));
                }
	    }
            else if(state.a == state.w){
                MaestroG.subscripter("C'","",stmp,g,state.font12,x,y);
                MaestroG.subscripterInfinityThree(" = ","","","","",g,state.font12,x+delta,y);
                g.drawString("[ F/m ]",x+state.s225,y);
            }

	    y = y + dyy + state.s2;
	    
	    if(state.Inductance < 1.0E-9){
		tempind = state.Inductance/1.0E-12;
                MaestroG.subscripter("L","",stmp,g,state.font12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempind,6),x+delta,y);
		g.drawString("[ pH/m ]",x+state.s225,y);
	    }	
	    else if(state.Inductance < 1.0E-6 && state.Inductance >= 1.0E-9){
		tempind = state.Inductance/1.0E-9;
		MaestroG.subscripter("L","",stmp,g,state.font12,x,y);
                g.drawString(" =  "+MaestroA.rounder(tempind,6),x+delta,y);
		g.drawString("[ nH/m ]",x+state.s225,y);
	    }	
	    else if(state.Inductance < 1.0E-3 && state.Inductance >= 1.0E-6){
		tempind = state.Inductance/1.0E-6;
                MaestroG.subscripter("L","",stmp,g,state.font12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempind,6),x+delta,y);
		g.drawString("[   H/m ]",x+state.s225,y);
                if (MaestroG.useBiggerFont) {
                    g.setFont(state.serifFont.deriveFont((float)state.font14));
                } else {
                    g.setFont(state.serifFont.deriveFont((float)state.font12));
                }
		g.drawString("\u03bc",x+state.s230,y);
                if (MaestroG.useBiggerFont) {
                    g.setFont(state.sanSerifFont.deriveFont((float)state.font14));
                } else {
                    g.setFont(state.sanSerifFont.deriveFont((float)state.font12));
                }
	    }
	    else if(state.Inductance >= 1.0E-3 && state.w != 0.0){
		tempind = state.Inductance/1.0E-3;
                MaestroG.subscripter("L","",stmp,g,state.font12,x,y);
		g.drawString(" =  "+MaestroA.rounder(tempind,6),x+delta,y);
		g.drawString("[ mH/m ]",x+state.s225,y);
	    }
            
            else if(state.w == 0.0){
                MaestroG.subscripter("L","",stmp,g,state.font12,x,y);
		MaestroG.subscripterInfinityThree(" = ","","","","",g,state.font12,x+delta,y);
                g.drawString("[ H/m ]",x+state.s225,y);
	    }
    
	    y = y + dyy + state.s2;
            
            if(state.w!=0.0){
                MaestroG.subscripter("R","",stmp,g,state.font12,x,y);
                g.drawString(" =  "+MaestroA.rounder(state.Resistance,6),x+delta,y);
            }
            else if(state.w == 0.0){
                MaestroG.subscripter("R","",stmp,g,state.font12,x,y);
		MaestroG.subscripterInfinityThree(" = ","","","","",g,state.font12,x+delta,y);
	    }
            g.drawString("[     /m ]",x+state.s225,y);
            if (MaestroG.useBiggerFont) {
                g.setFont(state.serifFont.deriveFont((float)state.font15));
            } else {
                g.setFont(state.serifFont.deriveFont((float)state.font13));
            }
            g.drawString("\u03a9",x+state.s230,y);
            
            if (MaestroG.useBiggerFont) {
                g.setFont(state.sanSerifFont.deriveFont((float)state.font14));
            }
            else {
                g.setFont(state.sanSerifFont.deriveFont((float)state.font12));
            }
            
	    y = y + dyy + state.s2;
            
            MaestroG.subscripter("G","",stmp,g,state.font12,x,y);
	    g.drawString(" =  "+MaestroA.rounder(state.Conductance,6),x+delta,y);
	    g.drawString("[ S/m ]",x+state.s225,y);
	    
	    y += state.s5;
	    
            g.setColor(Color.red);
	    g.drawLine(state.s5,y+dyy/4,getSize().width-state.s15,y+dyy/4);
	    
	    y = y + dyy + state.s5;
	    
	    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((float)state.font14));
                } else {
                    g.setFont(state.serifFont.deriveFont((float)state.font12));
                }
                MaestroG.subscripterInfinitySymFirst("\u03bb","0"," = ","","",g,state.font12,x+state.s5,y);
                g.drawString("[ m ]",x+state.s95,y);
            }
            else if(temp2 >= 1.0E6 && state.frequency > 0.0){
                temp = temp2/1.0E6;
                MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,3)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ 1,000 km ]",x+state.s95,y);
            }
            else if(temp2 < 1.0E6 && temp2 >= 1.0E3){
                temp = temp2/1.0E3;
                MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ km ]",x+state.s95,y);
            }
            else if(temp2 < 1.0E3 && temp2 >= 1.0E-1){
                MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp2,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ m ]",x+state.s95,y);
            }
            else if(temp2 <1.0E-1 && temp2 >= 1.0E-2){
                temp = temp2*1.0E2;
                MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ cm ]",x+state.s95,y);
            }
            else if(temp2 <1.0E2 && temp2 >= 1.0E-4){
                temp = temp2*1.0E3;
                MaestroG.subscripterSymFirst2("","0"," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ mm ]",x+state.s95,y);
            }
            else if(temp2 < 1.0E-4 && temp2 >=1.0E-6 ){
                temp = temp2*1.0E6;
                MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[  \u03bc m ]",x+state.s95,y);
            }
            else if(temp2 < 1.0E-6 ){
                temp = temp2*1.0E9;
                MaestroG.subscripterSymFirst2("\u03bb","0"," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ nm ]",x+state.s95,y);
            }
                g.drawString(" in vacuum ",x+state.s175,y);

            //wavelength in material
            y = y + dyy + state.s3;
            
            g.setColor(Color.red);
            temp2 = MaestroA.rounder(light_velocity/state.frequency,9);

            if(state.frequency == 0.0){
                MaestroG.subscripterInfinitySymFirst("\u03bb",""," = ","","",g,state.font12,x+state.s5,y);		    
                g.drawString("[ m ]",x+state.s95,y);
            }
            else if(temp2 >= 1.0E6 && state.frequency > 0.0){
                temp = temp2/1.0E6;
                MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,3)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ 1,000 km ]",x+state.s95,y);
            }
            else if(temp2 < 1.0E6 && temp2 >= 1.0E3){
                temp = temp2/1.0E3;
                MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ km ]",x+state.s95,y);
            }
            else if(temp2 < 1.0E3 && temp2 >= 1.0E-1){
                MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp2,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ m ]",x+state.s95,y);
            }
            else if(temp2 <1.0E-1 && temp2 >= 1.0E-2){
                temp = temp2*1.0E2;
                MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ cm ]",x+state.s95,y);
            }
            else if(temp2 <1.0E2 && temp2 >= 1.0E-4){
                temp = temp2*1.0E3;
                MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ mm ]",x+state.s95,y);
            }
            else if(temp2 < 1.0E-4 && temp2 >=1.0E-6 ){
                temp = temp2*1.0E6;
                MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ \u03bc m ]",x+state.s95,y);
            }
            else if(temp2 < 1.0E-6 ){
                temp = temp2*1.0E9;
                MaestroG.subscripterSymFirst2("\u03bb",""," = "+MaestroA.rounder(temp,4)+"",g,state.font12,x+state.s5,y);
                g.drawString("[ nm ]",x+state.s95,y);
            }
            g.drawString(" in guide ",x+state.s175,y);

            // Stuff at bottom
            y += state.s4;
	    g.drawLine(state.s5,y+dyy/4,getSize().width-state.s15,y+dyy/4);
	    
            y = y + dyy + state.s5;
	    
	    g.setColor(Color.blue);
            Complex alphaBeta = getAlphaBetaStuff();
            double alpha = Complex.Real(alphaBeta);
            double beta = Complex.Imaginary(alphaBeta);
            g.drawString("\u03b1",x+state.s5,y);
            g.drawString(" =  "+MaestroA.rounder(alpha,6),x+delta,y);
            g.drawString("[ Np/m ]",x+state.s225,y);
            	    
	    g.setColor(Color.red);
	    
            y = y + dyy + state.s2;
	    
            g.drawString("\u03b2",x+state.s5,y);
            g.drawString(" =  "+MaestroA.rounder(beta,6),x+delta,y);
            g.drawString("[rad/m ]",x+state.s225,y);

            /*
	    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;
    }
}
