import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PR extends Panel implements ActionListener{
	protected static final Color bgcolor = new Color(236,236,236);
	protected TextField text1[];
	protected Label lab1[], lab2[];
	public Label titlelabel;
	public Button b1;
        public JSlider slider1;
        public int SCROLLMIN=750, SCROLLMAX=1001;
        private int SleepTime = 50;
        
	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);
        protected double[] DValue;
	protected int indexsize;
        
        private Image im;
        private Graphics buf;
    
	PlaneWave_State state;
        
	public PR(String titulo, String[] nome, String[] unidade, PlaneWave_State state){
            super();
            setLayout(null);
            this.state = state;

            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.LEFT);
            //titlelabel.setBounds(0,0,50,25);   
            //titlelabel.setFont(titlefont);
            //add(titlelabel);

            for(i=0;i<indexsize;i++){
                    this.DValue[i]=0.0;
                    this.text1[i]=new TextField("0.0",8);
            }

            setBackground(bgcolor);

            int startx = state.s184;
            int starty = state.s40;
            int textwidth = state.s60;
            int textheight = state.s20;
            int yplus = state.s25;

            text1[0].setBounds(startx,starty,textwidth,textheight);
            text1[0].setFont(new Font("SanSerif",Font.PLAIN,state.font11));
            add(text1[0]);
            
            Panel pt0 = new Panel();
                    pt0.setBackground(Color.black);
                    add(pt0);
                    pt0.setBounds(startx-1,starty-1,textwidth+2,textheight+2);
                
            starty += yplus;
            text1[1].setBounds(startx,starty,textwidth,textheight);
            text1[1].setFont(new Font("SanSerif",Font.PLAIN,state.font11));
            add(text1[1]);

            Panel pt1 = new Panel();
                    pt1.setBackground(Color.black);
                    add(pt1);
                    pt1.setBounds(startx-1,starty-1,textwidth+2,textheight+2);
                
            starty += yplus;
            text1[2].setBounds(startx,starty,textwidth,textheight);
            text1[2].setFont(new Font("SanSerif",Font.PLAIN,state.font11));
            add(text1[2]);

            Panel pt2 = new Panel();
                    pt2.setBackground(Color.black);
                    add(pt2);
                    pt2.setBounds(startx-1,starty-1,textwidth+2,textheight+2);
                
            starty += yplus;
            text1[3].setBounds(startx,starty,textwidth,textheight);
            text1[3].setFont(new Font("SanSerif",Font.PLAIN,state.font11));
            add(text1[3]);

            Panel pt3 = new Panel();
                    pt3.setBackground(Color.black);
                    add(pt3);
                    pt3.setBounds(startx-1,starty-1,textwidth+2,textheight+2);
            
            starty += yplus;
            text1[4].setBounds(startx,starty,textwidth,textheight);
            text1[4].setFont(new Font("SanSerif",Font.PLAIN,state.font11));
            add(text1[4]);

            Panel pt4 = new Panel();
                    pt4.setBackground(Color.black);
                    add(pt4);
                    pt4.setBounds(startx-1,starty-1,textwidth+2,textheight+2);
            
            starty += yplus;
            text1[5].setBounds(startx,starty,textwidth,textheight);
            text1[5].setFont(new Font("SanSerif",Font.PLAIN,state.font11));
            add(text1[5]);
            
            Panel pt5 = new Panel();
                    pt5.setBackground(Color.black);
                    add(pt5);
                    pt5.setBounds(startx-1,starty-1,textwidth+2,textheight+2);
            
            starty += yplus;
            text1[6].setBounds(startx,starty,textwidth,textheight);
            text1[6].setFont(new Font("SanSerif",Font.PLAIN,state.font11));
            add(text1[6]);
            
            Panel pt6 = new Panel();
                    pt6.setBackground(Color.black);
                    add(pt6);
                    pt6.setBounds(startx-1,starty-1,textwidth+2,textheight+2);
            
            starty += yplus + state.s10;
            
            b1 = new Button("Update");
            b1.setBackground(Color.white);
            b1.setBounds(startx,starty+state.s5,textwidth,textheight);
            b1.setFont(new Font("SanSerif",Font.PLAIN,state.font11));
            add(b1);

            Panel pb1 = new Panel();
                pb1.setBackground(Color.black);
                add(pb1);
                pb1.setBounds(startx-1,starty+state.s5-1,textwidth+2,textheight+2);

	    slider1 = new JSlider(SCROLLMIN,SCROLLMAX);
	    slider1.setValue(SCROLLMAX-1-SleepTime);
	    //slider1 = new Scrollbar(Scrollbar.HORIZONTAL,SCROLLMAX-1-SleepTime,1,SCROLLMIN,SCROLLMAX);
            slider1.setBounds(state.s7,state.s200+state.s38,state.s150,state.s15);
            slider1.setBackground(bgcolor);
            add(slider1);
	    /*
            Panel p1 = new Panel();
                p1.setBackground(Color.black);
                add(p1);
                p1.setBounds(state.s7-1,state.s200+state.s38-1,state.s150+2,state.s15+2);
	    */
            //Listeners
            b1.addActionListener(this);
	}
	
	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);
	    slider1.requestFocusInWindow();
        }

            //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);
            
	    g.clearRect(0,0,getSize().width-2,getSize().height);
	                
            int startx = state.s12;
            int starty = state.s55;
            int yplus = state.s25;         // y step down
            int xvar = state.s150;         // coordinate for long variable
            int xvar2 = state.s155;        // coordinate for short variable
            int xequal = state.s170;       // coordinate for equal sign 
            int xunit = state.s200+state.s48;        // coordinate for units
            int fonto1 = state.font11;        // font for legends
            int fonto2 = state.font12;        // font for units
            int fonto3 = state.font14;        // font for variables
            int fonto6 = state.font16;        // font for variables
            
            g.setColor(Color.red.darker());
            MaestroG.subscripterBold("Input","","",g,state.font14,startx,startx+state.s5);
                
            g.setColor(Color.red.darker());
            MaestroG.subscripter("Frequency","","",g,fonto1,startx,starty);
            g.setColor(Color.black);
            MaestroG.subscripterSansItalic("f","","",g,fonto3,xvar2,starty);
            MaestroG.subscripter("=","","",g,fonto2,xequal,starty);
            MaestroG.subscripter("Hz","","",g,fonto2,xunit,starty);
            
            starty += yplus;
            g.setColor(Color.blue.darker());
            MaestroG.subscripter("Conductivity","","",g,fonto1,startx,starty);
            g.setColor(Color.black);
            MaestroG.subscripterSansItalic("\u03c3","","",g,fonto3,xvar2,starty);
            MaestroG.subscripter("=","","",g,fonto2,xequal,starty);
            MaestroG.subscripter("S/m","","",g,fonto2,xunit,starty);
            
            starty += yplus;
            g.setColor(Color.red.darker());
            MaestroG.subscripter("Relative Permittivity","","",g,fonto1,startx,starty);
            g.setColor(Color.black);
            MaestroG.subscripterSansItalic("\u03b5","r","",g,fonto3,xvar,starty);
            MaestroG.subscripter("=","","",g,fonto2,xequal,starty);
            //MaestroG.subscripter("","","",g,fonto2,xunit,starty);
            
            starty += yplus;
            g.setColor(Color.blue.darker());
            MaestroG.subscripter("Relative Permeability","","",g,fonto1,startx,starty);
            g.setColor(Color.black);
            MaestroG.subscripterSansItalicNudge("\u03bc","r","",g,fonto3,xvar,starty);
            MaestroG.subscripter("=","","",g,fonto2,xequal,starty);
            //MaestroG.subscripter("","","",g,fonto2,xunit,starty);
            
            starty += yplus;
            g.setColor(Color.red.darker());
            MaestroG.subscripter("E-field Magnitude (z=0)","","",g,fonto1,startx,starty);
            g.setColor(Color.black);
            MaestroG.subscripterSansItalic("E","o","",g,fonto3,xvar,starty);
            MaestroG.subscripter("=","","",g,fonto2,xequal,starty);
            MaestroG.subscripter("V/m","","",g,fonto2,xunit,starty);
            
            starty += yplus;
            g.setColor(Color.blue.darker());
            MaestroG.subscripter("E-field Phase (z=0)","","",g,fonto1,startx,starty);
            g.setColor(Color.black);
            MaestroG.subscripterSansItalic("\u03c6","","",g,fonto3,xvar2,starty);
            MaestroG.subscripter("=","","",g,fonto2,xequal,starty);
            MaestroG.subscripter("rad","","",g,fonto2,xunit,starty);
            
            starty += yplus;
            g.setColor(Color.red.darker());
            MaestroG.subscripter("Length Displayed","","",g,fonto1,startx,starty);
            g.setColor(Color.black);
            MaestroG.subscripterSansItalic("l","","",g,fonto6,xvar2,starty); // Lower case "L" not a "one"
            MaestroG.subscripter("=","","",g,fonto2,xequal,starty);
            MaestroG.subscripterSans("\u03bb","","",g,fonto2,xunit,starty);
            
            starty += yplus-state.s10;
            //g.setColor(Color.blue.darker());
            //MaestroG.subscripter("[A] & [B] Windows","","",g,fonto1,startx,starty);
            //g.setColor(Color.black);
            //MaestroG.subscripterSansItalic("Area","","",g,fonto3,xvar-10,starty);
            //MaestroG.subscripter("=","","",g,fonto2,xequal,starty);
            //MaestroG.subscripter("m\u00b2","","",g,fonto2,xunit,starty);
            
            int textwidth = 80;
            int textheight = 20;
            starty += yplus-10;
            startx = 180;
            
            //------------------------------------------------------------
            g.setColor(Color.black);
            int sety = state.s200+state.s65;
            int sety2 = state.s200+state.s62;
            MaestroG.subscripter("Animation speed","","",g,state.font10,state.s40,sety);
            g.setColor(Color.blue.darker());
            drawArrowNew(state.s15,sety2,2,g);
            drawArrowNew(state.s22,sety2,2,g);

            g.setColor(Color.red.darker());
            drawArrowNew(state.s142,sety2,1,g);
            drawArrowNew(state.s149,sety2,1,g);
            //--------------------------------------------------------------
        }
        
    public void drawArrowNew(int x, int y, int tipo, Graphics g){
	Graphics2D g2d = (Graphics2D)g;
	 
        switch (tipo){
	   case 1://ArrowRightSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
          	Polygon pK = new Polygon();
		pK.addPoint(x+state.s2, y-state.s2);
		pK.addPoint(x+state.s2, y+state.s2);
		pK.addPoint(x+state.s7,y);
		g.drawPolygon(pK);
		g.fillPolygon(pK);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	   
	   break;
           
           case 2://ArrowLeftSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                Polygon pL = new Polygon();
		pL.addPoint(x-state.s2, y-state.s2);
		pL.addPoint(x-state.s2, y+state.s2);
		pL.addPoint(x-state.s7,y);
		g.drawPolygon(pL);
		g.fillPolygon(pL);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
 
	   break;
          
	}		
    }  
    
        	
	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));
	}
}

