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(180,147,112);
	//protected static final Color bgcolor = new Color(200,200,200);
	protected static final Color bgcolor = new Color(236,236,236);
	protected TextField text1[];
	protected Label lab1[], lab2[];
	public Label titlelabel;
	public Button b1;
        private int SCROLLMIN=0, SCROLLMAX=1001;
        private int SCROLLMIN2=0, SCROLLMAX2=3601;
        public JSlider slider1, slider2, slider3, slider4;
	protected String titulo="Unknown Title";
	protected String[] nome;
	protected String[] unidade;
	protected Font labfont;
	protected Font titlefont;
        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;
		
                labfont = new Font("SanSerif",Font.PLAIN,state.font12);
                titlefont = new Font("SanSerif",Font.BOLD,state.font16);
        
                for(i=0;i<indexsize;i++){
			this.DValue[i]=0.0;
			this.text1[i]=new TextField("0.0",8);
		}
		
		setBackground(bgcolor);
                
                int startx = state.s170;
                int starty = state.s35;
                int textwidth = state.s60;
                int textheight = state.s20;
                int yplus = state.s25;
                
                text1[0].setBounds(startx,starty,textwidth,textheight);
		text1[0].setFont(labfont);
		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[2].setBounds(startx,starty,textwidth,textheight);
		text1[2].setFont(labfont);
		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(labfont);
		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(labfont);
		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(labfont);
		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(labfont);
		add(text1[6]);
                
                Panel pt6 = new Panel();
                    pt6.setBackground(Color.black);
                    add(pt6);
                    pt6.setBounds(startx-1,starty-1,textwidth+2,textheight+2);
                
                b1 = new Button("Update");
		b1.setBackground(Color.white);
                b1.setFont(new Font("SanSerif",Font.PLAIN,state.s10));
		b1.setBounds(startx,state.s7,textwidth,textheight);
                add(b1);
                Panel pb1 = new Panel();
                    pb1.setBackground(Color.black);
                    add(pb1);
                    pb1.setBounds(startx-1,state.s7-1,textwidth+2,textheight+2);
                
                starty += yplus + state.s23;
                
                //Listeners
		b1.addActionListener(this);
                
                slider1 = new JSlider(SCROLLMIN,SCROLLMAX);
		slider1.setValue(SCROLLMAX);
                //slider1 = new Scrollbar(Scrollbar.HORIZONTAL,SCROLLMAX,1,SCROLLMIN,SCROLLMAX);
                slider1.setBackground(bgcolor);
                slider2 = new JSlider(SCROLLMIN,SCROLLMAX);
		slider2.setValue(SCROLLMAX);
                //slider2 = new Scrollbar(Scrollbar.HORIZONTAL,SCROLLMAX,1,SCROLLMIN,SCROLLMAX);
                slider2.setBackground(bgcolor);
                slider3 = new JSlider(SCROLLMIN2,SCROLLMAX2);
		slider3.setValue(1800);
                //slider3 = new Scrollbar(Scrollbar.HORIZONTAL,1800,1,SCROLLMIN2,SCROLLMAX2);
                slider3.setBackground(bgcolor);
		/*
                slider4 = new JSlider(SCROLLMIN2,SCROLLMAX2);
		slider4.setValue(SCROLLMIN);
                //slider4 = new Scrollbar(Scrollbar.HORIZONTAL,SCROLLMIN,1,SCROLLMIN2,SCROLLMAX2);
                slider4.setBackground(bgcolor);
		*/
                add(slider1); add(slider2); add(slider3);
                
                slider1.setBounds(state.s20, starty, state.s110, state.s15);
		/*
                Panel ps1 = new Panel();
                    ps1.setBackground(Color.black);
                    add(ps1);
                    ps1.setBounds(state.s20-1, starty-1, state.s110+2, state.s15+2);
		*/
                slider2.setBounds(state.s150, starty, state.s110, state.s15);
		/*
                Panel ps2 = new Panel();
                    ps2.setBackground(Color.black);
                    add(ps2);
                    ps2.setBounds(state.s150-1, starty-1, state.s110+2, state.s15+2);
		*/
                starty += yplus + state.s19;
		
                slider3.setBounds(state.s20, starty, state.s200+state.s40, state.s15);
		/*
                Panel ps3 = new Panel();
                    ps3.setBackground(Color.black);
                    add(ps3);
                    ps3.setBounds(state.s20-1, starty-1, state.s200+state.s40+2, state.s15+2);
		*/
	}
        
        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();
	    slider2.requestFocusInWindow();
	    slider3.requestFocusInWindow();
	    //slider4.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.s15;
            int starty = state.s48;
            int yplus = state.s25;         // y step down
            int xvar = state.s140;         // coordinate for long variable
            int xvar2 = state.s145;        // coordinate for short variable
            int xequal = state.s159;       // coordinate for equal sign 
            int xunit = state.s200+state.s35;        // coordinate for units
            int fonto1 = state.font11;        // font for legends
            int fonto2 = state.font12;        // font for units
            int fonto3 = state.font14;        // font for variables
            
            //g.setColor(Color.gray);
            //MaestroG.subscripterBold("Input","","",g,state.font14,startx+state.s1,startx+state.s6);
            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.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);
            
            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);
            
            starty += yplus;
            g.setColor(Color.red.darker());
            MaestroG.subscripter("Reference Amplitude","","",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("Reference 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,fonto3+2,xvar2,starty);
            MaestroG.subscripter("=","","",g,fonto2,xequal,starty);
            MaestroG.subscripterSans("[ \u03bb ]","","",g,fonto3,xunit,starty);
            
            //g.setColor(Color.lightGray);
            //g.drawLine(state.s10,starty+state.s13,getSize().width-state.s10,starty+state.s13);

            //int textwidth = state.s80;
            //int textheight = state.s20;
            //startx = state.s170;
            //g.drawRect(startx-state.s3,state.s5,textwidth-state.s15,textheight+state.s4);
            
            starty += yplus;
            startx = state.s30;
               
            g.setColor(Color.red.darker());
            double value;
            value = (double)(slider1.getValue()*0.001);
            MaestroG.subsubItalic("E","x"," = "+MaestroA.rounder(value,3)," E","o","",g,state.font13,startx,starty);
            
            value = (double)(slider2.getValue()*0.001);
            MaestroG.subsubItalic("E","y"," = "+MaestroA.rounder(value,3)," E","o","",g,state.font13,startx+state.s130,starty);
            
            starty += yplus + state.s19;
            startx = state.s70;
            g.setColor(Color.blue.darker());
            value = (double)(slider3.getValue()*0.1-180.0);
            MaestroG.subsubItalic2("\u03c6 ( E","y"," ) - \u03c6 ( E","x"," )",
                                   " = "+MaestroA.rounder(value,3),"\u00ba",g,state.font13,startx,starty);
        }
        	
	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));
	}
}

