//PR.java
/* A Java class for
 * LineImpedance.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 PR extends Panel implements ActionListener{
	//protected static final Color bgcolor = new Color(180,147,112);
	//protected static final Color bgcolor = new Color(200,200,200);
	//private static final Color bgcolor = new Color(216,216,191);
	private static final Color bgcolor = new Color(236,236,221);
        private static final Color tinta = new Color(236,236,221);
        
	protected TextField text1[];
	protected Label lab1[], lab2[];
	public Label titlelabel;
	public Button b1;
	
	protected String titulo="Unknown Title";
	protected String[] nome;
	protected String[] unidade;
	protected static final Font labfont=TheFonts.sanSerif12;
	protected static final Font titlefont=TheFonts.bold16;
        protected double[] DValue;
	protected int indexsize;
	//GridBagLayout gb = new GridBagLayout();
	//GridBagConstraints gbc = new GridBagConstraints();
	
	public PR(String titulo, String[] nome, String[] unidade){
		super();
		//setLayout(gb);
                setLayout(null);
		this.indexsize=nome.length;
		this.titulo=titulo;
		this.nome=new String[indexsize];
		this.unidade=new String[indexsize];
		this.DValue=new double[indexsize];
		this.text1=new TextField[indexsize];
		this.lab1=new Label[indexsize];
		this.lab2=new Label[indexsize];
		int i;
		titlelabel = new Label(titulo,Label.CENTER);
		//titlelabel.setBounds(5,5,300,40);   
		titlelabel.setFont(titlefont);
                
                for(i=0;i<indexsize;i++){
			this.nome[i]=nome[i];
			this.unidade[i]=unidade[i];
			this.DValue[i]=0.0;

			this.text1[i]=new TextField("0.0",8);
			this.lab2[i]=new Label(unidade[i],Label.LEFT);
			this.lab2[i].setFont(labfont);
                        add(text1[i]);
                        add(lab2[i]);
		}
                
                
                //titlelabel.setBounds(105,10,70,20);
                titlelabel.setBounds(100,10,90,20);
                add(titlelabel);
                
                text1[0].setBounds(130,40,100,20);
                text1[1].setBounds(130,80,100,20);
                text1[2].setBounds(130,120,100,20);
                
		lab2[0].setBounds(235,40,50,20);
                //lab2[1].setBounds(245,80,30,20);
                lab2[2].setBounds(235,120,50,20);
                
		setBackground(bgcolor);
		b1 = new Button("Update");
		b1.setBackground(bgcolor);
                b1.setFont(labfont);
		b1.setBounds(130,160,100,20);
                add(b1);
                
                //Listeners
		b1.addActionListener(this);
	}
	
	public void paint(Graphics g){
                Font symbolfont=TheFonts.symbol12;
                Font normalfont=TheFonts.sanSerif12;
                Font smallfont=TheFonts.sanSerif10;
                FontMetrics fm;
	
                String alpha, Ohm, lambda, infinity, Gamma, epsilon;
                g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\u0393";
                epsilon ="\u03b5";
                g.setFont(normalfont);

	    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);
            
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            
            g.setColor(Color.black);
            MaestroG.subscripter("Z","0","",g,12,95,55);
            MaestroG.subscripterSymFirst2(""+epsilon,"r","",g,14,97,95);
            
                
            MaestroG.subscripterIT("l","","",g,16,100,135);  // lower case "L" not a one
            
            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
            
            g.setFont(normalfont);
            g.drawString("= ",115,55);
            g.drawString("= ",115,95);
            g.drawString("= ",115,135);
                        
            g.setFont(smallfont);
            g.setColor(Color.blue.darker());
            g.drawString("Characteristic",15,48);
            g.drawString("Impedance",15,61);
            
            g.setColor(Color.red.darker());
            g.drawString("Relative",15,88);
            g.drawString("Permittivity",15,101);
            
            g.setColor(Color.blue.darker());
            g.drawString("Line Length",15,135);

            
            
            g.setColor(Color.red.darker());
            g.drawRect(127,37,105,26);
            g.drawRect(127,77,105,26);
            g.drawRect(127,117,105,26);
            g.drawRect(127,157,105,25);
           

	}
	
	public void actionPerformed(ActionEvent evt){
		if(evt.getSource() == b1){
		  for(int i=0;i<indexsize;i++){
		  	DValue[i]=Double.valueOf(text1[i].getText()).doubleValue();
                  }
		}
	}
	
        public double getValue(int i){
		return DValue[i];	
        }

        public synchronized void setValue(double D,int i){
		text1[i].setText(String.valueOf(D));
	}
	
	public synchronized void setStrings(String nome[], String unidade[]){
		for(int i=0;i<nome.length;i++){
			this.nome[i]=nome[i];
			this.unidade[i]=unidade[i];
			//lab1[i].setText(nome[i]);
			lab2[i].setText(unidade[i]);
		}
		
	}
	
}

