import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class InputPanel extends Panel implements ChangeListener, ItemListener{
        PlaneWave_State state;
        
	protected static final Color bgcolor = new Color(246,246,246);
	protected TextField text1[];
	protected Label lab1[], lab2[];
	public Label titlelabel;
	public Button b1, about;
        private Image im;
        private Graphics buf;
        public JSlider slider1,sliderx0, slidery0;
        public JSlider slidervx0, slidervy0;
        public JSlider slidermass, slidercharge;
        public JSlider sliderEx, sliderEy;
        public JSlider sliderHz;
        public JSlider sliderTime1, sliderTime2;
        
        public Checkbox c1, c2, c3, c_trace, c_field;
        public CheckboxGroup cgrid;
        
        public Checkbox c_open, c_closed;
        public CheckboxGroup cbound;
        
        public int SCROLLMIN=750, SCROLLMAX=1001;
        public int SCROLLMIN2=0, SCROLLMAX2=20001;
        public int SCROLLMIN3=0, SCROLLMAX3=1001;
        
        private int SleepTime = 50;
	protected String titulo="Unknown Title";
	protected String[] nome;
	protected String[] unidade;
	protected Font labfont;
	protected Font titlefont;
        protected double[] DValue;
	protected int indexsize = 9;
	
	public InputPanel(PlaneWave_State state){
		super();
		setLayout(null);
                this.state = state;
                
                labfont = new Font("SanSerif",Font.PLAIN,state.font12);
                titlefont = new Font("SanSerif",Font.BOLD,state.font16);
        
		DValue=new double[indexsize];
		text1=new TextField[indexsize];
		int i;
		
                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(labfont);
		//add(text1[0]);
                
                starty += yplus;
                text1[1].setBounds(startx,starty,textwidth,textheight);
                text1[1].setFont(labfont);
		//add(text1[1]);
                
                starty += yplus;
                text1[2].setBounds(startx,starty,textwidth,textheight);
                text1[2].setFont(labfont);
		//add(text1[2]);
                
                starty += yplus;
                text1[3].setBounds(startx,starty,textwidth,textheight);
                text1[3].setFont(labfont);
		//add(text1[3]);
                
                starty += yplus;
                text1[4].setBounds(startx,starty,textwidth,textheight);
                text1[4].setFont(labfont);
		//add(text1[4]);
                
                starty += yplus;
                text1[5].setBounds(startx,starty,textwidth,textheight);
                text1[5].setFont(labfont);
		//add(text1[5]);

                starty += yplus;
                text1[6].setBounds(startx,starty,textwidth,textheight);
                text1[6].setFont(labfont);
		//add(text1[6]);
                
                starty += yplus;
                //text1[7].setBounds(startx,starty,textwidth,textheight);
		//add(text1[7]);
                
                starty += yplus+state.s93;
                
		about = new Button("Instructions");
		about.setBackground(Color.white);
		about.setBounds(startx,state.s300+state.s76,state.s100,textheight);
                about.setFont(new Font("SanSerif",Font.PLAIN,state.s11));
                add(about);
                
                Panel pb1 = new Panel();
                pb1.setBackground(Color.lightGray);
                add(pb1);
                pb1.setBounds(startx-1,state.s300+state.s76-1,state.s100+2,textheight+2);
	    
                Panel pb2 = new Panel();
                pb2.setBackground(Color.black);
                add(pb2);
                pb2.setBounds(startx-2,state.s300+state.s76-2,state.s100+4,textheight+4);	
                
                c_field=new Checkbox("Show Fields",true);
                add(c_field);
		c_field.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		c_field.setBackground(bgcolor);
		c_field.setForeground(Color.red.darker());
		c_field.setBounds(state.s90,state.s5,state.s100,state.s20);
                c_field.setVisible(true);
                
                c_trace=new Checkbox("Show Trace",false);
                add(c_trace);
		c_trace.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		c_trace.setBackground(bgcolor);
		c_trace.setForeground(Color.red.darker());
		c_trace.setBounds(state.s200,state.s5,state.s100,state.s20);
                c_trace.setVisible(true);

		/*
                cgrid = new CheckboxGroup();
                c1=new Checkbox("electron",cgrid,true);
                c2=new Checkbox("proton",cgrid,false);
                c3=new Checkbox("neutron",cgrid,false);
                
                //add(c1); // electron
		c1.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		c1.setBackground(bgcolor);
		c1.setForeground(Color.red.darker());
		c1.setBounds(state.s20,state.s165,state.s80,state.s20);
                c1.setVisible(true);
                
                //add(c2); // proton
		c2.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		c2.setBackground(bgcolor);
		c2.setForeground(Color.magenta.darker());
                c2.setBounds(state.s120,state.s165,state.s80,state.s20);
                c2.setVisible(true);
                
                //add(c3); // neutron
		c3.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		c3.setBackground(bgcolor);
		c3.setForeground(Color.black);
		c3.setBounds(state.s200+state.s20,state.s165,state.s80,state.s20);
                c3.setVisible(true);
		*/
                cbound = new CheckboxGroup();
                c_open = new Checkbox("open boundary",cbound,true);
                c_closed = new Checkbox("closed boundary",cbound,false);
                
                add(c_closed);
		c_closed.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		c_closed.setBackground(bgcolor);
		c_closed.setForeground(Color.red.darker());
                c_closed.setBounds(state.s170,state.s200+state.s40,state.s150,state.s20);
                c_closed.setVisible(true);
                
                add(c_open);
		c_open.setFont(new Font("SanSerif",Font.PLAIN,state.font12));
		c_open.setBackground(bgcolor);
		c_open.setForeground(Color.red.darker());
		c_open.setBounds(state.s170,state.s200+state.s60,state.s150,state.s20);
                c_open.setVisible(true);
                
                // Animation speed slider
                slider1 = new JSlider(SCROLLMIN,SCROLLMAX);
		slider1.setValue(SCROLLMAX-1-SleepTime);
                //slider1 = new Scrollbar(Scrollbar.HORIZONTAL,SCROLLMAX-1-SleepTime,1,SCROLLMIN,SCROLLMAX);
                slider1.setBounds(state.s12,state.s300+state.s78,state.s150,state.s15);
                slider1.setBackground(bgcolor);
                add(slider1);
		/*
                Panel p1 = new Panel();
                    p1.setBackground(Color.black);
                    add(p1);
                    p1.setBounds(state.s12-1,state.s300+state.s78-1,state.s150+2,state.s15+2);
		*/
                sliderx0 = new JSlider(SCROLLMIN2,SCROLLMAX2);
		sliderx0.setValue((SCROLLMAX2-1)/2);
                //sliderx0 = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAX2-1)/2,1,SCROLLMIN2,SCROLLMAX2);
                sliderx0.setBounds(state.s12,state.s50,state.s120,state.s15);
                sliderx0.setBackground(bgcolor);
                add(sliderx0);
		/*
                Panel px = new Panel();
                    px.setBackground(Color.black);
                    add(px);
                    px.setBounds(state.s12-1,state.s50-1,state.s120+2,state.s15+2);
		*/
		slidery0 = new JSlider(SCROLLMIN2,SCROLLMAX2);
		slidery0.setValue((SCROLLMAX2-1)/2);
		//slidery0 = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAX2-1)/2,1,SCROLLMIN2,SCROLLMAX2);
                slidery0.setBounds(state.s162,state.s50,state.s120,state.s15);
                slidery0.setBackground(bgcolor);
                add(slidery0);
		/*
                Panel py = new Panel();
                    py.setBackground(Color.black);
                    add(py);
                    py.setBounds(state.s162-1,state.s50-1,state.s120+2,state.s15+2);
		*/
                slidervx0 = new JSlider(SCROLLMIN2,SCROLLMAX2);
		slidervx0.setValue((SCROLLMAX2-1)/2+1000);
                //slidervx0 = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAX2-1)/2+1000,1,SCROLLMIN2,SCROLLMAX2);
                slidervx0.setBounds(state.s12,state.s95,state.s120,state.s15);
                slidervx0.setBackground(bgcolor);
                add(slidervx0);
		/*
                Panel pvx = new Panel();
                    pvx.setBackground(Color.black);
                    add(pvx);
                    pvx.setBounds(state.s12-1,state.s95-1,state.s120+2,state.s15+2);
		*/
                slidervy0 = new JSlider(SCROLLMIN2,SCROLLMAX2);
		slidervy0.setValue((SCROLLMAX2-1)/2+1000);
                //slidervy0 = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAX2-1)/2+1000,1,SCROLLMIN2,SCROLLMAX2);
                slidervy0.setBounds(state.s162,state.s95,state.s120,state.s15);
                slidervy0.setBackground(bgcolor);
                add(slidervy0);
		/*
                Panel pvy = new Panel();
                    pvy.setBackground(Color.black);
                    add(pvy);
                    pvy.setBounds(state.s162-1,state.s95-1,state.s120+2,state.s15+2);
		*/
                slidermass = new JSlider(1,11);
		slidermass.setValue(1);
                //slidermass = new Scrollbar(Scrollbar.HORIZONTAL,1,1,1,11);
                slidermass.setBounds(state.s12,state.s140,state.s120,state.s15);
                slidermass.setBackground(bgcolor);
                //add(slidermass);
		/*
                Panel pvm = new Panel();
                    pvm.setBackground(Color.black);
                    //add(pvm);
                    pvm.setBounds(state.s12-1,state.s140-1,state.s120+2,state.s15+2);
		*/
                slidercharge = new JSlider(0,21);
		slidercharge.setValue((21-1)/2);
                //slidercharge = new Scrollbar(Scrollbar.HORIZONTAL,(21-1)/2-1,1,0,21);
                slidercharge.setBounds(state.s162,state.s140,state.s120,state.s15);
                slidercharge.setBackground(bgcolor);
                //add(slidercharge);
		/*
                Panel pvc = new Panel();
                    pvc.setBackground(Color.black);
                    //add(pvc);
                    pvc.setBounds(state.s162-1,state.s140-1,state.s120+2,state.s15+2);
		*/ 
                
                sliderEx = new JSlider(SCROLLMIN3,SCROLLMAX3);
		sliderEx.setValue((SCROLLMAX3-1)/2+100);
                //sliderEx = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAX3-1)/2+100,1,SCROLLMIN3,SCROLLMAX3);
                if(state.IsMac){
                    sliderEx.setBounds(state.s12+state.s14,state.s200+state.s15,state.s120-state.s28,state.s15);
                }
                else{
                    sliderEx.setBounds(state.s12,state.s200+state.s15,state.s120,state.s15);
                }
                
                sliderEx.setBackground(bgcolor);
                add(sliderEx);
		/*
                Panel pEx = new Panel();
                    pEx.setBackground(Color.red);
                    add(pEx);
                    pEx.setBounds(state.s12-1,state.s200+state.s15-1,state.s120+2,state.s15+2);
		*/
		sliderEy = new JSlider(SCROLLMIN3,SCROLLMAX3);
		sliderEy.setValue((SCROLLMAX3-1)/2+100);
		// sliderEy = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAX3-1)/2+100,1,SCROLLMIN3,SCROLLMAX3);
                
                if(state.IsMac){
                    sliderEy.setBounds(state.s162+state.s14,state.s200+state.s15,state.s120-state.s28,state.s15);
                }
                else{
                    sliderEy.setBounds(state.s162,state.s200+state.s15,state.s120,state.s15);
                }
                
                sliderEy.setBackground(bgcolor);
                add(sliderEy);
		sliderEy.setForeground(Color.red); // ???
		/*
                Panel pEy = new Panel();
                    pEy.setBackground(Color.red);
                    add(pEy);
                    pEy.setBounds(state.s162-1,state.s200+state.s15-1,state.s120+2,state.s15+2);
		*/
                
                sliderHz = new JSlider(SCROLLMIN3,SCROLLMAX3);
		sliderHz.setValue((SCROLLMAX3-1)/2+100);
                //sliderHz = new Scrollbar(Scrollbar.HORIZONTAL,(SCROLLMAX3-1)/2+100,1,SCROLLMIN3,SCROLLMAX3);
                
                if(state.IsMac){
                    sliderHz.setBounds(state.s12+state.s14,state.s200+state.s60,state.s120-state.s28,state.s15);
                }
                else{
                    sliderHz.setBounds(state.s12,state.s200+state.s60,state.s120,state.s15);
                }
                
                sliderHz.setBackground(bgcolor);
                add(sliderHz);
		sliderHz.setForeground(Color.blue); // ???
		/*
                Panel pHz = new Panel();
                    pHz.setBackground(Color.blue);
                    add(pHz);
                    pHz.setBounds(state.s12-1,state.s200+state.s60-1,state.s120+2,state.s15+2);
		*/

		sliderTime1 = new JSlider(0,90);
		sliderTime1.setValue(0);
                //sliderTime1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,90);
                sliderTime1.setBounds(state.s12,state.s200+state.s95,state.s120,state.s15);
                sliderTime1.setBackground(bgcolor);
                add(sliderTime1);
		sliderTime1.setForeground(Color.green.darker()); // ????
		/*
                Panel pT1 = new Panel();
                    pT1.setBackground(Color.green.darker());
                    add(pT1);
                    pT1.setBounds(state.s12-1,state.s200+state.s95-1,state.s120+2,state.s15+2);
		*/
		sliderTime2 = new JSlider(1,21);
		sliderTime2.setValue(12);
                //sliderTime2 = new Scrollbar(Scrollbar.HORIZONTAL,12,1,1,21);
                sliderTime2.setBounds(state.s12,state.s300+state.s13,state.s120,state.s15);
                sliderTime2.setBackground(bgcolor);
                add(sliderTime2);
		sliderTime2.setForeground(Color.green.darker()); // ????
		/*
                Panel pT2 = new Panel();
                    pT2.setBackground(Color.green.darker());
                    add(pT2);
                    pT2.setBounds(state.s12-1,state.s300+state.s13-1,state.s120+2,state.s15+2);
		*/
		
                // draw line with panel to avoid flicker
                    Panel pLine = new Panel();
                    pLine.setBackground(Color.black);
                    add(pLine);
                    pLine.setBounds(0,state.s187,state.s300+state.s6,state.s1);
                    
                //Listeners
                //c1.addItemListener(this);
                //c2.addItemListener(this);
                //c3.addItemListener(this);
                c_open.addItemListener(this);
                c_closed.addItemListener(this);
                sliderx0.addChangeListener(this);
                slidery0.addChangeListener(this);
                slidervx0.addChangeListener(this);
                slidervy0.addChangeListener(this);
                slidermass.addChangeListener(this);
                slidercharge.addChangeListener(this);
                sliderEx.addChangeListener(this);
                sliderEy.addChangeListener(this);
                sliderHz.addChangeListener(this);
                sliderTime1.addChangeListener(this);
                sliderTime2.addChangeListener(this);
                        
	}
	
        @Override
        public void paint(Graphics g){
            if(im == null){
                im = createImage(getSize().width,getSize().height);
                buf = im.getGraphics();
                drawCanvas(buf);
		g.drawImage(im,0,0,null);
            }
            else{
                //drawCanvas(buf);
		UpdateStuff(g);
            }

	    slider1.requestFocusInWindow();
	    sliderx0.requestFocusInWindow();
	    slidery0.requestFocusInWindow();
	    slidervx0.requestFocusInWindow();
	    slidervy0.requestFocusInWindow();
	    slidermass.requestFocusInWindow();
	    slidercharge.requestFocusInWindow();
	    sliderEx.requestFocusInWindow();
	    sliderEy.requestFocusInWindow();
	    sliderHz.requestFocusInWindow();
	    sliderTime1.requestFocusInWindow();
	    sliderTime2.requestFocusInWindow();
        }

    

    // Need this, because otherwise lightweight Swing slider disappears
    //  after 1st call to repaint():
    public void UpdateStuff(Graphics g) {
            int startx = state.s12;
            int starty = state.s40;
            int yplus = state.s25;         // y step down

	    // Clear this area first (w/o clearing entire panel, b/c of problem with jsliders disappearing ...
	    g.setColor(bgcolor);
	    g.fillRect(startx,starty-state.s15,state.s140,state.s20);
	    g.fillRect(startx+state.s150,starty-state.s15,state.s140,state.s20);
	    
	    g.setColor(Color.blue.darker());
            MaestroG.subscripter("x","0"," = "+MaestroA.rounder(state.xpos_0,4)+" [ m ]",g,state.font12,startx,starty);
            MaestroG.subscripter("y","0"," = "+MaestroA.rounder(state.ypos_0,4)+" [ m ]",g,state.font12,startx+state.s150,starty);
            
            starty = state.s85;
	    // Clear this area first
	    g.setColor(bgcolor);
	    g.fillRect(startx,starty-state.s15,state.s140,state.s20);
	    g.fillRect(startx+state.s150,starty-state.s15,state.s140,state.s20);

	    g.setColor(Color.blue.darker());
	    MaestroG.subsup("u","x 0"," = "+MaestroA.rounder(state.vx_0,4)+" x 10","5","m/s",g,state.font12,startx,starty);
            MaestroG.subsup("u","y 0"," = "+MaestroA.rounder(state.vy_0,4)+" x 10","5","m/s",g,state.font12,startx+state.s150,starty);
            
            starty = state.s157;
            
	    // Clear this area first
	    g.setColor(bgcolor);
	    g.fillRect(startx+state.s10,starty-state.s15,state.s140,state.s20);
	    g.fillRect(startx+state.s10,starty+state.s20-state.s15,state.s140,state.s20);

            if(state.particle == 1){
                g.setColor(Color.red.darker());
                MaestroG.subscripter("Electron","","",g,state.font12,startx+state.s10,starty-state.s20);
                MaestroG.superscripter("mass = 9.10938188 x 10","-31"," [ Kg ]",g,state.font12,startx+state.s10,starty);
                MaestroG.superscripter("charge = - 1.60217646 x 10","-19"," [ C ] ",g,state.font12,startx+state.s10,starty+state.s20);
            }
            else if(state.particle == 2){
                g.setColor(Color.magenta.darker());
                MaestroG.superscripter("mass = 1.67262158 x 10","-27"," [ Kg ]",g,state.font12,startx+state.s10,starty);
                MaestroG.superscripter("charge = 1.60217646 x 10","-19"," [ C ] ",g,state.font12,startx+state.s10,starty+state.s20);
            }
            else if(state.particle == 3){
                g.setColor(Color.black);
                MaestroG.superscripter("mass = 1.67492729 x 10","-27"," [ Kg ]",g,state.font12,startx+state.s10,starty);
                MaestroG.superscripter("charge = 0.0",""," [ C ] ",g,state.font12,startx+state.s10,starty+state.s20);
            }
                        
            starty = state.s200+state.s5;
            
	    // Clear this area first
	    g.setColor(bgcolor);
	    g.fillRect(startx+state.s10,starty-state.s15,state.s140,state.s20);
	    g.fillRect(startx+state.s160,starty-state.s15,state.s140,state.s20);
	    
	    g.setColor(Color.red);
            MaestroG.subscripter("E","x"," = "+MaestroA.rounder(state.Ex,4)+" [ V /m ]",g,state.font12,startx+state.s10,starty);
            MaestroG.subscripter("E","y"," = "+MaestroA.rounder(state.Ey,4)+" [ V /m ]",g,state.font12,startx+state.s160,starty);
            
            starty = state.s250;
            
	    // Clear this area first
	    g.setColor(bgcolor);
	    g.fillRect(startx+state.s10,starty-state.s15,state.s140,state.s20);

	    g.setColor(Color.blue);
            MaestroG.subscripter("H","z"," = "+MaestroA.rounder(state.Hz,4)+" [ A /m ]",g,state.font12,startx+state.s10,starty);
                  
            starty = state.s300+state.s20;
            
	    // Clear this area first
	    g.setColor(bgcolor);
	    g.fillRect(startx+state.s145,starty-state.s15,state.s140,state.s20);
	    
            g.setColor(new Color(0,150,0));
            MaestroG.superscripter("\u0394 t = "+MaestroA.rounder(1.0+(double)sliderTime1.getValue()*0.1,2)+ 
                    " x 10","-"+(int)(21.0-(double)sliderTime2.getValue())," [s]",g,state.font13,startx+state.s145,starty);
            
            if(!state.IsOpen){
                g.setColor(Color.blue.darker());
                MaestroG.subscripter("Maximum recommended \u0394t  for closed boundary","","",g,state.font12,state.s20,state.s300+state.s45);
                //MaestroG.subscripter("dt = "+state.dt_suggested,"","",g,state.font12,state.s20,state.s350);
            
                MaestroG.superscripter("\u0394 t = "+state.mantissa+" x 10",""+state.exponent," [s]",g,state.font13,state.s20,state.s300+state.s63);
            } else {
		// clear this part of screen:
		g.setColor(bgcolor);
		g.fillRect(state.s20,state.s300+state.s45-state.s15,state.s280,state.s20);
		g.fillRect(state.s20,state.s300+state.s63-state.s15,state.s160,state.s20);
	    }
    }
        
        //Addition to reduce flicker new routine
	public void update(Graphics g){		// added to avoid clearing
	    paint(g);
	}
        
	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.s40;
            int yplus = state.s25;         // y step down
            
            //g.setColor(Color.gray);
            //MaestroG.subscripterBold("Input","","",g,14,startx+1,startx+6);
            g.setColor(Color.red.darker());
            MaestroG.subscripterBold("Input","","",g,state.font14,startx,startx+5);
            
            g.setColor(Color.blue.darker());
            //MaestroG.subscripter("Initial Position","","",g,state.font12,startx,starty);
            MaestroG.subscripter("x","0"," = "+MaestroA.rounder(state.xpos_0,4)+" [ m ]",g,state.font12,startx,starty);
            MaestroG.subscripter("y","0"," = "+MaestroA.rounder(state.ypos_0,4)+" [ m ]",g,state.font12,startx+state.s150,starty);
            
            starty = state.s85;
            MaestroG.subsup("u","x 0"," = "+MaestroA.rounder(state.vx_0,4)+" x 10","5","m/s",g,state.font12,startx,starty);
            MaestroG.subsup("u","y 0"," = "+MaestroA.rounder(state.vy_0,4)+" x 10","5","m/s",g,state.font12,startx+state.s150,starty);
            
            g.setColor(Color.black);
            g.drawLine(0,state.s120,getSize().width,state.s120);
            
            starty = state.s157;
            
            if(state.particle == 1){
                g.setColor(Color.red.darker());
                MaestroG.subscripter("Electron","","",g,state.font12,startx+state.s10,starty-state.s20);
                MaestroG.superscripter("mass = 9.10938188 x 10","-31"," [ Kg ]",g,state.font12,startx+state.s10,starty);
                MaestroG.superscripter("charge = - 1.60217646 x 10","-19"," [ C ] ",g,state.font12,startx+state.s10,starty+state.s20);
            }
            else if(state.particle == 2){
                g.setColor(Color.magenta.darker());
                MaestroG.superscripter("mass = 1.67262158 x 10","-27"," [ Kg ]",g,state.font12,startx+state.s10,starty);
                MaestroG.superscripter("charge = 1.60217646 x 10","-19"," [ C ] ",g,state.font12,startx+state.s10,starty+state.s20);
            }
            if(state.particle == 3){
                g.setColor(Color.black);
                MaestroG.superscripter("mass = 1.67492729 x 10","-27"," [ Kg ]",g,state.font12,startx+state.s10,starty);
                MaestroG.superscripter("charge = 0.0",""," [ C ] ",g,state.font12,startx+state.s10,starty+state.s20);
            }
            
            g.setColor(Color.black);
            //g.drawLine(0,187,getSize().width,187);
            
            g.setColor(Color.red);
            starty = state.s200+state.s5;
            MaestroG.subscripter("E","x"," = "+MaestroA.rounder(state.Ex,4)+" [ V /m ]",g,state.font12,startx+state.s10,starty);
            MaestroG.subscripter("E","y"," = "+MaestroA.rounder(state.Ey,4)+" [ V /m ]",g,state.font12,startx+state.s160,starty);
            
            g.setColor(Color.blue);
            starty = state.s250;
            MaestroG.subscripter("H","z"," = "+MaestroA.rounder(state.Hz,4)+" [ A /m ]",g,state.font12,startx+state.s10,starty);
            
            g.setColor(Color.black);
            g.drawLine(0,state.s200+state.s85,getSize().width,state.s200+state.s85);
            
            g.setColor(new Color(0,150,0));
            starty = state.s300+state.s5;
            MaestroG.subscripter("Time step","","",g,state.font12,startx+state.s145,starty);
            
            starty = state.s300+state.s20;
            MaestroG.superscripter("\u0394 t = "+MaestroA.rounder(1.0+(double)sliderTime1.getValue()*0.1,2)+ 
                    " x 10","-"+(int)(21.0-(double)sliderTime2.getValue())," [s]",g,state.font13,startx+state.s145,starty);
            
            if(!state.IsOpen){
                g.setColor(Color.blue.darker());
                MaestroG.subscripter("Maximum recommended \u0394t  for closed boundary","","",g,state.font12,state.s20,state.s300+state.s45);
                //MaestroG.subscripter("dt = "+state.dt_suggested,"","",g,state.font12,state.s20,state.s350);
            
                MaestroG.superscripter("\u0394 t = "+state.mantissa+" x 10",""+state.exponent," [s]",g,state.font13,state.s20,state.s300+state.s63);
            }
            int textwidth = state.s80;
            int textheight = state.s20;
            
            starty = 10*yplus+state.s85;
            startx = state.s180;
               //box around update button
            //g.drawRect(startx+1,373,104,textheight+5);
            //------------------------------------------------------------------
            g.setColor(Color.black);
            MaestroG.subscripter("Animation speed","","",g,state.font10,state.s45,state.s400+state.s5);
            
            g.setColor(Color.blue.darker());
            drawArrowNew(state.s20,state.s400,2,g);
            drawArrowNew(state.s27,state.s400,2,g);

            g.setColor(Color.red.darker());
            drawArrowNew(state.s147,state.s400,1,g);
            drawArrowNew(state.s154,state.s400,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 stateChanged(ChangeEvent evt){
	
            if(evt.getSource()==sliderx0){
                state.xpos_0 = MaestroA.rounder(state.Deltax*((double)sliderx0.getValue()-(double)(SCROLLMAX2-1)/2.0),5);
                state.xpos = state.xpos_0;
                state.xpos_old = state.xpos_0;
                state.ypos = state.ypos_0;
                state.ypos_old = state.ypos_0;
                state.ignition();
                repaint();
            }
            if(evt.getSource()==slidery0){
                state.ypos_0 = MaestroA.rounder(state.Deltax*((double)slidery0.getValue()-(double)(SCROLLMAX2-1)/2),5);
                state.ypos = state.ypos_0;
                state.ypos_old = state.ypos_0;
                state.xpos = state.xpos_0;
                state.xpos_old = state.xpos_0;
                state.ignition();
                repaint();
            }
            
            if(evt.getSource()==slidervx0){
                state.vx_0 = MaestroA.rounder(state.Deltavx*((double)slidervx0.getValue()-(double)(SCROLLMAX2-1)/2.0),5);
                state.vx = state.vx_0;
                state.vx_old = state.vx_0;
                state.ignition();
                repaint();
            }
            if(evt.getSource()==slidervy0){
                state.vy_0 = MaestroA.rounder(state.Deltavx*((double)slidervy0.getValue()-(double)(SCROLLMAX2-1)/2),5);
                state.vy = state.vy_0;
                state.vy_old = state.vy_0;
                state.ignition();
                repaint();
            }
            
            if(evt.getSource()==slidermass){
                state.mass = MaestroA.rounder(((double)slidermass.getValue()),2);
                state.ignition();
                repaint();
            }
            if(evt.getSource()==slidercharge){
                state.charge = MaestroA.rounder(((double)slidercharge.getValue()-(double)(21-1)/2),5);
                state.ignition();
                repaint();
            }
            
            if(evt.getSource()==sliderEx){
                state.Ex = 0.01*MaestroA.rounder(((double)sliderEx.getValue()-(double)(SCROLLMAX3-1)/2),5);
                state.ignition();
                repaint();
            }
            if(evt.getSource()==sliderEy){
                state.Ey = 0.01*MaestroA.rounder(((double)sliderEy.getValue()-(double)(SCROLLMAX3-1)/2),5);
                state.ignition();
                repaint();
            }
            if(evt.getSource()==sliderHz){
                state.Hz = 0.01*MaestroA.rounder(((double)sliderHz.getValue()-(double)(SCROLLMAX3-1)/2),5);
                state.ignition();
                repaint();
            }
            
            if(evt.getSource()==sliderTime1){
                state.dt = MaestroA.rounder(1.0+(double)sliderTime1.getValue()*0.1,5)
                           * Math.pow(10.0,-(21.0-(double)sliderTime2.getValue()));
                state.ignition();
                repaint();
            }
            if(evt.getSource()==sliderTime2){
                state.dt = MaestroA.rounder(1.0+(double)sliderTime1.getValue()*0.1,5)
                           * Math.pow(10.0,-(21.0-(double)sliderTime2.getValue()));
                state.ignition();
                repaint();
            }
        }
        
        public void itemStateChanged(ItemEvent evt){
	    ItemSelectable ie = evt.getItemSelectable();
	    /*
            if(evt.getSource() == c1){
		  state.particle = 1;
                  repaint();
            }
                
            else if(evt.getSource() == c2){
		  state.particle = 2;
                  repaint();
            }
                
            else if(evt.getSource() == c3){
		  state.particle = 3;
                  repaint();
            }
            */
            if(evt.getSource() == c_open){
		  state.IsOpen = true;
                  repaint();
            }
            else if(evt.getSource() == c_closed){
		  state.IsOpen = false;
                  repaint();
            }
        }	
    
        	
	
        public double getValue(int i){
		return DValue[i];	
        }

        public synchronized void setValue(double D,int i){
		text1[i].setText(String.valueOf(D));
	}
}

