import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;

public class Polarization extends Frame implements MouseListener, AdjustmentListener, ActionListener, Runnable, WindowListener{
    //About about;
    Instructions instructions;
private TitlePanel titlepanel;
private Polarization_State state;
private PolarCanvas polarcanvas;
private Polarization_Controls pcontrols;
private StartStopClock ssc;
private SimpleMessageCanvas smc1, smc2;
private Thread tron;
private boolean isTronRunning;

    public static void main(String[] args){
        int xmove = 20;
        int ymove = 45;
        Polarization f = new Polarization();
        f.setSize(635+xmove,466+ymove);
        f.setVisible(true);
        f.setLayout(null);
    }
    public Polarization(){
	
	setLayout(null);
	state = new Polarization_State();
	
        int xmove = 10;
        int ymove = 34;
        
	titlepanel = new TitlePanel("Module 7.3","Polarization","I");
	//add(titlepanel);
	//titlepanel.setBounds(10,10,360,40);
	titlepanel.setBounds(10+xmove,10+ymove,250,40);
	
	polarcanvas = new PolarCanvas();
	//add(polarcanvas);
	//polarcanvas.setBounds(375,55,360,360);
	polarcanvas.setBounds(266+xmove,56+ymove,358,358);
        
        Panel ppol = new Panel();
	    ppol.setBackground(Color.black);
	    //add(ppol);
	    ppol.setBounds(265+xmove,55+ymove,360,360);
	
	ssc = new StartStopClock();
	//add(ssc);
	//ssc.setBounds(415,390,180,60);
	ssc.setBounds(40+xmove,390+ymove,189,56);
	
	pcontrols = new Polarization_Controls();
	//add(pcontrols);
	//pcontrols.setBounds(375,55,250,400);
	pcontrols.setBounds(12+xmove,57+ymove,246,396);
		
	smc1 = new SimpleMessageCanvas("The wave travels in the z-direction (towards the viewer)");
	//add(smc1);
	smc1.setFont(new Font("SanSerif",Font.BOLD,11));
	//smc1.setBounds(10,425,360,30);
	//smc1.setBounds(265,425,360,30);
	smc1.setBounds(267+xmove,422+ymove,356,31);
	
	smc2 = new SimpleMessageCanvas();
	//add(smc2);
	//smc2.setBounds(375,10,250,40);
	smc2.setBounds(267+xmove,12+ymove,356,36);
	smc2.setFont(new Font("SanSerif",Font.BOLD,11));
	smc2.setMessage(state.getInfo());
        
        /*
        about = new About();
        about.setVisible(false);
        //add(about);
        about.setBounds(7,55,621,400);
        */
        instructions = new Instructions();
        //instructions.setVisible(false);12,57,246,396
        instructions.setBounds(10+xmove,55+ymove,615,400);
	
	polarcanvas.setState(state);
	pcontrols.setState(state);
	
	Panel p11 = new Panel();
	    p11.setBackground(Color.white);
	    //add(p11);
	    p11.setBounds(266+xmove,421+ymove,358,33);
	
	Panel p10 = new Panel();
	    p10.setBackground(Color.black);
	    //add(p10);
	    p10.setBounds(265+xmove,420+ymove,360,35);
	    
	Panel p21 = new Panel();
	    p21.setBackground(Color.white);
	    //add(p21);
	    p21.setBounds(266+xmove,11+ymove,358,38);
	
	Panel p20 = new Panel();
	    p20.setBackground(Color.black);
	    //add(p20);
	    p20.setBounds(265+xmove,10+ymove,360,40);
	    
	Panel p31 = new Panel();
	    p31.setBackground(Color.white);
	    //add(p31);
	    p31.setBounds(11+xmove,56+ymove,248,398);
	
	Panel p30 = new Panel();
	    p30.setBackground(Color.black);
	    //add(p30);
	    p30.setBounds(10+xmove,55+ymove,250,400);
		
	Panel p0 = new Panel();
	    p0.setBackground(Color.cyan);
	    //add(p0);
	    p0.setBounds(3+xmove,3+ymove,629,460);
	    
	Panel p00 = new Panel();
	    p00.setBackground(Color.black);
	    //add(p00);
	    p00.setBounds(xmove,ymove,635,466);
	
            //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(polarcanvas);
	    add(ssc);
	    add(pcontrols);
	    add(smc1);
	    add(smc2);
            add(ppol);
	    add(p11);
	    add(p10);
	    add(p21);
	    add(p20);
	    add(p31);
	    add(p30);
	    add(p0);
	    add(p00);
	    state.LicenseExpired = false; 	
            //}

	ssc.setStatus(0,360,2);
	isTronRunning = false;
	
	//Listeners
        //about.bupdate.addActionListener(this);
        this.addWindowListener(this);
        instructions.bupdate.addActionListener(this);
        
	ssc.button1.addMouseListener(this);
	ssc.button2.addMouseListener(this);
	
	pcontrols.slider1.addAdjustmentListener(this);
	pcontrols.slider2.addAdjustmentListener(this);
	pcontrols.slider3.addAdjustmentListener(this);
	pcontrols.slider4.addAdjustmentListener(this);
	pcontrols.b1.addActionListener(this);
        pcontrols.b_about.addActionListener(this);
        pcontrols.slider_speed.addAdjustmentListener(this);
    }

    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;
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        //g.setColor(Color.white);	
	//g.fillRect(0,0,635,486);  	
	/*	
	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);
	} 
        
        
        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,483);
        //g.setFont(fonto);
        //g.drawString("Amanogawa, "+annonuovo+" - All Rights Reserved",30,481);
}

    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 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){
            if(tron == null){
		start();
	    }
	    else {
		//tron.resume();
	    }
            ssc.IsStop = false;
            ssc.repaint();
	    isTronRunning = true;
	    //polarcanvas.reset();
	    //state.reset(); 
	    //ssc.reset();
	}
	if(evt.getSource()==ssc.button2){
	    isTronRunning = false;
            ssc.IsStop = true;
            ssc.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();
	}
	if(evt.getSource() == ssc.button2){
	    IsTronRunning = false;
	    //tron.suspend();
            ssc.IsStop = true;
            ssc.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){
	    try{
		   Thread.sleep(state.SleepTime);
	    }
	catch(InterruptedException e){e.printStackTrace();}   
	    if(isTronRunning){
		state.increment();
		if(state.RESET){smc2.setMessage(state.getInfo());}
		ssc.increment();
		polarcanvas.repaint();
	    }
	}
    }
	
    public void pause(boolean tmp){
	 if(tmp){
		isTronRunning = false;
		//tron.suspend();
	 }
	 else {
		isTronRunning = true;
		//tron.resume();
	 }
    }
    
    public void actionPerformed(ActionEvent evt){
        /*
           if(evt.getSource()==about.bupdate){
                about.setVisible(false);
           }
        */

           if(evt.getSource()==instructions.bupdate){
               instructions.setVisible(false);
           }
           
           if(evt.getSource()==pcontrols.b_about){
               //about.setVisible(true);
               instructions.setVisible(true);
           }
        
	   synchronized(this){
	   if(evt.getSource() == pcontrols.b1){
		state.reset();
		{smc2.setMessage(state.getInfo());}
		smc2.repaint();
                polarcanvas.repaint();
	   }
	}
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
        if(evt.getSource() == pcontrols.slider1 || evt.getSource() == pcontrols.slider2 
           || evt.getSource() == pcontrols.slider3 || evt.getSource() == pcontrols.slider4){
		state.reset();
                {smc2.setMessage(state.getInfo());}
		smc2.repaint();
                polarcanvas.repaint();
        }		
        if(evt.getSource() == pcontrols.slider_speed ){
            state.SleepTime = pcontrols.SpeedMax -pcontrols.slider_speed.getValue();
        }
    }
	
	
    
}
