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

public class Attenuation extends Frame implements ActionListener, MouseListener, Runnable, AdjustmentListener, ItemListener, WindowListener{
    //About about;
    Instructions instructions;
    private TitlePanel titlepanel;
    private Timer timer;
    private Reset reset;
    private PlaneWaveCanvas pw_canvas;
    private PlaneWaveSlidePanel pw_slidepanel;
    private StartStopClock ssc;
    private PlaneWaveMenuPanel pw_menupanel;
    private FieldOn fieldon;
    private PlaneWavePolarCanvas pwpc1, pwpc2;
    private PlaneWave_State state;
    private PlaneWaveOutputPanel outputpanel; 
    private PlaneWaveOutputPanel2 outputpanel2; 
    private PR  inputpanel;
    private PR2  examples;
    private int SCROLLMIN=0, SCROLLMAX=1001;
    Thread tron;
    boolean IsTronRunning;
    int SleepTime;
    
    public static void main(String[] args){
        Attenuation f = new Attenuation();
        int xmove = 20;
        int ymove = 41;
        f.setSize(1145+xmove,588+ymove);
        f.setVisible(true);
        f.setLayout(null);
    }
    
    public Attenuation(){
	setLayout(null);
	try{
	    state = new PlaneWave_State();
	    state.ignition();
	    //state.scan_wave();
	    Thread.sleep(100);
	}

        catch(InterruptedException e){}
	
        int xmove = 10;
        int ymove = 34;
	titlepanel = new TitlePanel("Module 7.5","Wave Attenuation");
        //add(titlepanel);
	titlepanel.setBounds(10+xmove,9+ymove,310,60);
	
        timer = new Timer();
	//add(timer);
	timer.setBounds(22+xmove,44+ymove,288,16);
	
	pw_menupanel = new PlaneWaveMenuPanel();
	//add(pw_menupanel);
	pw_menupanel.setBounds(532+xmove,12+ymove,306,55);
        
        instructions = new Instructions();
        instructions.setBounds(11+xmove,75+ymove,828,498);
        //instructions.setVisible(false);
        
        fieldon = new FieldOn();
	//add(fieldon);
	fieldon.setBounds(649+xmove,79+ymove,185,22);
        
        reset = new Reset();
	//add(reset);
	reset.setBounds(13+xmove,77+ymove,110,45);
	
	pw_slidepanel = new PlaneWaveSlidePanel(state);
	//add(pw_slidepanel);
	pw_slidepanel.setBounds(12+xmove,553+ymove,826,17);
            
	state.scan_wave();
	
	ssc = new StartStopClock();
	//add(ssc);
	ssc.setBounds(327+xmove,12+ymove,196,55);
	ssc.clockcanvas.setStatus(0,state.NPointsT,state.DeltaT);
	
	//Second argument says which plane to work on.
	pwpc1 = new PlaneWavePolarCanvas(state,0);
	//add(pwpc1);
	pwpc1.setBounds(847+xmove,12+ymove,286,276);
	pwpc1.setVisible(false);
	
	pwpc2 = new PlaneWavePolarCanvas(state,1);
	//add(pwpc2);
	pwpc2.setBounds(847+xmove,296+ymove,286,276);
	pwpc2.setVisible(false);
	
	outputpanel = new PlaneWaveOutputPanel(state);
	//add(outputpanel);
	outputpanel.setBounds(847+xmove,296+ymove,286,276);
	//outputpanel.setVisible(false);
	
        outputpanel2 = new PlaneWaveOutputPanel2(state);
	//add(outputpanel2);
	outputpanel2.setBounds(12+xmove,472+ymove,826,100);
	//outputpanel2.setVisible(false);
	
	//{
	    //remnants from old set up - just don't touch - new labels set inside PR
            String str1[] = {"Frequency   f  =  ","Conductivity   \u03c3  =  ","Relative Permittivity  \u03b5  =  ",
		"Relative Permeability  \u03bc  =  ","Amplitude E-field  Eo  =  ","Phase E-field  \u03c6  =  ", "Total Length Displayed  L  =  ",
		"[A] & [B] Window Area  =  ","Sleep time step  =  "};
	    String str2[] = {"    [ Hz ]","    [ S/m ]"," "," ", "    [ V/m ]","    [ rad ]","       \u03bb","    [ m\u00b2 ]","    [ ms ]"};
	    inputpanel = new PR("Input",str1,str2);
	    //add(inputpanel);
	    inputpanel.setLocation(847+xmove,12+ymove);
	    inputpanel.setSize(286,276);
	    //inputpanel.titlelabel.setBounds(10,5,200,40);  
	    
	    inputpanel.setVisible(false);
	    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
	//}
            
            examples = new PR2();
	    //add(examples);
	    examples.setLocation(847+xmove,12+ymove);
	    examples.setSize(286,276);
	    examples.setVisible(true);
            
	pw_canvas = new PlaneWaveCanvas();
	//add(pw_canvas);
	pw_canvas.setBounds(10+xmove,74+ymove,830,500);
	pw_canvas.setDynamics(false);
	pw_canvas.setTrace(false);
	
        //behind field0n panel
        Panel pfield = new Panel();
	    pfield.setBackground(Color.black);
	    //add(pfield);
	    pfield.setBounds(648+xmove,78+ymove,187,24);
        
	//behind control panel
	Panel p11 = new Panel();
	    p11.setBackground(Color.white);
	    //add(p11);
	    p11.setBounds(531+xmove,11+ymove,308,57);
	
	Panel p10 = new Panel();
	    p10.setBackground(Color.black);
	    //add(p10);
	    p10.setBounds(530+xmove,10+ymove,310,59);
	//---------------------------------------------------
        // behind outputpanel and section B
	Panel p21 = new Panel();
	    p21.setBackground(Color.white);
	    p21.setBounds(846+xmove,295+ymove,288,278);
	
	Panel p20 = new Panel();
	    p20.setBackground(Color.black);
	    //add(p20);
	    p20.setBounds(845+xmove,294+ymove,290,280);
	 
        // behind outputpanel 2    
        Panel p61 = new Panel();
	    p61.setBackground(Color.white);
	    //add(p61);
	    p61.setBounds(11+xmove,471+ymove,828,102);
	
	Panel p60 = new Panel();
	    p60.setBackground(Color.black);
	    //add(p60);
	    p60.setBounds(10+xmove,470+ymove,830,104);
	
        // behind inputpanel and section A
	Panel p31 = new Panel();
	    p31.setBackground(Color.white);
	    //add(p31);
	    p31.setBounds(846+xmove,11+ymove,288,278);
	
	Panel p30 = new Panel();
	    p30.setBackground(Color.black);
	    //add(p30);
	    p30.setBounds(845+xmove,10+ymove,290,280);
	
        //     backing for slide panel ----------------------
	Panel p41 = new Panel();
	    p41.setBackground(Color.white);
	    //add(p41);
	    p41.setBounds(11+xmove,427+ymove,828,39);
	
	Panel p40 = new Panel();
	    p40.setBackground(Color.black);
	    //add(p40);
	    p40.setBounds(10+xmove,426+ymove,830,41);
	//----------------------------------------------------
            
        // backing for clock panel    
	Panel p51 = new Panel();
	    p51.setBackground(Color.white);
	    //add(p51);
	    p51.setBounds(326+xmove,11+ymove,198,57);
	
	Panel p50 = new Panel();
	    p50.setBackground(Color.black);
	    //add(p50);
	    p50.setBounds(325+xmove,10+ymove,200,59);
	//--------------------------------------------------------------
        // backing for complete applet    
	Panel p0 = new Panel();
	    p0.setBackground(Color.cyan);
	    //add(p0);
	    p0.setBounds(3+xmove,3+ymove,1139,579);
	    
	Panel p00 = new Panel();
	    p00.setBackground(Color.black);
	    //add(p00);
	    p00.setBounds(xmove,ymove,1145,585);
	    
            //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(timer);
            add(titlepanel);
	    add(pw_menupanel);
            add(fieldon);
            add(pfield);
	    
            add(reset);
	    add(pw_slidepanel);
	    add(ssc);
	    //add(pwpc1);
	    //add(pwpc2);
	    add(outputpanel);
            
            //add(outputpanel2);
	    add(inputpanel);
            add(examples);
	    add(pw_canvas);
            
            add(p11);
	    add(p10);
	    add(p21);
	    add(p20);
	    add(p31);
	    add(p30);
	    //add(p41);
	    //add(p40);
	    add(p51);
	    add(p50);
            //add(p61);
	    //add(p60);
	    add(p0);
	    add(p00);
	    state.LicenseExpired = false; 	
            //}

	
	pw_canvas.setPlanes(state.zpos[0]*state.wavelength,state.zpos[1]*state.wavelength);
	pw_canvas.setXRangeMax(state.Ex);
	pw_canvas.setXRangeMin(-state.Ex);
	pw_canvas.setYRangeMax(state.Ex/state.wave_impedance_mag);
	pw_canvas.setYRangeMin(-state.Ex/state.wave_impedance_mag);
    
	pw_canvas.ignition();
	//pw_canvas.plot(state.x,state.efield,state.hfield);
	pw_canvas.cleanUp();
	pw_canvas.repaint();
	pw_canvas.plot(state.x,state.efield,state.hfield,state.pfield,state.wave_impedance_phase);  
	//Thread control
	IsTronRunning = false;
	SleepTime = 50;
        
	//Listeners
	this.addWindowListener(this);
        instructions.bupdate.addActionListener(this);
        pw_menupanel.b1.addActionListener(this);
	pw_menupanel.b2.addActionListener(this);
        pw_menupanel.b3.addActionListener(this);
	pw_menupanel.EHbox.addItemListener(this);
        pw_menupanel.SDbox.addItemListener(this);
        
        fieldon.Ebox.addItemListener(this);
        fieldon.Hbox.addItemListener(this);
        fieldon.b1.addActionListener(this);
        reset.b1.addActionListener(this);
        reset.slider1.addAdjustmentListener(this);
       
        pw_menupanel.slider1.addAdjustmentListener(this);
	inputpanel.b1.addActionListener(this);
        inputpanel.slider1.addAdjustmentListener(this);
        examples.slider1.addAdjustmentListener(this);
        
        examples.exampleA.addActionListener(this);
        examples.exampleB.addActionListener(this);
        examples.exampleC.addActionListener(this);
        
	ssc.button1.addMouseListener(this);
	ssc.button2.addMouseListener(this);
	pw_slidepanel.slider1.addAdjustmentListener(this);
	pw_slidepanel.slider2.addAdjustmentListener(this);
	pw_canvas.plot(state.x,state.efield,state.hfield,state.pfield,state.wave_impedance_phase); 
    
       StartUp(); // to set up example A 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()==about.bupdate){
            about.setVisible(false);
        }
        */
        if(evt.getSource()==instructions.bupdate){
            instructions.setVisible(false);
        }
	if(evt.getSource() == pw_menupanel.b1){
	    pwpc1.setVisible(false);
	    pwpc2.setVisible(false);
	    outputpanel.setVisible(true);
	    inputpanel.setVisible(true);
            state.IsExample1 = false;
            state.IsExample2 = false;
            state.IsExample3 = false;
            state.IsGeneral_Input = true;
            
            examples.IsExample1 = false;
            examples.IsExample2 = false;
            examples.IsExample3 = false;
            examples.IsGeneral_Input = true;
            
            state.ignition();
	}
	else if(evt.getSource() == pw_menupanel.b2){
	    //pwpc1.setVisible(true);
	    //pwpc2.setVisible(true);
	    outputpanel.setVisible(true);
	    inputpanel.setVisible(false);
	}
        else if(evt.getSource() == pw_menupanel.b3){
            //about.setVisible(true);
            instructions.setVisible(true);
        }
        else if(evt.getSource() == examples.exampleA){
            state.frequency=1.0E7;
            inputpanel.setValue(1.0E7,0);
            
            state.conductivity = 0.001;
            inputpanel.setValue(0.001,1);
            
            state.epsilon_r = 9.0;
            inputpanel.setValue(9.0,2);
            
            state.mu_r=1.0;
            inputpanel.setValue(1.0,3);
            
            state.Ex = 10.0;
            inputpanel.setValue(10.0,4);
            
            state.Phix = 0.0;
            inputpanel.setValue(0.0,5);
            
            state.total_length = 6.0;
            inputpanel.setValue(6.0,6);
            
            state.IsExample1 = true;
            state.IsExample2 = false;
            state.IsExample3 = false;
            state.IsGeneral_Input = false;
            
            StartUp();
        }
        
        else if(evt.getSource() == examples.exampleB){
            state.frequency=1.0E7;
            inputpanel.setValue(1.0E7,0);
            
            state.conductivity = 0.01;
            inputpanel.setValue(0.01,1);
            
            state.epsilon_r = 9.0;
            inputpanel.setValue(9.0,2);
            
            state.mu_r=1.0;
            inputpanel.setValue(1.0,3);
            
            state.Ex = 10.0;
            inputpanel.setValue(10.0,4);
            
            state.Phix = 0.0;
            inputpanel.setValue(0.0,5);
            
            state.total_length = 1.0;
            inputpanel.setValue(2.0,6);
            
            state.IsExample1 = false;
            state.IsExample2 = true;
            state.IsExample3 = false;
            state.IsGeneral_Input = false;
            
            StartUp();
        }
        
        else if(evt.getSource() == examples.exampleC){
            state.frequency=1.0E7;
            inputpanel.setValue(1.0E7,0);
            
            state.conductivity = 1.0;
            inputpanel.setValue(1.0,1);
            
            state.epsilon_r = 9.0;
            inputpanel.setValue(9.0,2);
            
            state.mu_r=1.0;
            inputpanel.setValue(1.0,3);
            
            state.Ex = 10.0;
            inputpanel.setValue(10.0,4);
            
            state.Phix = 0.0;
            inputpanel.setValue(0.0,5);
            
            state.total_length = 1.0;
            inputpanel.setValue(1.0,6);
            
            state.IsExample1 = false;
            state.IsExample2 = false;
            state.IsExample3 = true;
            state.IsGeneral_Input = false;
            
            StartUp();
        }
        
	else if(evt.getSource() == inputpanel.b1){// || evt.getSource() == fieldon.b1){
	    double temp, temp2;
            
	    pw_slidepanel.reset();
	    
            state.CurrentTime = 0;
            state.TotalTime = 0;
            timer.repaint();
            
            SleepTime = state.SleepTime;
	    ssc.reset();
            
            // update frequency
            try{if(inputpanel.getValue(0)<1.0E-6){
		    state.frequency=1.0E-6;
                    inputpanel.setValue(1.0E-6,0);
		}
                else if(inputpanel.getValue(0)>1.0E22){
                    state.frequency=1.0E22;
                    inputpanel.setValue(1.0E22,0);
                }
		else{
		    state.frequency = inputpanel.getValue(0);
		}
	    }
	    catch(NumberFormatException e){
		  state.frequency = 1.0;
	    }
	    
            // update conductivity
            try{if(inputpanel.getValue(1)<1.0E-21){
		    state.conductivity = 0.0;
                    inputpanel.setValue(0.0,1);
		}
		else{
		    state.conductivity = inputpanel.getValue(1);
		}
	    }
	    catch(NumberFormatException e){
		  state.conductivity = 0.0;
	    }
            
            // 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;
	    }

            // update length displayed
            try{if(inputpanel.getValue(6)<0.0){
		    state.total_length = 0.0;
                    inputpanel.setValue(0.0,6);
		}
                else if(inputpanel.getValue(6)>100.0){
                    state.total_length = 100.0;
                    inputpanel.setValue(100.0,6);
                }
		else{
		    state.total_length = inputpanel.getValue(6);
		}
	    }
	    catch(NumberFormatException e){
		  state.total_length = 1.0;
	    }

            // update window area
            try{if(inputpanel.getValue(7)<0.0){
		    state.window_area = 0.0;
                    inputpanel.setValue(0.0,7);
		}
		else{
		    state.window_area = inputpanel.getValue(7);
		}
	    }
	    catch(NumberFormatException e){
		  state.window_area = 1.0;
	    }
            
            timer.repaint();
            
            //state.Ex = 1.0;
	    //state.Phix = 0.0;
            state.CurrentTime = 0;
            timer.TotalTime = state.TotalTime;
            timer.CurrentTime = state.CurrentTime;
                        
            state.ignition();
	    state.scan_wave();
            
            ssc.clockcanvas.setStatus(0,state.NPointsT,state.DeltaT);
            ssc.reset();
            ssc.repaint();
            
	    SCROLLMAX = pw_slidepanel.slider1.getMaximum();
	    SCROLLMIN = pw_slidepanel.slider1.getMinimum();
	    state.zpos[0] = state.total_length-(SCROLLMAX-SCROLLMIN-1-pw_slidepanel.slider1.getValue())*state.total_length/(SCROLLMAX-SCROLLMIN-1);
	    SCROLLMAX = pw_slidepanel.slider2.getMaximum();
	    SCROLLMIN = pw_slidepanel.slider2.getMinimum();
	    state.zpos[1] = state.total_length-(SCROLLMAX-SCROLLMIN-1-pw_slidepanel.slider2.getValue())*state.total_length/(SCROLLMAX-SCROLLMIN-1);
	    
	    outputpanel.pwocA.repaint();
	    outputpanel.pwocB.repaint();
            
            outputpanel2.pwocC.repaint();
	    	    
            pw_canvas.ignition();
            
	    pw_canvas.setAlpha(state.alpha);
	    pw_canvas.setWavelength(state.wavelength);
	    pw_canvas.setLength(state.total_length);
	    pw_canvas.setEx(state.Ex);
	    pw_canvas.setHy(state.Ex/state.wave_impedance_mag);
	    pw_canvas.setPz(state.Power);
            
	    pw_canvas.setYRangeMax(state.Ex/state.wave_impedance_mag);
	    pw_canvas.setYRangeMin(-state.Ex/state.wave_impedance_mag);
	    pw_canvas.setXRangeMax(state.Ex);
	    pw_canvas.setXRangeMin(-state.Ex);
	    
	    pw_canvas.setPlanes(state.zpos[0]*state.wavelength,state.zpos[1]*state.wavelength);
	    pw_canvas.plot(state.x,state.efield,state.hfield,state.pfield,state.wave_impedance_phase); 
	    pw_canvas.setSkinDepth(state.skin_depth);
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel2.repaint();

            //---------------------------------------------
            pwpc1.repaint();
	    pwpc2.repaint();    
            pw_slidepanel.repaint();
            
            if(state.skin_depth > (state.total_length*state.wavelength)){
                //pw_menupanel.SDbox.setEnabled(false);
                pw_menupanel.SDbox.setVisible(false);
                pw_menupanel.show_delta = false;
                pw_menupanel.repaint();
            }
            else{
                //pw_menupanel.SDbox.setEnabled(true);
                pw_menupanel.SDbox.setVisible(true);
                pw_menupanel.show_delta = true;
                pw_menupanel.repaint();
            }
	}
        
        else if(evt.getSource() == reset.b1){
	    double temp, temp2;
	    
            state.CurrentTime = 0;
            state.TotalTime = 0;         
            
            ssc.clockcanvas.setStatus(0,state.NPointsT,state.DeltaT);
            ssc.reset();
            ssc.repaint();
            
            timer.repaint();
            
            timer.TotalTime = state.TotalTime;
            timer.CurrentTime = state.CurrentTime;
                        
            state.ignition();
	    state.scan_wave();
                        
            pw_canvas.ignition();
            
	    pw_canvas.setAlpha(state.alpha);
	    pw_canvas.setWavelength(state.wavelength);
	    pw_canvas.setLength(state.total_length);
	    pw_canvas.setEx(state.Ex);
	    pw_canvas.setHy(state.Ex/state.wave_impedance_mag);
	    pw_canvas.setPz(state.Power);
            
	    pw_canvas.setYRangeMax(state.Ex/state.wave_impedance_mag);
	    pw_canvas.setYRangeMin(-state.Ex/state.wave_impedance_mag);
	    pw_canvas.setXRangeMax(state.Ex);
	    pw_canvas.setXRangeMin(-state.Ex);
	    
	    pw_canvas.setPlanes(state.zpos[0]*state.wavelength,state.zpos[1]*state.wavelength);
	    pw_canvas.plot(state.x,state.efield,state.hfield,state.pfield,state.wave_impedance_phase); 
	    pw_canvas.setSkinDepth(state.skin_depth);
            
            pw_canvas.cleanUp();
	    pw_canvas.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){
	    	ssc.clockcanvas.increment();
		state.increment();
		state.scan_wave();
		pw_canvas.setRotor(state.current_z);
		pw_canvas.plot(state.x,state.efield,state.hfield,state.pfield,state.wave_impedance_phase); 
		pwpc1.repaint();
		pwpc2.repaint();
                timer.TotalTime = state.TotalTime;
                timer.CurrentTime = state.CurrentTime;            
                timer.repaint();
	    }
	    try{
		Thread.sleep(SleepTime);
	    }
	    catch(InterruptedException e){e.printStackTrace();}
	}
    }
    
    public void mouseClicked(MouseEvent evt){mouseHandler(evt);}
    public void mouseEntered(MouseEvent evt){;}
    public void mouseExited(MouseEvent evt){;}
    public void mousePressed(MouseEvent evt){;}
    public void mouseReleased(MouseEvent evt){;}
    
    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();
	}
	if(evt.getSource() == ssc.button2){
	    IsTronRunning = false;
	    //tron.suspend();
            ssc.IsStop = true;
            ssc.repaint();
        }
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==pw_slidepanel.slider1 || evt.getSource()==pw_slidepanel.slider2){
	    pw_canvas.setPlanes(state.zpos[0]*state.wavelength,state.zpos[1]*state.wavelength);
	    pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel2.repaint();
	    pwpc1.repaint();
	    pwpc2.repaint();
	    outputpanel.pwocA.repaint();
	    outputpanel.pwocB.repaint();
	    
            outputpanel2.pwocC.repaint();
	   
            pw_slidepanel.repaint();
	}
        if(evt.getSource()==inputpanel.slider1){
            state.SleepTime = (inputpanel.SCROLLMAX-1) - inputpanel.slider1.getValue();
            SleepTime = state.SleepTime;
            examples.slider1.setValue(inputpanel.slider1.getValue());
        }
        
        if(evt.getSource()==examples.slider1){
            state.SleepTime = (inputpanel.SCROLLMAX-1) - examples.slider1.getValue();
            SleepTime = state.SleepTime;
            inputpanel.slider1.setValue(examples.slider1.getValue());
        }
        
        if(evt.getSource()== reset.slider1){
            pw_canvas.setLines(reset.slider1.getValue()+1);
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
        }

    }
    
    public void itemStateChanged(ItemEvent evt){
	    ItemSelectable ie = evt.getItemSelectable();
       if(evt.getSource() == pw_menupanel.EHbox){
	    if(pw_menupanel.EHbox.getState()){
		pw_canvas.setPhasor(true);
		pw_canvas.repaint();
                outputpanel2.repaint();
	    }
	    else {
		pw_canvas.setPhasor(false);
		pw_canvas.repaint();
                outputpanel2.repaint();
	    }
	}

        if(evt.getSource() == pw_menupanel.SDbox){
	    if(pw_menupanel.SDbox.getState()){
		pw_canvas.setSkinDepthOn(true);
		pw_canvas.repaint();
                outputpanel2.repaint();
	    }
	    else {
		pw_canvas.setSkinDepthOn(false);
		pw_canvas.repaint();
                outputpanel2.repaint();
	    }
	}
            
        if(evt.getSource() == fieldon.Ebox){
	    if(fieldon.Ebox.getState()){
		pw_canvas.setEon(true);
		pw_canvas.repaint();
                outputpanel2.repaint();
	    }
	    else {
		pw_canvas.setEon(false);
		pw_canvas.repaint();
                outputpanel2.repaint();
	    }
	}
        
        if(evt.getSource() == fieldon.Hbox){
	    if(fieldon.Hbox.getState()){
		pw_canvas.setHon(true);
		pw_canvas.repaint();
                outputpanel2.repaint();
	    }
	    else {
		pw_canvas.setHon(false);
		pw_canvas.repaint();
                outputpanel2.repaint();
	    }
	}
    }	
    
    public void StartUp(){
        double temp, temp2;
	    pw_slidepanel.reset();
	    
            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();
	    state.scan_wave();
            
            ssc.clockcanvas.setStatus(0,state.NPointsT,state.DeltaT);
            ssc.reset();
            ssc.repaint();
            
	    SCROLLMAX = pw_slidepanel.slider1.getMaximum();
	    SCROLLMIN = pw_slidepanel.slider1.getMinimum();
	    state.zpos[0] = state.total_length-(SCROLLMAX-SCROLLMIN-1-pw_slidepanel.slider1.getValue())*state.total_length/(SCROLLMAX-SCROLLMIN-1);
	    SCROLLMAX = pw_slidepanel.slider2.getMaximum();
	    SCROLLMIN = pw_slidepanel.slider2.getMinimum();
	    state.zpos[1] = state.total_length-(SCROLLMAX-SCROLLMIN-1-pw_slidepanel.slider2.getValue())*state.total_length/(SCROLLMAX-SCROLLMIN-1);
	    
	    outputpanel.pwocA.repaint();
	    outputpanel.pwocB.repaint();
            
            pw_canvas.ignition();
            
	    pw_canvas.setAlpha(state.alpha);
	    pw_canvas.setWavelength(state.wavelength);
	    pw_canvas.setLength(state.total_length);
	    pw_canvas.setEx(state.Ex);
	    pw_canvas.setHy(state.Ex/state.wave_impedance_mag);
	    pw_canvas.setPz(state.Power);
            
	    pw_canvas.setYRangeMax(state.Ex/state.wave_impedance_mag);
	    pw_canvas.setYRangeMin(-state.Ex/state.wave_impedance_mag);
	    pw_canvas.setXRangeMax(state.Ex);
	    pw_canvas.setXRangeMin(-state.Ex);
	    
	    pw_canvas.setPlanes(state.zpos[0]*state.wavelength,state.zpos[1]*state.wavelength);
	    pw_canvas.plot(state.x,state.efield,state.hfield,state.pfield,state.wave_impedance_phase); 
	    pw_canvas.setSkinDepth(state.skin_depth);
            
            pw_canvas.cleanUp();
	    pw_canvas.repaint();
            outputpanel2.repaint();

            //---------------------------------------------
            
            if(state.skin_depth > (state.total_length*state.wavelength)){
                //pw_menupanel.SDbox.setEnabled(false);
                pw_menupanel.SDbox.setVisible(false);
                pw_menupanel.show_delta = false;
                pw_menupanel.repaint();
            }
            else{
                //pw_menupanel.SDbox.setEnabled(true);
                pw_menupanel.SDbox.setVisible(true);
                pw_menupanel.show_delta = true;
                pw_menupanel.repaint();
            }
    }
}
