/**
 * PlaneWave.java 
 *
 */
import java.awt.*;
import java.awt.event.*;
//import java.applet.*;

public class ChargeMotion extends Frame implements ActionListener, MouseListener, Runnable, AdjustmentListener, ItemListener, WindowListener{
    private Instructions instructions;
    //private About about;
    private TitlePanel titlepanel;
    private Timer timer;
    private PlaneWaveCanvas pw_canvas;
    private StartStopClock ssc;
    private PlaneWaveMenuPanel pw_menupanel;
    private FieldOn fieldon;
    private PlaneWave_State state;
    private PlaneWaveOutputPanel outputpanel; 
    private InputPanel inputpanel;
    private ForcePanel fpanel;
    
    Thread tron;
    boolean IsTronRunning;
    int SleepTime;
    
    
    public static void main(String[] args){
        ChargeMotion f = new ChargeMotion();
        int xmove = 17;
        int ymove = 48;
        f.setSize(945+xmove,671+ymove);
        //f.setSize(960,712);
        f.setVisible(true);
        f.setLayout(null);
    }
    
    public ChargeMotion(){
	setLayout(null);
	try{
	    state = new PlaneWave_State();
	    state.ignition();
	    Thread.sleep(100);
	}

        catch(InterruptedException e){}
	int xmove = 12;
        int ymove = 25;
        
	titlepanel = new TitlePanel("Module 5.1","Electron Motion in Static Fields");
        //add(titlepanel);
	titlepanel.setBounds(617+xmove,19+ymove,311,60);
	
        timer = new Timer();
	//add(timer);
	timer.setBounds(22+xmove,54+ymove,288,16);
	
	pw_menupanel = new PlaneWaveMenuPanel();
	//add(pw_menupanel);
	pw_menupanel.setBounds(532+xmove,22+ymove,306,55);
        
        instructions = new Instructions();
        //instructions.setBounds(7,7,920,607);
        instructions.setBounds(11+xmove,21+ymove,599,599);
        //instructions.setVisible(false);

        //about = new About();
        ////add(about);
        //about.setBounds(7,7,920,607);
        //about.setVisible(false);
        
        fieldon = new FieldOn();
	//add(fieldon);
	fieldon.setBounds(649+xmove,89+ymove,185,22);
        
        ssc = new StartStopClock();
	//add(ssc);
	//ssc.setBounds(675,535,196,55);
        ssc.setBounds(675+xmove,605+ymove,196,55);
	ssc.clockcanvas.setStatus(0,state.NPointsT,state.DeltaT);
	
	outputpanel = new PlaneWaveOutputPanel(state);
	//add(outputpanel);
	outputpanel.setBounds(620+xmove,508+ymove,306,161);
	//outputpanel.setVisible(false);
	
            inputpanel = new InputPanel(state);
	    //add(inputpanel);
	    inputpanel.setLocation(620+xmove,87+ymove);
	    inputpanel.setSize(306,411);
	    //inputpanel.titlelabel.setBounds(10,5,200,40);  
	    
            fpanel = new ForcePanel(state);
            fpanel.setBounds(11+xmove,629+ymove,597,40);
            
	    inputpanel.setVisible(true);
	    inputpanel.setValue(1.0E7,0);//frequency
	    inputpanel.setValue(0.001,1); //conductivity
	    inputpanel.setValue(9.0,2); //epsilon_r
	    inputpanel.setValue(1.0,3); //mu_r
	    inputpanel.setValue(10.0,4); //Ex
	    inputpanel.setValue(0.0,5); //Phix
	    inputpanel.setValue(6.0,6); //total_length
	    inputpanel.setValue(1.0,7); //window_area
	    inputpanel.setValue(50,8); //iteration sleep time step
	//}
            
	pw_canvas = new PlaneWaveCanvas(state,inputpanel);
	//add(pw_canvas);
	//pw_canvas.setBounds(10,73,830,500);
        pw_canvas.setBounds(10+xmove,20+ymove,601,601);
	pw_canvas.setDynamics(false);
	
        //behind field0n panel
        Panel pfield = new Panel();
	    pfield.setBackground(Color.black);
	    //add(pfield);
	    pfield.setBounds(648+xmove,88+ymove,187,24);
        
        //behind field panel
	Panel pf1 = new Panel();
	    pf1.setBackground(Color.white);
	    //add(pf1);
	    pf1.setBounds(11+xmove,628+ymove,599,42);
	
	Panel pf0 = new Panel();
	    pf0.setBackground(Color.black);
	    //add(pf0);
	    pf0.setBounds(10+xmove,627+ymove,601,44);    
            
        
	//behind control panel
	Panel p11 = new Panel();
	    p11.setBackground(Color.white);
	    //add(p11);
	    p11.setBounds(531+xmove,21+ymove,308,57);
	
	Panel p10 = new Panel();
	    p10.setBackground(Color.black);
	    //add(p10);
	    p10.setBounds(530+xmove,20+ymove,310,59);
	//---------------------------------------------------
        // behind outputpanel 
	Panel p21 = new Panel();
            //add(p21);
	    p21.setBackground(Color.white);
	    p21.setBounds(619+xmove,507+ymove,308,163);
	
	Panel p20 = new Panel();
	    p20.setBackground(Color.black);
	    //add(p20);
	    p20.setBounds(618+xmove,506+ymove,310,165);
	//----------------------------------------------------
        // behind inputpanel and section A
	Panel p31 = new Panel();
	    p31.setBackground(Color.white);
	    //add(p31);
	    p31.setBounds(619+xmove,86+ymove,308,413);
	
	Panel p30 = new Panel();
	    p30.setBackground(Color.black);
	    //add(p30);
	    p30.setBounds(618+xmove,85+ymove,310,415);
	//----------------------------------------------------
        //     backing for slide panel 
	Panel p41 = new Panel();
	    p41.setBackground(Color.white);
	    //add(p41);
	    p41.setBounds(11+xmove,437+ymove,828,39);
	
	Panel p40 = new Panel();
	    p40.setBackground(Color.black);
	    //add(p40);
	    p40.setBounds(10+xmove,436+ymove,830,41);
	//----------------------------------------------------
            
        // backing for clock panel    
	Panel p51 = new Panel();
	    p51.setBackground(Color.white);
	    //add(p51);
	    p51.setBounds(674+xmove,604+ymove,198,57);
	
	Panel p50 = new Panel();
	    p50.setBackground(Color.black);
	    //add(p50);
	    p50.setBounds(673+xmove,603+ymove,200,59);
	//--------------------------------------------------------------
        // backing for complete applet    
	Panel p0 = new Panel();
	    p0.setBackground(Color.cyan);
	    //add(p0);
	    p0.setBounds(3+xmove,13+ymove,932,665);
	    
	Panel p00 = new Panel();
	    p00.setBackground(Color.black);
	    //add(p00);
	    p00.setBounds(xmove,10+ymove,938,671);
	    
            //if(state.this_year > line.magicyear || (state.this_year == line.magicyear && state.this_month > line.magicmonth)){
	    //state.LicenseExpired = true;
            //}
            //else
            //{   
            //add(about);
            add(instructions);
            add(titlepanel);
	    add(ssc);
	    add(inputpanel);
            add(pw_canvas);
            add(fpanel);
            add(p51);    // BEHIND START PANEL
	    add(p50);    // BEHIND START PANEL
	    add(outputpanel);
            
            add(p21);    // BEHIND OUTPUT PANEL
            add(p20);    // BEHIND OUTPUT PANEL
            
	    add(p31);
	    add(p30);
	    add(pf1);
            add(pf0);
            add(p0);
	    add(p00);
	    state.LicenseExpired = false; 	
            //}

	pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
        
        pw_canvas.ignition();
        pw_canvas.cleanUp();
	pw_canvas.repaint();
	outputpanel.pwocA.repaint();
        
        //Thread control
	IsTronRunning = false;
	SleepTime = 50;
        
	//Listeners
	//about.bupdate.addActionListener(this);
        this.addWindowListener(this);
	instructions.closeIt.addActionListener(this);
        
        fpanel.c1.addItemListener(this);
        fpanel.c2.addItemListener(this);
        fpanel.c3.addItemListener(this);
        fpanel.c4.addItemListener(this);
        fpanel.c5.addItemListener(this);
        
        inputpanel.about.addActionListener(this);
        inputpanel.c1.addItemListener(this);
        inputpanel.c2.addItemListener(this);
        inputpanel.c3.addItemListener(this);
        inputpanel.c_open.addItemListener(this);
        inputpanel.c_closed.addItemListener(this);
        inputpanel.c_trace.addItemListener(this);
        inputpanel.c_field.addItemListener(this);
        
        inputpanel.slider1.addAdjustmentListener(this);
        inputpanel.sliderx0.addAdjustmentListener(this);
        inputpanel.slidery0.addAdjustmentListener(this);
        inputpanel.slidervx0.addAdjustmentListener(this);
        inputpanel.sliderEx.addAdjustmentListener(this);
        inputpanel.sliderEy.addAdjustmentListener(this);
        inputpanel.sliderHz.addAdjustmentListener(this);
        inputpanel.sliderTime1.addAdjustmentListener(this);
        inputpanel.sliderTime2.addAdjustmentListener(this);
        
        // CHANGE ======================================================
                inputpanel.b2a.addActionListener(this);
                inputpanel.b2b.addActionListener(this);
                inputpanel.b3a.addActionListener(this);
                inputpanel.b3b.addActionListener(this);
                inputpanel.b4a.addActionListener(this);
                inputpanel.b4b.addActionListener(this);        
        //==============================================================
        
        
        inputpanel.slidervy0.addAdjustmentListener(this);
        ssc.button1.addMouseListener(this);
	ssc.button2.addMouseListener(this);
        ssc.button3.addMouseListener(this);
        
	pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
        pw_canvas.ignition();
        pw_canvas.addMouseListener(this);
	    
       StartUp(); // to set up properly at startup
    }
    
    /*
    public void paint(Graphics g){
    
	Font licensefont = new Font("SanSerif",Font.BOLD,18); 	
	g.setColor(Color.white);	 	
	g.fillRect(0,0,945,765); 	
	
	if(state.LicenseExpired){
	g.setFont(licensefont);
	
	g.setColor(Color.red);
	g.drawString("The software license has expired.", 50, 200);
	g.setColor(Color.black);
	g.drawString("Obtain updated files from www.amanogawa.com", 50, 230);
	} 
    }
    */
    
    public void paint(Graphics g){
    
    Font licensefont = new Font("SanSerif",Font.BOLD,18); 
    Font fonto = new Font("SanSerif",Font.PLAIN,12);
    Font fontc = new Font("SanSerif",Font.BOLD,16);
    
	Graphics2D g2d = (Graphics2D)g;
        //g.setColor(Color.white);	
	//g.fillRect(0,0,1160,620);  	
	/*	
	if(state.LicenseExpired){
	    g.setFont(licensefont);
	
	    g.setColor(Color.red);
	    g.drawString("The software license has expired.", 50, 200);
	    g.setColor(Color.black);
	    g.drawString("Obtain updated files from www.amanogawa.com", 50, 230);
	} 
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        int annonuovo = 2006;
        int anno = 2006;
        
        if(anno > state.this_year){
            annonuovo = anno;
        }
        else{
            annonuovo = state.this_year;
        }
        //g.setColor(Color.red.darker());
        //g.setFont(fontc);
        //g.drawString("\u00a9",10,602);
        //g.setFont(fonto);
        //g.drawString("Amanogawa, "+annonuovo+" - All Rights Reserved",30,600);
        */
    }
   
    public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
    }
    
    public void windowOpened(WindowEvent evt){}
    
    public void windowIconified(WindowEvent evt){}
    
    public void windowClosed(WindowEvent evt){}
    
    public void windowDeiconified(WindowEvent evt){}
    
    public void windowActivated(WindowEvent evt){}
    
    public void windowDeactivated(WindowEvent evt){}
    
    public void actionPerformed(ActionEvent evt){
        if(evt.getSource()==inputpanel.b2a || evt.getSource()==inputpanel.b2b ||
           evt.getSource()==inputpanel.b3a || evt.getSource()==inputpanel.b3b ||
           evt.getSource()==inputpanel.b4a || evt.getSource()==inputpanel.b4b
                ){
            state.ignition();
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
            inputpanel.repaint();
            //if(state.dt > state.dt_suggested){
                state.dt = Math.pow(10.0, (double)state.exponent);
                        if(state.mantissa > 5.0){
                            inputpanel.sliderTime1.setValue(40);
                        }
                        else{
                            inputpanel.sliderTime1.setValue(0);
                        }
                inputpanel.sliderTime2.setValue(20-Math.abs(state.exponent)+1);   
            //}
        }
        
        //if(evt.getSource()==about.bupdate){
        //    about.setVisible(false);
        //}
        if (evt.getSource()==instructions.closeIt) {
            instructions.setVisible(false);
        }
        else if(evt.getSource() == inputpanel.about){
            //about.setVisible(true);
            instructions.setVisible(true);
        }
        
	else if(evt.getSource() == inputpanel.b1){// || evt.getSource() == fieldon.b1){
	    double temp, temp2;
            state.CurrentTime = 0;
            state.TotalTime = 0;
            timer.repaint();
            outputpanel.pwocA.repaint();
            SleepTime = state.SleepTime;
	    ssc.reset();
            
            // update permittivity
            try{if(inputpanel.getValue(2)<1.0){
		    state.epsilon_r=1.0;
                    inputpanel.setValue(1.0,2);
		}
		else{
		    state.epsilon_r = inputpanel.getValue(2);
		}
	    }
	    catch(NumberFormatException e){
		  state.epsilon_r = 1.0;
	    } 
            
            // update permeability
            try{if(inputpanel.getValue(3)<1.0){
		    state.mu_r=1.0;
                    inputpanel.setValue(1.0,3);
		}
		else{
		    state.mu_r = inputpanel.getValue(3);
		}
	    }
	    catch(NumberFormatException e){
		  state.mu_r = 1.0;
	    }

            // update electric field magnitude
            try{if(inputpanel.getValue(4)<0.0){
		    state.Ex = 0.0;
                    inputpanel.setValue(0.0,4);
		}
		else{
		    state.Ex = inputpanel.getValue(4);
		}
	    }
	    catch(NumberFormatException e){
		  state.Ex = 1.0;
	    }
            
            // update electric field reference phase
            try{if(inputpanel.getValue(5)<-Math.PI){
		    state.Phix = -Math.PI;
                    inputpanel.setValue(-Math.PI,5);
		}
                else if(inputpanel.getValue(5)>Math.PI){
                    state.Phix = Math.PI;
                    inputpanel.setValue(Math.PI,5);
                }
		else{
		    state.Phix = inputpanel.getValue(5);
		}
	    }
	    catch(NumberFormatException e){
		  state.Phix = 0.0;
	    }

            timer.repaint();
            state.ignition();
	    
            ssc.clockcanvas.setStatus(0,state.NPointsT,state.DeltaT);
            ssc.reset();
            ssc.repaint();
            
	    //outputpanel.pwocA.repaint();
            
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
	}
        
        else if(evt.getSource() == ssc.button3){
	    double temp, temp2;
	    
            state.tempo = 0.0;
            state.jj = 0;        
            
            ssc.clockcanvas.setStatus(0,state.NPointsT,state.DeltaT);
            ssc.reset();
            ssc.repaint();
            
            timer.repaint();
            
            timer.TotalTime = state.TotalTime;
            timer.CurrentTime = state.CurrentTime;
            state.vx = state.vx_0;
            state.vy = state.vy_0;
            state.ignition();
	                
            pw_canvas.ignition();
            
	    pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
	}
    }
    
     public void start(){
	if(tron == null){
	    tron = new Thread(this);
	    tron.start();
	}
    }
    
    //public void stop(){
	//if(tron != null) {
	//    tron.stop();
	//    tron = null;
	//}
    //}
    
    public void run(){
	while(true){
	    if(IsTronRunning){
	    	if(!state.IsOpen){
                    ssc.clockcanvas.increment();
                    state.increment();
                    pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
                    pw_canvas.ignition();
                    pw_canvas.repaint();

                    outputpanel.pwocA.repaint();

                    timer.TotalTime = state.TotalTime;
                    timer.CurrentTime = state.CurrentTime;            
                    timer.repaint();
                }
                else{
                    if(Math.abs(state.xpos) >(2.0*state.xmax) ||
                       Math.abs(state.ypos) > (2.0*state.ymax) ){}
                    else{
                        ssc.clockcanvas.increment();
                        state.increment();
                        pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
                        pw_canvas.ignition();
                        pw_canvas.repaint();

                        outputpanel.pwocA.repaint();

                        timer.TotalTime = state.TotalTime;
                        timer.CurrentTime = state.CurrentTime;            
                        timer.repaint();
                    }
                }
	    }
	    try{
		Thread.sleep(SleepTime+1);
	    }
	    catch(InterruptedException e){e.printStackTrace();}
	}
    }
    
    public void mouseClicked(MouseEvent evt){mouseHandler(evt); mouseHandler2(evt);}
    public void mouseDragged(MouseEvent evt){mouseHandler2(evt);}
    public void mouseMoved(MouseEvent evt){;}
    public void mouseEntered(MouseEvent evt){;}
    public void mouseExited(MouseEvent evt){;}
    public void mousePressed(MouseEvent evt){mouseHandler2(evt);}
    public void mouseReleased(MouseEvent evt){mouseHandler2(evt);}
    
    private synchronized void mouseHandler2(MouseEvent evt){
        
        if(evt.getSource() == pw_canvas){
            inputpanel.repaint();
            outputpanel.pwocA.repaint();
        }
    }
  
    private synchronized void mouseHandler(MouseEvent evt){
        
	if(evt.getSource() == ssc.button1){
	    IsTronRunning = true;
	    if(tron == null){
		start();
	    }
	    else {
		//tron.resume();
	    }
            ssc.IsStop = false;
            ssc.repaint();
            pw_canvas.setStop(false);
            
            inputpanel.sliderx0.setEnabled(false);
            inputpanel.slidery0.setEnabled(false);
            inputpanel.slidervx0.setEnabled(false);
            inputpanel.slidervy0.setEnabled(false);
            
            inputpanel.sliderEx.setEnabled(false);
            inputpanel.sliderEy.setEnabled(false);
            inputpanel.sliderHz.setEnabled(false);
            
            inputpanel.sliderTime1.setEnabled(false);
            inputpanel.sliderTime2.setEnabled(false);
            inputpanel.c1.setEnabled(false);
            inputpanel.c2.setEnabled(false);
            inputpanel.c3.setEnabled(false);
            inputpanel.c_trace.setEnabled(false);
            inputpanel.c_field.setEnabled(false);
            
            pw_canvas.setStop(false);
            pw_canvas.setReset(false);
            pw_canvas.setStart(true);
            
            fpanel.c1.setEnabled(false);
            fpanel.c2.setEnabled(false);
            fpanel.c3.setEnabled(false);
            fpanel.c4.setEnabled(false);
            fpanel.c5.setEnabled(false);
	}
	if(evt.getSource() == ssc.button2){
	    IsTronRunning = false;
	    //tron.suspend();
            ssc.IsStop = true;
            ssc.repaint();
            pw_canvas.setStop(true);
            pw_canvas.setReset(false);
            pw_canvas.setStart(false);
            
            fpanel.c1.setEnabled(false);
            fpanel.c2.setEnabled(false);
            fpanel.c3.setEnabled(false);
            fpanel.c4.setEnabled(false);
            fpanel.c5.setEnabled(false);
        }
        if(evt.getSource() == ssc.button3){
            fpanel.c1.setEnabled(true);
            fpanel.c2.setEnabled(true);
            fpanel.c3.setEnabled(true);
            fpanel.c4.setEnabled(true);
            fpanel.c5.setEnabled(true);
            
            inputpanel.sliderx0.setEnabled(true);
            inputpanel.slidery0.setEnabled(true);
            inputpanel.slidervx0.setEnabled(true);
            inputpanel.slidervy0.setEnabled(true);
            
            inputpanel.sliderEx.setEnabled(true);
            inputpanel.sliderEy.setEnabled(true);
            inputpanel.sliderHz.setEnabled(true);
            
            inputpanel.sliderTime1.setEnabled(true);
            inputpanel.sliderTime2.setEnabled(true);
            inputpanel.c1.setEnabled(true);
            inputpanel.c2.setEnabled(true);
            inputpanel.c3.setEnabled(true);
            inputpanel.c_trace.setEnabled(true);
            inputpanel.c_field.setEnabled(true);
            
	    state.tempo = 0.0;
            state.jj = 0;        
            
            IsTronRunning = false;
	    ssc.IsStop = true;
            ssc.reset();
            ssc.repaint();
            state.xpos = state.xpos_0;
            state.ypos = state.ypos_0;
            state.xpos_old = state.xpos_0;
            state.ypos_old = state.ypos_0;
            state.vx = state.vx_0;
            state.vy = state.vy_0;
            state.vx_old = state.vx_0;
            state.vy_old = state.vy_0;
            state.ax_old = state.charge_value/state.mass_value*(state.Ex + state.vy_old * state.Bz);
            state.ay_old = state.charge_value/state.mass_value*(state.Ey - state.vx_old * state.Bz);
            state.ay = state.ay_old;
            state.ax = state.ax_old;
            
            state.ignition();
	    
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.cleanUp();
	    pw_canvas.ignition();
            pw_canvas.repaint();
            pw_canvas.setStop(true);
            pw_canvas.setReset(true);
            pw_canvas.setStart(false);
            outputpanel.pwocA.repaint();
	}
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	
        if(evt.getSource()==inputpanel.slider1){
            state.SleepTime = (inputpanel.SCROLLMAX-1) - inputpanel.slider1.getValue();
            SleepTime = state.SleepTime;
        }
        
        else if(evt.getSource()==inputpanel.sliderEy){
            state.ignition();
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
            inputpanel.repaint();
            //if(state.dt > state.dt_suggested){
                state.dt = Math.pow(10.0, (double)state.exponent);
                        if(state.mantissa > 5.0){
                            inputpanel.sliderTime1.setValue(40);
                        }
                        else{
                            inputpanel.sliderTime1.setValue(0);
                        }
                inputpanel.sliderTime2.setValue(20-Math.abs(state.exponent)+1);   
            //}
        }
        
        else if(evt.getSource()==inputpanel.sliderHz){
            state.ignition();
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
            inputpanel.repaint();
            //if(state.dt > state.dt_suggested){
                state.dt = Math.pow(10.0, (double)state.exponent);
                        if(state.mantissa > 5.0){
                            inputpanel.sliderTime1.setValue(40);
                        }
                        else{
                            inputpanel.sliderTime1.setValue(0);
                        }
                inputpanel.sliderTime2.setValue(20-Math.abs(state.exponent)+1);
            //}
        }
        else if(evt.getSource()==inputpanel.sliderEx){
            state.ignition();
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
            inputpanel.repaint();
            //if(state.dt > state.dt_suggested){
                state.dt = Math.pow(10.0, (double)state.exponent);
                        if(state.mantissa > 5.0){
                            inputpanel.sliderTime1.setValue(40);
                        }
                        else{
                            inputpanel.sliderTime1.setValue(0);
                        }
                inputpanel.sliderTime2.setValue(20-Math.abs(state.exponent)+1);   
            //}
        }
        else if(evt.getSource()==inputpanel.sliderx0){
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
        }
        
        else if(evt.getSource()==inputpanel.slidery0){
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
        }
        
        else if(evt.getSource()==inputpanel.slidervx0){
            state.ignition();
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
            inputpanel.repaint();
            //if(state.dt > state.dt_suggested){
                state.dt = Math.pow(10.0, (double)state.exponent);
                if(state.mantissa > 5.0){
                    inputpanel.sliderTime1.setValue(40);
                }
                else{
                    inputpanel.sliderTime1.setValue(0);
                }
                inputpanel.sliderTime2.setValue(20-Math.abs(state.exponent)+1);
                
            //}
        }
        
        else if(evt.getSource()==inputpanel.slidervy0){
            state.ignition();
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
            
            inputpanel.repaint();
            //if(state.dt > state.dt_suggested){
                state.dt = Math.pow(10.0, (double)state.exponent);
                if(state.mantissa > 5.0){
                    inputpanel.sliderTime1.setValue(40);
                }
                else{
                    inputpanel.sliderTime1.setValue(0);
                }
                inputpanel.sliderTime2.setValue(20-Math.abs(state.exponent)+1);
                
            //}
        }
    }
    
    public void itemStateChanged(ItemEvent evt){
	    ItemSelectable ie = evt.getItemSelectable();
        
        if(evt.getSource() == fpanel.c1){
		  state.xmax=0.1; //meters
                  state.xmin=-0.1; //meters
                  state.ymax=0.1; //meters
                  state.ymin=-0.1; //meters
                  
                  state.xpos = 0.0;
                  state.ypos = 0.0;
                  state.xpos_0 = 0.0;
                  state.ypos_0 = 0.0;
                  state.xpos_old = 0.0;
                  state.ypos_old = 0.0;
                  
                  pw_canvas.xnewmax=0.1; //meters
                  pw_canvas.xnewmin=-0.1; //meters
                  pw_canvas.ynewmax=0.1; //meters
                  pw_canvas.ynewmin=-0.1; //meters
                  pw_canvas.xpos = 0.0;
                  pw_canvas.ypos = 0.0;
                  
                  state.ignition();
                  inputpanel.repaint();
                  pw_canvas.cleanUp();
                  pw_canvas.ignition();
                  pw_canvas.repaint();
        }
            
        else if(evt.getSource() == fpanel.c2){
                  state.xmax=1.0; //meters
                  state.xmin=-1.0; //meters
                  state.ymax=1.0; //meters
                  state.ymin=-1.0; //meters
                  
                  state.xpos = 0.0;
                  state.ypos = 0.0;
                  state.xpos_0 = 0.0;
                  state.ypos_0 = 0.0;
                  state.xpos_old = 0.0;
                  state.ypos_old = 0.0;
                  
                  pw_canvas.xnewmax=1.0; //meters
                  pw_canvas.xnewmin=-1.0; //meters
                  pw_canvas.ynewmax=1.0; //meters
                  pw_canvas.ynewmin=-1.0; //meters
                  pw_canvas.xpos = 0.0;
                  pw_canvas.ypos = 0.0;
                  
                  state.ignition();
                  inputpanel.repaint();
                  pw_canvas.cleanUp();
                  pw_canvas.ignition();
                  pw_canvas.repaint();
        }
         
        else if(evt.getSource() == fpanel.c3){
		  state.ignition();
                  state.xmax=10.0; //meters
                  state.xmin=-10.0; //meters
                  state.ymax=10.0; //meters
                  state.ymin=-10.0; //meters
                  
                  state.xpos = 0.0;
                  state.ypos = 0.0;
                  state.xpos_0 = 0.0;
                  state.ypos_0 = 0.0;
                  state.xpos_old = 0.0;
                  state.ypos_old = 0.0;
                  
                  pw_canvas.xnewmax=10.0; //meters
                  pw_canvas.xnewmin=-10.0; //meters
                  pw_canvas.ynewmax=10.0; //meters
                  pw_canvas.ynewmin=-10.0; //meters
                  pw_canvas.xpos = 0.0;
                  pw_canvas.ypos = 0.0;
                  
                  state.ignition();
                  inputpanel.repaint();
                  pw_canvas.cleanUp();
                  pw_canvas.ignition();
                  pw_canvas.repaint();
        }
        else if(evt.getSource() == fpanel.c4){
		  state.ignition();
                  state.xmax=100.0; //meters
                  state.xmin=-100.0; //meters
                  state.ymax=100.0; //meters
                  state.ymin=-100.0; //meters
                  
                  state.xpos = 0.0;
                  state.ypos = 0.0;
                  state.xpos_0 = 0.0;
                  state.ypos_0 = 0.0;
                  state.xpos_old = 0.0;
                  state.ypos_old = 0.0;
                  
                  pw_canvas.xnewmax=100.0; //meters
                  pw_canvas.xnewmin=-100.0; //meters
                  pw_canvas.ynewmax=100.0; //meters
                  pw_canvas.ynewmin=-100.0; //meters
                  
                  pw_canvas.repaint();
        }
        else if(evt.getSource() == fpanel.c5){
		  state.ignition();
                  state.xmax=1000.0; //meters
                  state.xmin=-1000.0; //meters
                  state.ymax=1000.0; //meters
                  state.ymin=-1000.0; //meters
                  
                  state.xpos = 0.0;
                  state.ypos = 0.0;
                  state.xpos_0 = 0.0;
                  state.ypos_0 = 0.0;
                  state.xpos_old = 0.0;
                  state.ypos_old = 0.0;
                  
                  pw_canvas.xnewmax=1000.0; //meters
                  pw_canvas.xnewmin=-1000.0; //meters
                  pw_canvas.ynewmax=1000.0; //meters
                  pw_canvas.ynewmin=-1000.0; //meters
                  
                  pw_canvas.repaint();
        } 
        
            else if(evt.getSource() == inputpanel.c1){
		  state.ignition();
                  pw_canvas.repaint();
                    inputpanel.repaint();
                    if(state.dt > state.dt_suggested){
                        state.dt = Math.pow(10.0, (double)state.exponent);
                        if(state.mantissa > 5.0){
                            inputpanel.sliderTime1.setValue(40);
                        }
                        else{
                            inputpanel.sliderTime1.setValue(0);
                        }
                        inputpanel.sliderTime2.setValue(20-Math.abs(state.exponent)+1);
                
                    }
                    state.ignition();
                    outputpanel.pwocA.repaint();
        }       
        else if(evt.getSource() == inputpanel.c2){
		  state.ignition();
                  pw_canvas.repaint();
                  inputpanel.repaint();
                    if(state.dt > state.dt_suggested){
                        state.dt = Math.pow(10.0, (double)state.exponent);
                        if(state.mantissa > 5.0){
                            inputpanel.sliderTime1.setValue(40);
                        }
                        else{
                            inputpanel.sliderTime1.setValue(0);
                        }
                        inputpanel.sliderTime2.setValue(20-Math.abs(state.exponent)+1);
                
                    }
                  state.ignition();
                  outputpanel.pwocA.repaint();
	}       
        else if(evt.getSource() == inputpanel.c3){
		  state.ignition();
                  pw_canvas.repaint();
                  inputpanel.repaint();
                    if(state.dt > state.dt_suggested){
                        state.dt = Math.pow(10.0, (double)state.exponent);
                        if(state.mantissa > 5.0){
                            inputpanel.sliderTime1.setValue(40);
                        }
                        else{
                            inputpanel.sliderTime1.setValue(0);
                        }
                        inputpanel.sliderTime2.setValue(20-Math.abs(state.exponent)+1);
                
                    }
                  state.ignition();
                  outputpanel.pwocA.repaint();
	}
            
        else if(evt.getSource() == inputpanel.c_trace){
            if(inputpanel.c_trace.getState()){
                pw_canvas.setTrace(true);
                pw_canvas.repaint();
                outputpanel.pwocA.repaint();
                //inputpanel.c_field.setVisible(false);
                inputpanel.c_field.setEnabled(false);
            }
            else{
                pw_canvas.setTrace(false);
                pw_canvas.repaint();
                outputpanel.pwocA.repaint();
                //inputpanel.c_field.setVisible(true);
                inputpanel.c_field.setEnabled(true);
            }
	}
            
        else if(evt.getSource() == inputpanel.c_field){
            if(inputpanel.c_field.getState()){
                pw_canvas.setField(true);
                pw_canvas.repaint();
                outputpanel.pwocA.repaint();
            }
            else{
                pw_canvas.setField(false);
                pw_canvas.repaint();
                outputpanel.pwocA.repaint();
            }
	}
    }	
    
    public void StartUp(){
        double temp, temp2;
	    
            state.CurrentTime = 0;
            state.TotalTime = 0;
            timer.repaint();
            
            SleepTime = state.SleepTime;
	    ssc.reset();
            state.CurrentTime = 0;
            timer.TotalTime = state.TotalTime;
            timer.CurrentTime = state.CurrentTime;
                        
            state.ignition();
	    
            ssc.clockcanvas.setStatus(0,state.NPointsT,state.DeltaT);
            ssc.reset();
            ssc.repaint();
            //outputpanel.pwocA.repaint();
	    
            pw_canvas.setPositions(state.xpos, state.ypos, state.xpos_old, state.ypos_old);
            pw_canvas.ignition();
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel.pwocA.repaint();
    }
}
