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

public class Polarization_Controls extends Panel implements AdjustmentListener, ActionListener, ItemListener{   
	//private static final Color bgcolor = new Color(216,216,191);
	private static final Color bgcolor = new Color(236,236,236);
	private static final Color slidercolor1 = new Color(250,250,250);
	
        Scrollbar slider1, slider2, slider3, slider4, slider_speed;
	TextField text1, text2, text3, text4;
	public Button b1, b_about;
	
	private Checkbox c1, c2, c3, c4;
	private Checkbox c5, c6;
	private Polarization_State state;
	CheckboxGroup  cgrid1, cgrid2, cgrid3;
	//final int ScrollPhaseMin=0;
	//final int ScrollPhaseMax=360;
	final int ScrollPhaseMin=-360;
	final int ScrollPhaseMax=360;
	final int ScrollAmpMin=0;
	final int ScrollAmpMax=1000;
        final int SpeedMax = 150;
        final int ypos1=65, ypos2=115, ypos3=165, ypos4=215, ypos5=270, ypos6=310;
                
                
	public Polarization_Controls(){
		super();
		setBackground(bgcolor);
		setLayout(null);
		
		state = new Polarization_State();
		
		text1=new TextField("1.0",8);	
		text2=new TextField("0.0",8);
		text3=new TextField("1.0",8);	
		text4=new TextField("0.0",8);
		text1.setBackground(Color.white);
		text2.setBackground(Color.white);
		text3.setBackground(Color.white);
		text4.setBackground(Color.white);
		
		cgrid1=new CheckboxGroup();
		cgrid2=new CheckboxGroup();
		cgrid3=new CheckboxGroup();

		c1 = new Checkbox("deg",cgrid1,true);
		c2 = new Checkbox("rad",cgrid1,false);

		c3 = new Checkbox("deg",cgrid2,true);
		c4 = new Checkbox("rad",cgrid2,false);

		c5 = new Checkbox("On",cgrid3,false);
		c6 = new Checkbox("Off",cgrid3,true);

		add(c1);
		add(c2);
		add(c3);
		add(c4);
		add(c5);
		add(c6);
                
                /*
                c1.setBounds(135,ypos2-20,50,20);
		c2.setBounds(190,ypos2-20,50,20);
		c3.setBounds(135,ypos4-20,50,20);
		c4.setBounds(190,ypos4-20,50,20);
		c5.setBounds(105,ypos6,45,20);
		c6.setBounds(155,ypos6,45,20);
                */
                c1.setBounds(130,ypos2-20,55,20);
		c2.setBounds(185,ypos2-20,55,20);
		c3.setBounds(130,ypos4-20,55,20);
		c4.setBounds(185,ypos4-20,55,20);
		c5.setBounds(105,ypos6,55,20);
		c6.setBounds(160,ypos6,55,20);

		c1.setBackground(bgcolor);
		c2.setBackground(bgcolor);
		c3.setBackground(bgcolor);
		c4.setBackground(bgcolor);
		c5.setBackground(bgcolor);
		c6.setBackground(bgcolor);
		
		slider1=new Scrollbar(Scrollbar.HORIZONTAL,ScrollAmpMax,1,ScrollAmpMin,ScrollAmpMax+1);
		slider2=new Scrollbar(Scrollbar.HORIZONTAL,0,1,ScrollPhaseMin,ScrollPhaseMax+1);
		slider3=new Scrollbar(Scrollbar.HORIZONTAL,ScrollAmpMax,1,ScrollAmpMin,ScrollAmpMax+1);
		slider4=new Scrollbar(Scrollbar.HORIZONTAL,0,1,ScrollPhaseMin,ScrollPhaseMax+1);
                
                slider_speed=new Scrollbar(Scrollbar.HORIZONTAL,115,1,0,SpeedMax);
		//slider1.setBackground(bgcolor.darker());
		//slider2.setBackground(bgcolor.darker());
		//slider3.setBackground(bgcolor.darker());
		//slider4.setBackground(bgcolor.darker());
		slider1.setForeground(Color.black);
		slider2.setForeground(Color.black);
		slider3.setForeground(Color.black);
		slider4.setForeground(Color.black);
		slider1.setBackground(slidercolor1);
		slider2.setBackground(slidercolor1);
		slider3.setBackground(slidercolor1);
		slider4.setBackground(slidercolor1);
		
                slider_speed.setForeground(Color.black);
		slider_speed.setBackground(slidercolor1);
                
		b1=new Button("Update");
		b_about=new Button("Instructions");
                
		add(text1);
		add(text2);
		add(text3);
		add(text4);
		add(slider1);
		add(slider2);
		add(slider3);
		add(slider4);
                add(slider_speed);
		add(b1);
                add(b_about);
		
		text1.setBounds(180,ypos1,60,20);
		text2.setBounds(180,ypos2,60,20);
		text3.setBounds(180,ypos3,60,20);
		text4.setBounds(180,ypos4,60,20);
		
		slider1.setBounds(10,ypos1+4,160,15);
		slider2.setBounds(10,ypos2+4,160,15);
		slider3.setBounds(10,ypos3+4,160,15);
		slider4.setBounds(10,ypos4+4,160,15);
                slider_speed.setBounds(120,ypos5-1,119,15);
                
                Panel p_speed = new Panel();
		    p_speed.setBackground(Color.black);
		    add(p_speed);
		    p_speed.setBounds(119,ypos5-2,121,17);
                    
		b1.setBounds(10,ypos5,100,20);
		b1.setBackground(slidercolor1);
                
                //------------------------------------------------------------
                int y_about = 10;
                b_about.setBounds(53,y_about,140,20);
		b_about.setBackground(Color.white);
		
                Panel p_about = new Panel();
		    p_about.setBackground(Color.black);
		    add(p_about);
		    p_about.setBounds(52,y_about-1,142,22);
                //------------------------------------------------------------
                    
                Panel slida = new Panel();
		    slida.setBackground(Color.black);
		    add(slida);
		    slida.setBounds(9,ypos1+3,162,17);
		Panel slide1 = new Panel();
		    slide1.setBackground(Color.green);
		    add(slide1);
		    slide1.setBounds(7,ypos1+1,166,21);
                    
                Panel slidb = new Panel();
		    slidb.setBackground(Color.black);
		    add(slidb);
		    slidb.setBounds(9,ypos2+3,162,17);
		Panel slide2 = new Panel();
		    slide2.setBackground(Color.green);
		    add(slide2);
		    slide2.setBounds(7,ypos2+1,166,21);
                    
                Panel slide1b = new Panel();
		    slide1b.setBackground(Color.black);
		    add(slide1b);
		    slide1b.setBounds(6,ypos1,168,23);
		Panel slide2b = new Panel();
		    slide2b.setBackground(Color.black);
		    add(slide2b);
		    slide2b.setBounds(6,ypos2,168,23);    
                    
                    
                Panel slidc = new Panel();
		    slidc.setBackground(Color.black);
		    add(slidc);
		    slidc.setBounds(9,ypos3+3,162,17);
		Panel slide3 = new Panel();
		    slide3.setBackground(Color.red);
		    add(slide3);
		    slide3.setBounds(7,ypos3+1,166,21);
                Panel slidd = new Panel();
		    slidd.setBackground(Color.black);
		    add(slidd);
		    slidd.setBounds(9,ypos4+3,162,17);
		Panel slide4 = new Panel();
		    slide4.setBackground(Color.red);
		    add(slide4);
		    slide4.setBounds(7,ypos4+1,166,21);

                Panel slide3b = new Panel();
		    slide3b.setBackground(Color.black);
		    add(slide3b);
		    slide3b.setBounds(6,ypos3,168,23);
		Panel slide4b = new Panel();
		    slide4b.setBackground(Color.black);
		    add(slide4b);
		    slide4b.setBounds(6,ypos4,168,23);    
                    
		Panel updat1 = new Panel();
		    updat1.setBackground(Color.white);
		    add(updat1);
		    updat1.setBounds(9,ypos5-1,102,22);
		Panel updat2 = new Panel();
		    updat2.setBackground(Color.black);
		    add(updat2);
		    updat2.setBounds(8,ypos5-2,104,24);
				
		//Listeners
		slider1.addAdjustmentListener(this);
		slider2.addAdjustmentListener(this);
		slider3.addAdjustmentListener(this);
		slider4.addAdjustmentListener(this);
		c1.addItemListener(this);
		c2.addItemListener(this);
		c3.addItemListener(this);
		c4.addItemListener(this);
		c5.addItemListener(this);
		c6.addItemListener(this);
		b1.addActionListener(this);
	}
	
	public void paint(Graphics g){
		//g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
		g.clearRect(0,0,getSize().width,getSize().height);

                g.drawLine(0,getSize().height-1,getSize().width-1,getSize().height-1);
		g.drawLine(getSize().width-1,0,getSize().width-1,getSize().height-1);
		g.setColor(Color.white);
		g.drawLine(0,0,getSize().width-1,0);
		g.drawLine(0,0,0,getSize().height-1);
		
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                g.setColor(Color.black);
		g.setFont(new Font("SanSerif",Font.BOLD,12));
		//g.setColor(Color.green);
		g.drawString("Amplitude X",60,ypos1-5);
		g.drawString("Phase X",60,ypos2-5);
		//g.setColor(Color.red);
		g.drawString("Amplitude Y",60,ypos3-5);
		g.drawString("Phase Y",60,ypos4-5);
		g.setFont(new Font("SanSerif",Font.BOLD,12));
		g.setColor(Color.blue.darker());
		g.drawString("Trace:",55,ypos6+12);
                
                g.setColor(Color.black);
            int sety = ypos5-7;
            int sety2 = ypos5+21;
            int setx = 120;
            int setx2 = 119;
            
            MaestroG.subscripter("Animation speed","","",g,10,setx+18,sety);
            
            g.setColor(Color.blue.darker());
            MaestroG.drawArrowtip(setx+10,sety2,8,g);
            MaestroG.drawArrowtip(setx+17,sety2,8,g);

            g.setColor(Color.red.darker());
            MaestroG.drawArrowtip(setx+setx2-17,sety2,7,g);
            MaestroG.drawArrowtip(setx+setx2-10,sety2,7,g);
            //------------------------------------------------------------------
	}
	
	public void adjustmentValueChanged(AdjustmentEvent evt){
		
		if(slider1.getValue()>(ScrollAmpMax-ScrollAmpMin)){
		    slider1.setValue(ScrollAmpMax-ScrollAmpMin);
		    state.ampA = 1.0;
		}
		else{
		    state.ampA = (double)slider1.getValue()/(ScrollAmpMax-ScrollAmpMin);    
		}
		if(slider3.getValue()>(ScrollAmpMax-ScrollAmpMin)){
		    slider3.setValue(ScrollAmpMax-ScrollAmpMin);
		    state.ampB = 1.0;
		}
		else{
		    state.ampB = (double)slider3.getValue()/(ScrollAmpMax-ScrollAmpMin);
		}
	   
	   if(slider2.getValue()>=360){
		slider2.setValue(360);
		state.phaseA = Math.PI;
		
		if(c2.getState()){
		    //radians
		    text2.setText(String.valueOf(MaestroA.rounder(Math.PI,4)));
		
		}
		else {
		    //degrees
		    text2.setText(String.valueOf(MaestroA.rounder(Math.PI*180/Math.PI,4)));
		}
	   }
	   else{
		state.phaseA = (double)slider2.getValue()*2*Math.PI/(ScrollPhaseMax-ScrollPhaseMin);
		if(c2.getState()){
		    //radians
		    text2.setText(String.valueOf(MaestroA.rounder(state.phaseA,4)));
		
		}
		else {
		    //degrees
		    text2.setText(String.valueOf(MaestroA.rounder(state.phaseA*180/Math.PI,4)));
		}
	   }
	   
	   if(slider4.getValue()>=(360)){
		slider4.setValue(360);
		state.phaseB = Math.PI;
		if(c4.getState()){
		    //radians
		    text4.setText(String.valueOf(MaestroA.rounder(Math.PI,4)));
		}
		else {
		    //degrees
		    text4.setText(String.valueOf(MaestroA.rounder(Math.PI*180/Math.PI,4)));
		}
	   }
	   else{
		state.phaseB = (double)slider4.getValue()*2*Math.PI/(ScrollPhaseMax-ScrollPhaseMin);
		if(c4.getState()){
		    //radians
		    text4.setText(String.valueOf(MaestroA.rounder(state.phaseB,4)));
		}
		else {
		    //degrees
		    text4.setText(String.valueOf(MaestroA.rounder(state.phaseB*180/Math.PI,4)));
		}
	   
	   }
	   
	    
	    
	    text1.setText(String.valueOf(MaestroA.rounder(state.ampA,3)));
	    text3.setText(String.valueOf(MaestroA.rounder(state.ampB,3)));
	    
	    state.reset();
	    
	    /*if(c2.getState()){
		//radians
		text2.setText(String.valueOf(MaestroA.rounder(state.phaseA,4)));
		
	    }
	    else {
		//degrees
		text2.setText(String.valueOf(MaestroA.rounder(state.phaseA*180/Math.PI,3)));
	    }
	    if(c4.getState()){
		//radians
		text4.setText(String.valueOf(MaestroA.rounder(state.phaseB,4)));
	    }
	    else {
		//degrees
		text4.setText(String.valueOf(MaestroA.rounder(state.phaseB*180/Math.PI,3)));
	    }*/
	}
	
	public void actionPerformed(ActionEvent evt){
	   state.reset();
	   int  DIGITS = 3;
	   synchronized(this){
	   if(evt.getSource() == b1){
		Toolkit.getDefaultToolkit().beep();
		state.ampA = Double.valueOf(text1.getText()).doubleValue();
		if(state.ampA > 1.0) { 
		    state.ampA = 1.0;
		    text1.setText(String.valueOf(MaestroA.rounder(state.ampA,DIGITS)));
		}
		if(state.ampA < 0.0) {
		    state.ampA = 0.0;
		    text1.setText(String.valueOf(state.ampA));
		}
		state.ampB = Double.valueOf(text3.getText()).doubleValue();
		if(state.ampB > 1.0) { 
		    state.ampB = 1.0;
		    text3.setText(String.valueOf(MaestroA.rounder(state.ampB,DIGITS)));
		}
		if(state.ampB < 0.0) {
		    state.ampB = 0.0;
		    text3.setText(String.valueOf(MaestroA.rounder(state.ampB,DIGITS)));
		}
		if(c2.getState()){
		    state.phaseA=Double.valueOf(text2.getText()).doubleValue();
		    text2.setText(String.valueOf(state.phaseA));
		    if(state.phaseA > Math.PI){
	    		state.phaseA = Math.PI;
			text2.setText(String.valueOf(MaestroA.rounder(Math.PI,DIGITS+1)));
		    }
		    if(state.phaseA < (-Math.PI)){
			state.phaseA = -Math.PI;
			text2.setText(String.valueOf(-MaestroA.rounder(Math.PI,DIGITS+1)));
		    }
		}
		else{
		    state.phaseA=Double.valueOf(text2.getText()).doubleValue()*Math.PI/180;
		    text2.setText(String.valueOf(MaestroA.rounder(state.phaseA*180/Math.PI,4)));
		    if(state.phaseA > Math.PI){
			state.phaseA = Math.PI;
			text2.setText(String.valueOf(180.0));
		    }
		    if(state.phaseA < (-Math.PI)){
			state.phaseA = -Math.PI;
			text2.setText(String.valueOf(-180.0));
		    }	
	    	}
		if(c4.getState()){
		    state.phaseB=Double.valueOf(text4.getText()).doubleValue();
		    text4.setText(String.valueOf(MaestroA.rounder(state.phaseB,DIGITS+1)));
		    if(state.phaseB > Math.PI){
			state.phaseB = Math.PI;
			text4.setText(String.valueOf(MaestroA.rounder(Math.PI,DIGITS+1)));
		    }
		    if(state.phaseB < (-Math.PI)){
			state.phaseB = -Math.PI;
			text4.setText(String.valueOf(-MaestroA.rounder(Math.PI,DIGITS+1)));
		    }
		}
		else{
			state.phaseB=Double.valueOf(text4.getText()).doubleValue()*Math.PI/180;
			text4.setText(String.valueOf(MaestroA.rounder(state.phaseB*180/Math.PI,DIGITS+1)));
			if(state.phaseB > Math.PI){
				state.phaseB = Math.PI;
				text4.setText(String.valueOf(180.0));
			}
			if(state.phaseB < -Math.PI){
				state.phaseB = -Math.PI;
				text4.setText(String.valueOf(-180.0));
			}	
		}	
		
		slider1.setValue((int)((ScrollAmpMax-ScrollAmpMin)*state.ampA));
		slider2.setValue((int)((ScrollPhaseMax-ScrollPhaseMin)*state.phaseA/(2.0*Math.PI)));
		slider3.setValue((int)((ScrollAmpMax-ScrollAmpMin)*state.ampB));
		slider4.setValue((int)((ScrollPhaseMax-ScrollPhaseMin)*state.phaseB/(2.0*Math.PI)));
		//state.reset();
	      }}
	}
	
	public void itemStateChanged(ItemEvent evt){
	    int  DIGITS = 3;
	    if(evt.getSource() == c1 || evt.getSource() == c2){
		
		if(c2.getState()){
		    text2.setText(String.valueOf(MaestroA.rounder(state.phaseA,DIGITS+1)));
		}
		else{
		    text2.setText(String.valueOf(MaestroA.rounder(state.phaseA*180/Math.PI,DIGITS+1)));
		}
	    }
	    else if(evt.getSource() == c3 || evt.getSource() == c4){
		if(c4.getState()){
		    text4.setText(String.valueOf(MaestroA.rounder(state.phaseB,DIGITS+1)));
		}
		else{
		    text4.setText(String.valueOf(MaestroA.rounder(state.phaseB*180/Math.PI,DIGITS+1)));
		}
	    }
	    else if(evt.getSource() == c5 || evt.getSource() == c6){
		state.isTracerOn = c5.getState();
                state.reset();
                repaint();
	    }
	    //state.reset();
	}
	
	public synchronized void setState(Polarization_State state){
		this.state = state;
	}
	
}//End of Polarization_Controls.java
