//PR2.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 PR2 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(236,236,236);
	private static final Color tinta = new Color(236,236,236);
	
	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=new Font("SanSerif",Font.PLAIN,12);
    //protected static final Font titlefont=new Font("SanSerif",Font.BOLD,16);
    //Font symbolfont=new Font("Symbol",Font.PLAIN,12);
    //Font normalfont=new Font("SanSerif",Font.PLAIN,12);
    //Font smallfont=new Font("SanSerif",Font.PLAIN,10);
    public static Font labfont, titlefont, symbolfont, normalfont, smallfont;
        protected double[] DValue;
	protected int indexsize;
	GridBagLayout gb = new GridBagLayout();
	GridBagConstraints gbc = new GridBagConstraints();
	
	public PR2(String titulo, String[] nome, String[] unidade){
		super();
		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.setFont(titlefont);
		add(titlelabel);
                
                //try{
		//MaestroG.addComponent(this,titlelabel,0,0,3,1,
		//	GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		//}  catch(Exception e){e.printStackTrace();}
		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);
                        add(text1[i]);
			//try{
			//MaestroG.addComponent(this,text1[i],1,i+1,1,1,
			//    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
			//}  catch(Exception e){e.printStackTrace();}
			this.lab1[i]=new Label(nome[i],Label.RIGHT);
			this.lab2[i]=new Label(unidade[i],Label.LEFT);
			this.lab1[i].setFont(labfont);
			this.lab2[i].setFont(labfont);
			add(lab1[i]);
                        add(lab2[i]);
			
			//try{
			//MaestroG.addComponent(this,lab1[i],0,i+1,1,1,
			//    GridBagConstraints.BOTH,GridBagConstraints.WEST);
			//} catch(Exception e){e.printStackTrace();}
			//try{
			//MaestroG.addComponent(this,lab2[i],2,i+1,1,1,
			//    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
                        //}
			//catch(Exception e){e.printStackTrace();}
		    
		}
		
		
		setBackground(bgcolor);
		b1 = new Button("Update");
		//b1.setBackground(bgcolor);
                b1.setBackground(new Color(220,220,210));
		//try{
		//MaestroG.addComponent(this,b1,1,indexsize+1,1,1,
		//	    GridBagConstraints.BOTH,GridBagConstraints.CENTER);
		//}
		//catch(Exception e){e.printStackTrace();}
		
                //Listeners
		b1.addActionListener(this);
                add(b1);
                
                
                titlelabel.setBounds(5,10,268,20);  
                
                text1[0].setBounds(130,45,80,20);
                text1[1].setBounds(130,70,80,20);
                text1[2].setBounds(130,95,80,20);
                text1[3].setBounds(130,120,80,20);
                text1[4].setBounds(130,145,80,20);
                text1[5].setBounds(130,170,80,20);
                
                lab2[0].setBounds(215,45,50,20);
                lab2[1].setBounds(215,70,60,20);
                lab2[2].setBounds(215,95,50,20);
                lab2[3].setBounds(215,120,60,20);
                lab2[4].setBounds(215,145,50,20);
                lab2[5].setBounds(215,170,50,20);
                
                b1.setBounds(130,195,80,20);
	}
	
	public void paint(Graphics g){
            
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                //Font symbolfont=new Font("Symbol",Font.PLAIN,12);
                //Font normalfont=new Font("SanSerif",Font.PLAIN,12);
                //Font smallfont=new Font("SanSerif",Font.PLAIN,10);
            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);
            
            g.setColor(Color.black);
            g.drawRect(0,0,getSize().width-1,getSize().height-1);
            
            g.setFont(normalfont);
            g.drawString("= ",115,60);
            g.drawString("= ",115,85);
            g.drawString("= ",115,110);
            g.drawString("= ",115,135);
            g.drawString("= ",115,160);
            g.drawString("= ",115,185);
            
            g.setFont(smallfont);
            g.setColor(Color.blue.darker());
            g.drawString("Resistance/m",15,60);
            MaestroG.subscripter("R","","",g,12,100,60);
            
            g.setFont(smallfont);
            g.setColor(Color.red.darker());
            g.drawString("Inductance/m",15,85);
            MaestroG.subscripter("L","","",g,12,100,85);
            
            g.setFont(smallfont);
            g.setColor(Color.blue.darker());
            g.drawString("Conductance/m",15,110);
            MaestroG.subscripter("G","","",g,12,100,110);
            
            g.setFont(smallfont);
            g.setColor(Color.red.darker());
            g.drawString("Capacitance/m",15,135);
            MaestroG.subscripter("C","","",g,12,100,135);
            
            g.setFont(smallfont);
            g.setColor(Color.blue.darker());
            g.drawString("Frequency",15,160);
            MaestroG.subscripterIT("f","o","",g,14,100,160);  

            g.setFont(smallfont);
            g.setColor(Color.red.darker());
            g.drawString("Line Length",15,185);
            MaestroG.subscripterIT("l","","",g,16,100,185);  
            MaestroG.subscripterIT("l","","",g,16,100,185);  //repaint - washed out
            g.setFont(smallfont);
            
            g.setColor(Color.red.darker());
            g.drawRect(127,42,85,175);
	}
	
	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]);
		}
		
	}
	
}

