//PC.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 PC 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[], text2[];
	protected Label lab1[], lab2[], lab3[], 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 Complex[] CValue;
	protected int indexsize;
	
	public PC(String titulo, String[] nome, String[] unidade){
		super();
		setBackground(bgcolor);
		setLayout(null);
		this.indexsize=nome.length;
		this.titulo=titulo;
		this.nome=new String[indexsize];
		this.unidade=new String[indexsize];
		this.CValue=new Complex[indexsize];
		this.text1=new TextField[indexsize];
		this.text2=new TextField[indexsize];
		this.lab1=new Label[indexsize];
		this.lab2=new Label[indexsize];
		this.lab3=new Label[indexsize];
		int i;
		
                titlelabel = new Label(titulo,Label.CENTER);
		titlelabel.setFont(titlefont);
                add(titlelabel);
                titlelabel.setBounds(150,40,60,20);
		
                for(i=0;i<indexsize;i++){
			this.nome[i]=nome[i];
			this.unidade[i]=unidade[i];
			this.CValue[i]=new Complex(0.0,0.0);

			this.text1[i]=new TextField("0.0",8);
			this.text2[i]=new TextField("0.0",8);
			this.text1[i].setBackground(Color.white);
			this.text2[i].setBackground(Color.white);
	
			this.lab1[i]=new Label(nome[i],Label.LEFT);
			this.lab2[i]=new Label("+ j",Label.CENTER);
			this.lab3[i]=new Label(unidade[i],Label.LEFT);
			this.lab1[i].setFont(labfont);
			this.lab2[i].setFont(labfont);
			this.lab3[i].setFont(labfont);
			add(text1[i]); add(text2[i]); add(lab1[i]); add(lab2[i]); add(lab3[i]); 
		}
                
		b1 = new Button("Update");
		b1.setBackground(bgcolor);
                b1.setFont(labfont);
		add(b1);
               
                //Listeners
		b1.addActionListener(this);
	}
	
	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);
            
            g.setColor(Color.red.darker());
            g.drawRect(127,37,105,25);
            g.drawRect(127,77,105,25);
            g.drawRect(127,117,105,25);
	}
	
	
	public void actionPerformed(ActionEvent evt){
		if(evt.getSource() == b1){
		    for(int i=0;i<indexsize;i++){
		  	CValue[i]=new Complex(Double.valueOf(text1[i].getText()).doubleValue(),
		                     Double.valueOf(text2[i].getText()).doubleValue());
                    }
		}
	}
	
        public Complex getValue(int i){
		return (Complex)CValue[i].clone();	
        }

	public void setValue(Complex Z,int i){
		text1[i].setText(String.valueOf(Z.Real()));
		text2[i].setText(String.valueOf(Z.Imaginary()));	
	}
}

