//Trans_Data.java

/* A Java class for
 * LossyWide.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.*;  

public class Trans_Data extends Panel{
    //private static final Color bgcolor = new Color(180,147,112);
    //private static final Color bgcolor = Color.lightGray;
    private static final Color bgcolor = new Color(236,236,236);
    //private static final Font titlefont=new Font("SanSerif",Font.BOLD,16);
    private static Font titlefont;
    
	    
    Label titlelabel;
    public Canvasone canvas1;
    
    Trans_State state;
    public Trans_Data(Trans_State state){
	super();
	this.state = state;
        titlefont = state.ttfFont.deriveFont(Font.BOLD,16);
	titlelabel = new Label("",Label.CENTER);
	setLayout(null);
	add(titlelabel);
	titlelabel.setFont(titlefont);
    
	setBackground(bgcolor);
	canvas1 = new Canvasone(state);
	
	add(canvas1);
    
	//titlelabel.setBounds(10,5,60,19);
	canvas1.setBounds(2,2,300,33);
	
	
    }
    
    public void paint(Graphics g){
	    g.clearRect(0,0,getSize().width,getSize().height);
	    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 Canvasone extends Canvas{
    //private static final Font normalfont = new Font("SanSerif",Font.PLAIN,12);
    private static Font normalfont;
	private static String alpha, Ohm, lambda, infinity, Gamma;
	Trans_State state;
	private Image im;
	private Graphics buf;

	public Canvasone(Trans_State state){
	    super();
	    this.state = state;
            normalfont = state.ttfFont.deriveFont(12f);
	}
	
	public void paint(Graphics g){
	    if(im == null){
		im = createImage(getSize().width,getSize().height);
		buf = im.getGraphics();
		drawGraph(buf);
	    }
	    else{
		drawGraph(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 drawGraph(Graphics g){
	//public void paint(Graphics g){
	    Color maroon = new Color(185,50,50);
	    int x, y, dx, dy, dyy;
	    double temp;
	    Complex tempc;
	    FontMetrics fm;
	    x = 30;
	    y = 12;
	    dy = 15;
	    
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
	    g.setFont(normalfont);
	    fm = g.getFontMetrics();
	    
	    g.clearRect(0,0,getSize().width,getSize().height);
	    g.setColor(Color.red);
	    
	    //g.setFont(symbolfont);
	    alpha="\u03b1";
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    infinity="\u221e";
	    Gamma="\uu0393"; 
	    g.setFont(normalfont);
	    
	    int xstart;
	    
	
		String stalpha, stalpha2;
		
		if(state.IsLowLoss){
		    if(state.alpha<=0.0){g.setColor(Color.blue); stalpha = "LOSS-LESS APPROXIMATION";}
		    else{
                        //g.setColor(maroon); stalpha = "LOW-LOSS APPROXIMATION";
                        g.setColor(maroon); stalpha = "LOSS-LESS APPROXIMATION";
                    }
		}
		else{g.setColor(Color.red);stalpha = "GENERAL LOSSY LINE";}
		xstart = (getSize().width - fm.stringWidth(stalpha))/2;
		g.drawString(stalpha,xstart,y);
		
		y+=dy;
		g.setColor(Color.black);
		if(state.alpha < 1.0){
		    stalpha2 =" = "+MaestroA.rounder(state.alpha/state.wavelength,5)+" [ Ne/m ] = "
				+MaestroA.rounder(state.alpha,5)+" [ Ne/ ";
		    xstart = (getSize().width - fm.stringWidth(stalpha2+""+lambda+"     ]"))/2;
		    //g.drawString(stalpha2,xstart,y);
                    //MaestroG.subscripterSymFirst2(""+alpha,"",""+stalpha2,g,12,xstart,y);
                    MaestroG.subscripterSymFirstBeforeLast(""+alpha,"",""+stalpha2,""+lambda," ]",g,12,xstart,y);
		}
		else{
		    stalpha2 =" = "+MaestroA.rounder(state.alpha/state.wavelength,3)+" [ Ne/m ] = "
				+MaestroA.rounder(state.alpha,3)+" [ Ne/ ";
		    xstart = (getSize().width - fm.stringWidth(stalpha2+""+lambda+"     ]"))/2;
		    //g.drawString(stalpha2,xstart,y);
                    //MaestroG.subscripterSymFirst2(""+alpha,"",""+stalpha2,g,12,xstart,y);
                    MaestroG.subscripterSymFirstBeforeLast(""+alpha,"",""+stalpha2,""+lambda," ]",g,12,xstart,y);
                }
	}
}

