//NewGuideOutputPanel.java

/* A Java class for
 * Coaxial.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 NewOutputCanvasB rwocB; 
	//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);
	    
	    //pane1 = new ScrollPane();
	    //pane1.setBounds(2,30,297,285);
	    //add(pane1);
	    
	    //pane2  = new ScrollPane();
	    //pane2.setBounds(2,30,297,205);
	    //add(pane2);

	    
	    // 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);
	    
	    rwocB = new NewOutputCanvasB(state);
	    //pane2.add(rwocB);
	    add(rwocB);
	    rwocB.setBounds(5,40,285,242);
	    
	    rwocA.setVisible(true);
	    rwocB.setVisible(false);
	    //pane1.setVisible(true);
	    //pane2.setVisible(false);
	    
	    //Listeners
	    //c1.addItemListener(this);
	    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 = Color.lightGray;
	    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);
	    
            //Graphics2D g2d = (Graphics2D)g;
            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
 
	    x = 10;
	    y = 10;
	    dx = 80;
	    dxx = 15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    dy = 15;
	    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.magenta);
		g.setColor(Color.black);
		
		y+=dyy;
		
		g.setColor(Color.black);
		g.drawString("a",x,y);
		g.drawString(" = "+MaestroA.rounder(state.w,5),x+15,y);
		g.drawString("[mm]",x+85,y);
                
                g.drawString("b / a",x+150,y);
		if(state.a!=0.0 && state.w == 0.0){
                    MaestroG.subscripterInfinityThree("  = ","","","","",g,12,x+175,y);
                }
                else{
                    g.drawString("  = "+MaestroA.rounder(state.a/state.w,5),x+175,y);
                }
                
		y+=dyy;
	    
		g.drawString("b",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+=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),8),x+25,y);
		    
		}
		else{
		    g.drawString(" =  "+MaestroA.rounder(Complex.Real(state.Z0),6)+
				" - j "+Math.abs(MaestroA.rounder(Complex.Imaginary(state.Z0),8)),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("Serif",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;
	    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;
	    
	    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("[ H/m ]",x+225,y);
	    }
	    
	    y+=dyy;
	    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;
            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+=4;
	    g.setColor(Color.red);
	    g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	    y+=4;   
	    y+=dyy;
	    
	    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;
                    if (MaestroG.useBiggerFont) {
                        g.setFont(state.sanSerifFont.deriveFont(14f));
                    } else {
                        g.setFont(state.sanSerifFont.deriveFont(12f));
                    }
		    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"," = ","","",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);
		
	    y+=4;
	    g.setColor(Color.red);
	    g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	    y+=dyy;
	    y+=4;  
	    
            // Janice Richards new stuff here ---------------------
            g.setColor(Color.red);
            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.blue);
	    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);
		    //g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ Hz ]",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);
		    //g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ kHz ]",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);
		    //g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ MHz ]",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);
		    //g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ GHz ]",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);
		    //g.drawString("    f  =  "+MaestroA.rounder(f_normalized,6)+"   [ THz ]",x,y);	
		}
		else{
		    stmp = " =  ";
		    MaestroG.subscripter("f","c",stmp,g,12,x,y);
                    MaestroG.subscripterInfinityThree(" =  ","",""+stmp,"","  [ Hz ]",g,12,x,y);
                
		    //g.setFont(state.serifFont.deriveFont(12f));
		    //g.drawString("\u221e",x+40,y);
		    
		    //g.drawString("    f  =  \u221e",x,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;
    }

}
    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  Font normalfont;
        private static  Font subfont;        
	NewGuide_State state;
	private Image im;
	private Graphics buf;
	public NewOutputCanvasB(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 v_normalized;
	    //Color bgcolor = Color.lightGray;
	    Color bgcolor = new Color(236,236,236);
	    String stmp;
	    FontMetrics fm;
	    
	    x = 10;
	    y = 15;
	    dx = 80;
	    dxx = 15;
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    dy = fm.getHeight();
	    dyy = dy+2;
	    
	    g.setColor(bgcolor);
	    g.fillRect(0,0,getSize().width,getSize().height);
	    
	    
	    //g.setColor(Color.red);
	    //g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	    //y+=dyy;
	    //y+=2;
	    
	    g.setColor(Color.blue);
	    g.drawString("Substrate Loss Tangent",x,y);
	    
	    y+=dyy;
	    g.setColor(Color.black);
	    stmp="";
	    
	    MaestroG.subscripterSanSym2("tan","","","\u03b4",g,12,x+20,y);
	    g.drawString("",x+175,y);
	    if(state.loss_tangent > 1.0e-5){
		stmp=" = "+MaestroA.rounder(state.loss_tangent,6);
	    }
	    else{
		stmp=" = "+MaestroA.rounder(state.loss_tangent,10);
	    }
	    
	    MaestroG.subscripter("","",stmp,g,12,x+45,y);
	    
	    y+=4;
	    g.setColor(Color.red);
	    g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	    y+=4;
	    
	    y+=dyy;
	    g.setColor(Color.blue);
	    g.drawString("Metal Skin Depth  ",x,y);
	    y+=dyy;
	    g.setColor(Color.black);
	    if(!state.IsMilsOn){
		stmp = "";
		MaestroG.subscripterSymFirst2("\u03b4","s",stmp,g,14,x+25,y);
		
                stmp = " = "+MaestroA.rounder(state.skin_depth*1000.0,7)+"";
		MaestroG.subscripter("","",stmp,g,12,x+45,y);
		g.drawString("[ mm ]",x+175,y);
	    }
	    else{
		stmp = "";
		MaestroG.subscripterSymFirst2("\u03b4","s",stmp,g,14,x+25,y);
		
		stmp = " = "+MaestroA.rounder(state.skin_depth*1000.0/0.0254,7)+"";
		MaestroG.subscripter("","",stmp,g,12,x+45,y);
		g.drawString("[ mils ]",x+175,y);
	    }
	    y+=dyy;
	    g.setColor(Color.blue);
	    g.drawString("Sheet Resistance",x,y);
	    y+=dyy;
	    g.setColor(Color.black);
	    stmp = "";
	    MaestroG.subscripter("R","s",stmp,g,12,x+25,y);
	    stmp = " = "+MaestroA.rounder(state.Resistance_surf,7)+"";
	    MaestroG.subscripter("","",stmp,g,12,x+45,y);
            if (MaestroG.useBiggerFont) {
                g.setFont(state.serifFont.deriveFont(15f));
            } else {
                g.setFont(state.serifFont.deriveFont(13f));
            }
	    g.drawString("\u03a9",x+180,y);
            if (MaestroG.useBiggerFont) {
                g.setFont(state.sanSerifFont.deriveFont(14f));
            } else {
                g.setFont(state.sanSerifFont.deriveFont(12f));
            }
	    g.drawString("[ ",x+175,y);
	    g.drawString(" /sq ]",x+190,y);
	    
	    y+=5;
	    g.setColor(Color.red);
	    g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	    y+=dyy;
	    y+=5;
	    	   
	    g.setColor(Color.blue);
	    g.drawString("Power attenuation",x,y);
	    
	   // y+=dyy;
	    g.setColor(Color.black);
	    //g.drawString("P( z )",x+122,y);
	    stmp = " z )";
	    MaestroG.subscripter("P(z) = P(0) exp(-2 \u03b1","tot",stmp,g,12,x+110,y);
	    //g.setFont(state.serifFont.deriveFont(12f));
	    //g.drawString("\u03b1",x+226,y);
            if (MaestroG.useBiggerFont) {
                g.setFont(state.sanSerifFont.deriveFont(14f));
            } else {
                g.setFont(state.sanSerifFont.deriveFont(12f));
            }
	    
	    y+=dyy;
	    g.setColor(Color.blue);
	    g.drawString("where",x+80,y);
	    g.setColor(Color.black);
	    stmp=" = ";
	    MaestroG.subscripterSymFirst2("\u03b1","tot",stmp,g,12,x+125,y);
	    stmp = " + ";
	    MaestroG.subscripterSymFirst2("\u03b1","d",stmp,g,12,x+165,y);
	    stmp="";
	    MaestroG.subscripterSymFirst2("\u03b1","c",stmp,g,12,x+195,y);
	    
	    
	    y+=dyy;
	    g.setColor(Color.red);
	    g.drawString("Dielectric loss attenuation",x,y);
	    g.setColor(Color.black);
	    y+=dyy;
	    stmp = " = "+MaestroA.rounder(state.alpha_d*8.686,6)+" dB/m = "
			+MaestroA.rounder(state.alpha_d,6)+" Ne/m";
	    MaestroG.subscripterSymFirst2("\u03b1","d",stmp,g,12,x+10,y);
	    
	    y+=dyy;
	    
	    g.setColor(Color.red);
	    g.drawString("Conductor loss attenuation",x,y);
	    g.setColor(Color.black);
	    y+=dyy;
	    if(state.w==0.0){
		MaestroG.subscripterInfinitySymFirst("\u03b1","c"," = ","","",g,12,x+10,y);
	    }
	    else{
		stmp = " = "+MaestroA.rounder(state.alpha_c*8.686,6)+" dB/m = "
			    +MaestroA.rounder(state.alpha_c,6)+" Ne/m";
		MaestroG.subscripterSymFirst2("\u03b1","c",stmp,g,12,x+10,y);
		
	    }
	    //y+=4;
	    //g.setColor(Color.red);
	    //g.drawLine(5,y+dyy/4,getSize().width-15,y+dyy/4);
	    	    
    }

}
