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


public class SlidePanel extends Panel implements ItemListener, ActionListener, AdjustmentListener{
	Antenna ant;
        
	Scrollbar slider1, slider2, slider3, slider4;
	TextField text1, text2;
	TextField text3, text4;
	TextField text5, text6;
	Checkbox c1, c2;
	CheckboxGroup cgrid1;
	private static final Color bgcolor = new Color(236,236,221);
	
	
	Button button1;
        // CHANGE ==============================================================
        public Button b2a, b2b, b3a, b3b, b4a, b4b, b5a, b5b;
        //======================================================================
        
	Rectangle r;
	final int ScrollMin=0;
	final int ScrollMax=3001;
	final int ScrollFaseMax=361;
	final int ScrollFaseMin=0;
	final int ScrollMax2 = 1001;
	double fase = 0.0;
	
        SlidePanel(Antenna ant){
		super();
		this.ant=ant;
		setBackground(Color.white);
		cgrid1=new CheckboxGroup();
		c1 = new Checkbox("deg",cgrid1,true);
		c2 = new Checkbox("rad",cgrid1,false);

		setLayout(null);
		setBackground(bgcolor);
		c1.addItemListener(this);
		c2.addItemListener(this);
		add(c1);
		add(c2);
		c1.setBounds(110,83,55,17);
		c2.setBounds(165,83,55,17);
		c1.setBackground(bgcolor);
		c2.setBackground(bgcolor);

		text1=new TextField("0.5",8);	
		text2=new TextField("0.5",8);
		text3=new TextField("0.0",8);
		text4=new TextField("0.5",8);
		text5=new TextField("1.0",8);
		text6=new TextField("1.0",8);
	
		slider1=new Scrollbar(Scrollbar.HORIZONTAL,500,1,ScrollMin,ScrollMax);
		slider2=new Scrollbar(Scrollbar.HORIZONTAL,500,1,ScrollMin,ScrollMax);
		slider3=new Scrollbar(Scrollbar.HORIZONTAL,180,1,ScrollFaseMin,ScrollFaseMax);
		slider4=new Scrollbar(Scrollbar.HORIZONTAL,500,1,ScrollMin,ScrollMax2);
                slider1.setBackground(new Color(255,255,235));
                slider2.setBackground(new Color(255,255,235));
                slider3.setBackground(new Color(255,255,235));
                slider4.setBackground(new Color(255,255,235));
                
		button1=new Button("Update");
                button1.setBackground(Color.white);
		add(text1);
		add(text2);
		add(text3);
		add(text4);
		add(text5);
		add(text6);
                
                // CHANGE  =====================================================
                b2a = new Button("<");
                b2a.setBackground(bgcolor);        
                b2b = new Button(">");
                b2b.setBackground(bgcolor);
                b3a = new Button("<");
                b3a.setBackground(bgcolor);
                b3b = new Button(">");
                b3b.setBackground(bgcolor);
                b4a = new Button("<");
                b4a.setBackground(bgcolor);
                b4b = new Button(">");
                b4b.setBackground(bgcolor);
                b5a = new Button("<");
                b5a.setBackground(bgcolor);
                b5b = new Button(">");
                b5b.setBackground(bgcolor);
                
                add(b2a); add(b2b); add(b3a); add(b3b); add(b4a); add(b4b); add(b5a); add(b5b);
                //==============================================================

		slider1.addAdjustmentListener(this);
		slider2.addAdjustmentListener(this);
		slider3.addAdjustmentListener(this);
		slider4.addAdjustmentListener(this);
		// CHANGE ======================================================
                b2a.addActionListener(this);
                b2b.addActionListener(this);
                b3a.addActionListener(this);
                b3b.addActionListener(this);
                b4a.addActionListener(this);
                b4b.addActionListener(this);
                b5a.addActionListener(this);
                b5b.addActionListener(this);
                //==============================================================
		add(slider1);
		add(slider2);
		add(slider3);
		add(slider4);
		
		button1.addActionListener(this);
		add(button1);

		text1.setBounds(220,30,50,20);
                Panel ptext1 = new Panel();
		ptext1.setBackground(Color.black);
		add(ptext1);
		ptext1.setBounds(219,29,52,22);
		text2.setBounds(220,55,50,20);
                Panel ptext2 = new Panel();
		ptext2.setBackground(Color.black);
		add(ptext2);
		ptext2.setBounds(219,54,52,22);
		text3.setBounds(220,102,50,20);
                Panel ptext3 = new Panel();
		ptext3.setBackground(Color.black);
		add(ptext3);
		ptext3.setBounds(219,101,52,22);
		text4.setBounds(220,145,50,20);
                Panel ptext4 = new Panel();
		ptext4.setBackground(Color.black);
		add(ptext4);
		ptext4.setBounds(219,144,52,22);
		text5.setBounds(25,195,50,20);
                Panel ptext5 = new Panel();
		ptext5.setBackground(Color.black);
		add(ptext5);
		ptext5.setBounds(24,194,52,22);
		text6.setBounds(115,195,50,20);
                Panel ptext6 = new Panel();
		ptext6.setBackground(Color.black);
		add(ptext6);
		ptext6.setBounds(114,194,52,22);
		
		slider1.setBounds(23,33,185,15);
		// CHANGE  =====================================================
                b2a.setBounds(23,33,14,15);
                b2b.setBounds(194,33,14,15);
                //==============================================================
                slider2.setBounds(23,58,185,15);
                // CHANGE  =====================================================
                b3a.setBounds(23,58,14,15);
                b3b.setBounds(194,58,14,15);
                //==============================================================
                
		slider3.setBounds(23,105,185,15);
                // CHANGE  =====================================================
                b4a.setBounds(23,105,14,15);
                b4b.setBounds(194,105,14,15);
                //==============================================================
                
		slider4.setBounds(23,148,185,15);
                // CHANGE  =====================================================
                b5a.setBounds(23,148,14,15);
                b5b.setBounds(194,148,14,15);
                //==============================================================
                
	    Panel pslider1 = new Panel();
		pslider1.setBackground(Color.black);
		add(pslider1);
		pslider1.setBounds(22,32,187,17);
	    
	    Panel pslider2 = new Panel();
		pslider2.setBackground(Color.black);
		add(pslider2);
		pslider2.setBounds(22,57,187,17);
	    
	    Panel pslider3 = new Panel();
		pslider3.setBackground(Color.black);
		add(pslider3);
		pslider3.setBounds(22,104,187,17);
	    
	    Panel pslider4 = new Panel();
		pslider4.setBackground(Color.black);
		add(pslider4);
		pslider4.setBounds(22,147,187,17);
	    
		button1.setBounds(195,190,90,30);
		Panel pb1 = new Panel();
                pb1.setBackground(Color.black);
                add(pb1);
                pb1.setBounds(194,189,92,32);
	}
	
	public void paint(Graphics g){
		r=getBounds();
		
                // This is to clear background (smoothing makes fonts pixelated on re-writes)
                g.setColor(bgcolor);
                g.fillRect(0,0,r.width-1,r.height-1);
                
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
		g.setFont(new Font("Helvetica",Font.PLAIN,11));
		g.setColor(Color.blue);
		g.drawString("Dipole Lengths (wavelengths)",10,25);
		g.drawString("Phase Difference",10,95);
		g.drawString("Distance between dipoles (wavelengths)",10,140);
		g.drawString("Max Dipole Current (Amps)",10,185);
		g.setColor(Color.black);
		g.setFont(new Font("Serif",Font.PLAIN,14));
		g.drawString(" \u03bb ",272,45);
		g.drawString(" \u03bb ",272,70);
		g.drawString(" \u03bb ",272,160);
		
		g.draw3DRect(0,0,r.width-1,r.height-1,false);
		g.setColor(Color.black);
		g.setFont(new Font("Sanserif",Font.BOLD,12));
		g.drawString("A",10,45);
		g.drawString("B",10,70);
		g.drawString("A",10,210);
		g.drawString("B",100,210);
	}


    	public void adjustmentValueChanged(AdjustmentEvent evt){
	    if(evt.getSource()==slider1 || evt.getSource()==slider2 || evt.getSource()==slider3 || evt.getSource()==slider4){
	
                double dipolelengthA, dipolelengthB;
                double fase=ant.fase, distancia;

                if((double)ant.dipolelengthmaxA*slider1.getValue()/(double)(ScrollMax-1)>ant.dipolelengthmaxA){
                    dipolelengthA=ant.dipolelengthmaxA;
                    slider1.setValue(ScrollMax-1);
                }
                else{
                    dipolelengthA=(double)ant.dipolelengthmaxA*slider1.getValue()/(double)(ScrollMax-1);
                }

                if((double)ant.dipolelengthmaxB*slider2.getValue()/(double)(ScrollMax-1)>ant.dipolelengthmaxB){
                    dipolelengthB=ant.dipolelengthmaxB;
                    slider2.setValue(ScrollMax-1);
                }
                else{
                    dipolelengthB=(double)ant.dipolelengthmaxB*slider2.getValue()/(double)(ScrollMax-1);
                }

                if((double)ant.distanciamax*slider4.getValue()/(double)(ScrollMax2-1)>ant.distanciamax){
                    distancia = ant.distanciamax;
                    slider4.setValue(ScrollMax2-1);
                }
                else{
                    distancia=(double)ant.distanciamax*slider4.getValue()/(double)(ScrollMax2-1);
                }

                if(dipolelengthA==0.0) dipolelengthA=1.0e-10;
                if(dipolelengthB==0.0) dipolelengthB=1.0e-10;

                text1.setText(String.valueOf(MaestroA.rounder(dipolelengthA,4)));
                text2.setText(String.valueOf(MaestroA.rounder(dipolelengthB,4)));

                if(c2.getState()){
                    fase=(double)((slider3.getValue()-180)*2*Math.PI/(ScrollFaseMax-1));
                    text3.setText(String.valueOf(MaestroA.rounder(fase,3)));
                    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue());
                }
                else if(c1.getState()){
                    fase=(double)(2*Math.PI*(slider3.getValue()-180)/(ScrollFaseMax-1));
                    text3.setText(String.valueOf(MaestroA.rounder(fase*180/Math.PI,2)));
                    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue()*Math.PI/180);
                }

                text4.setText(String.valueOf(MaestroA.rounder(distancia,4)));

                //=====================================

                    ant.dipolelengthA=Double.valueOf(text1.getText()).doubleValue();
                    ant.dipolecurrentA=Double.valueOf(text5.getText()).doubleValue();
                    ant.dipolelengthB=Double.valueOf(text2.getText()).doubleValue();
                    ant.dipolecurrentB=Double.valueOf(text6.getText()).doubleValue();

                    ant.distancia=Double.valueOf(text4.getText()).doubleValue();
                    if(ant.dipolelengthA < 0.0){
                        ant.dipolelengthA=0.0;
                        text1.setText(String.valueOf(0.0));
                    }
                    if(ant.dipolelengthA > ant.dipolelengthmaxA){
                        ant.dipolelengthA=ant.dipolelengthmaxA;
                        text1.setText(String.valueOf(ant.dipolelengthmaxA));
                    }
                    if(ant.dipolelengthB < 0.0){
                        ant.dipolelengthB=0.0;
                        text2.setText(String.valueOf(0.0));
                    }
                    if(ant.dipolelengthB > ant.dipolelengthmaxB){
                        ant.dipolelengthB=ant.dipolelengthmaxB;
                        text2.setText(String.valueOf(ant.dipolelengthmaxB));
                    }
                    if(ant.distancia < 0.0){
                        ant.distancia=0.0;
                        text4.setText(String.valueOf(ant.distancia));
                    }
                    if(ant.distancia>1.0){
                        ant.distancia=1.0;
                        text4.setText(String.valueOf(ant.distancia));
                    }

                    slider1.setValue((int)(ScrollMax*ant.dipolelengthA/ant.dipolelengthmaxA));
                    slider2.setValue((int)(ScrollMax*ant.dipolelengthB/ant.dipolelengthmaxB));

                    slider3.setValue((int)(ScrollFaseMax*ant.fase*0.5/Math.PI)+180);
                    slider4.setValue((int)(ScrollMax2*ant.distancia/ant.distanciamax));

                    //  Remove to make update button pushing mandatory for refresh
                    ant.dcan.redraw(); //draw the dipole
                    ant.setThetaPlaneMaxima();
                    ant.UpdateRadiation();
                    ant.rcan1.redraw();//draw the radar canvas
                    ant.rcan2.redraw();
                    ant.outpanel.repaint();
                    //-------------------------------------------------------------

                    if(ant.dipolelengthA==0.0) ant.dipolelengthA=1.0e-10;
                    if(ant.dipolelengthB==0.0) ant.dipolelengthB=1.0e-10;
            }   
    	}
	

	public void itemStateChanged(ItemEvent evt){
	    fase=(double)(2*Math.PI*(slider3.getValue()-180)/(ScrollFaseMax-1));
	    if(evt.getSource()==c2){
		text3.setText(String.valueOf(MaestroA.rounder(fase,3)));
	    }	
	    else if(evt.getSource()==c1){
		text3.setText(String.valueOf(MaestroA.rounder(fase*180/Math.PI,2)));
	    }
	}


        public void actionPerformed(ActionEvent evt){
            if(evt.getSource()==b2a || evt.getSource()==b2b || evt.getSource()==b3a ||
               evt.getSource()==b3b || evt.getSource()==b4a || evt.getSource()==b4b ||
               evt.getSource()==b5a || evt.getSource()==b5b)
            {
                if(evt.getSource()==b2a){
                    slider1.setValue(slider1.getValue()-1);
                }
                else if(evt.getSource()==b2b){
                    slider1.setValue(slider1.getValue()+1);
                }
                else if(evt.getSource()==b3a){
                    slider2.setValue(slider2.getValue()-1);
                }
                else if(evt.getSource()==b3b){
                    slider2.setValue(slider2.getValue()+1);
                }
                else if(evt.getSource()==b4a){
                    slider3.setValue(slider3.getValue()-1);
                }
                else if(evt.getSource()==b4b){
                    slider3.setValue(slider3.getValue()+1);
                }
                else if(evt.getSource()==b5a){
                    slider4.setValue(slider4.getValue()-1);
                }
                else if(evt.getSource()==b5b){
                    slider4.setValue(slider4.getValue()+1);
                }
                
                double dipolelengthA, dipolelengthB;
                double fase=ant.fase, distancia;

                if((double)ant.dipolelengthmaxA*slider1.getValue()/(double)(ScrollMax-1)>ant.dipolelengthmaxA){
                    dipolelengthA=ant.dipolelengthmaxA;
                    slider1.setValue(ScrollMax-1);
                }
                else{
                    dipolelengthA=(double)ant.dipolelengthmaxA*slider1.getValue()/(double)(ScrollMax-1);
                }

                if((double)ant.dipolelengthmaxB*slider2.getValue()/(double)(ScrollMax-1)>ant.dipolelengthmaxB){
                    dipolelengthB=ant.dipolelengthmaxB;
                    slider2.setValue(ScrollMax-1);
                }
                else{
                    dipolelengthB=(double)ant.dipolelengthmaxB*slider2.getValue()/(double)(ScrollMax-1);
                }

                if((double)ant.distanciamax*slider4.getValue()/(double)(ScrollMax2-1)>ant.distanciamax){
                    distancia = ant.distanciamax;
                    slider4.setValue(ScrollMax2-1);
                }
                else{
                    distancia=(double)ant.distanciamax*slider4.getValue()/(double)(ScrollMax2-1);
                }

                if(dipolelengthA==0.0) dipolelengthA=1.0e-10;
                if(dipolelengthB==0.0) dipolelengthB=1.0e-10;

                text1.setText(String.valueOf(MaestroA.rounder(dipolelengthA,4)));
                text2.setText(String.valueOf(MaestroA.rounder(dipolelengthB,4)));

                if(c2.getState()){
                    fase=(double)((slider3.getValue()-180)*2*Math.PI/(ScrollFaseMax-1));
                    text3.setText(String.valueOf(MaestroA.rounder(fase,3)));
                    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue());
                }
                else if(c1.getState()){
                    fase=(double)(2*Math.PI*(slider3.getValue()-180)/(ScrollFaseMax-1));
                    text3.setText(String.valueOf(MaestroA.rounder(fase*180/Math.PI,2)));
                    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue()*Math.PI/180);
                }

                text4.setText(String.valueOf(MaestroA.rounder(distancia,4)));

                //=====================================

                ant.dipolelengthA=Double.valueOf(text1.getText()).doubleValue();
                ant.dipolecurrentA=Double.valueOf(text5.getText()).doubleValue();
                ant.dipolelengthB=Double.valueOf(text2.getText()).doubleValue();
                ant.dipolecurrentB=Double.valueOf(text6.getText()).doubleValue();

                ant.distancia=Double.valueOf(text4.getText()).doubleValue();
                if(ant.dipolelengthA < 0.0){
                    ant.dipolelengthA=0.0;
                    text1.setText(String.valueOf(0.0));
                }
                if(ant.dipolelengthA > ant.dipolelengthmaxA){
                    ant.dipolelengthA=ant.dipolelengthmaxA;
                    text1.setText(String.valueOf(ant.dipolelengthmaxA));
                }
                if(ant.dipolelengthB < 0.0){
                    ant.dipolelengthB=0.0;
                    text2.setText(String.valueOf(0.0));
                }
                if(ant.dipolelengthB > ant.dipolelengthmaxB){
                    ant.dipolelengthB=ant.dipolelengthmaxB;
                    text2.setText(String.valueOf(ant.dipolelengthmaxB));
                }
                if(ant.distancia < 0.0){
                    ant.distancia=0.0;
                    text4.setText(String.valueOf(ant.distancia));
                }
                if(ant.distancia>1.0){
                    ant.distancia=1.0;
                    text4.setText(String.valueOf(ant.distancia));
                }

                slider1.setValue((int)(ScrollMax*ant.dipolelengthA/ant.dipolelengthmaxA));
                slider2.setValue((int)(ScrollMax*ant.dipolelengthB/ant.dipolelengthmaxB));

                slider3.setValue((int)(ScrollFaseMax*ant.fase*0.5/Math.PI)+180);
                slider4.setValue((int)(ScrollMax2*ant.distancia/ant.distanciamax));

                //  Remove to make update button pushing mandatory for refresh
                ant.dcan.redraw(); //draw the dipole
                ant.setThetaPlaneMaxima();
                ant.UpdateRadiation();
                ant.rcan1.redraw();//draw the radar canvas
                ant.rcan2.redraw();
                ant.outpanel.repaint();
                //-------------------------------------------------------------

                if(ant.dipolelengthA==0.0) ant.dipolelengthA=1.0e-10;
                if(ant.dipolelengthB==0.0) ant.dipolelengthB=1.0e-10;

            }
            
            if(evt.getSource()==button1){
               
		ant.dipolelengthA=Double.valueOf(text1.getText()).doubleValue();
		ant.dipolecurrentA=Double.valueOf(text5.getText()).doubleValue();
		ant.dipolelengthB=Double.valueOf(text2.getText()).doubleValue();
		ant.dipolecurrentB=Double.valueOf(text6.getText()).doubleValue();
		if(c2.getState()){
		    //rad
		    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue());
		    if(ant.fase > Math.PI){
			ant.fase = Math.PI;
			text3.setText(String.valueOf(MaestroA.rounder(ant.fase,4)));
		    }
		    else if(ant.fase < -Math.PI){
			ant.fase = - Math.PI;
			text3.setText(String.valueOf(MaestroA.rounder(ant.fase,4)));
		    }
		}
		else{ 
		    //degree
		    ant.fase=(double)(Double.valueOf(text3.getText()).doubleValue()*Math.PI/180);
		    if(ant.fase > Math.PI){
			ant.fase = Math.PI;
			text3.setText(String.valueOf(MaestroA.rounder(ant.fase*180/Math.PI,2)));
		    }
		    else if(ant.fase < -Math.PI){
			ant.fase = - Math.PI;
			text3.setText(String.valueOf(MaestroA.rounder(ant.fase*180/Math.PI,2)));
		    }
		}
		ant.distancia=Double.valueOf(text4.getText()).doubleValue();
		if(ant.dipolelengthA < 0.0){
		    ant.dipolelengthA=0.0;
		    text1.setText(String.valueOf(0.0));
		}
		if(ant.dipolelengthA > ant.dipolelengthmaxA){
		    ant.dipolelengthA=ant.dipolelengthmaxA;
		    text1.setText(String.valueOf(ant.dipolelengthmaxA));
		}
		if(ant.dipolelengthB < 0.0){
		    ant.dipolelengthB=0.0;
		    text2.setText(String.valueOf(0.0));
		}
		if(ant.dipolelengthB > ant.dipolelengthmaxB){
		    ant.dipolelengthB=ant.dipolelengthmaxB;
		    text2.setText(String.valueOf(ant.dipolelengthmaxB));
		}
		if(ant.distancia < 0.0){
		    ant.distancia=0.0;
		    text4.setText(String.valueOf(ant.distancia));
		}
		if(ant.distancia>1.0){
		    ant.distancia=1.0;
		    text4.setText(String.valueOf(ant.distancia));
		}

		slider1.setValue((int)(ScrollMax*ant.dipolelengthA/ant.dipolelengthmaxA));
		slider2.setValue((int)(ScrollMax*ant.dipolelengthB/ant.dipolelengthmaxB));
		
		slider3.setValue((int)(ScrollFaseMax*ant.fase*0.5/Math.PI)+180);
		slider4.setValue((int)(ScrollMax2*ant.distancia/ant.distanciamax));
		ant.dcan.redraw(); //draw the dipole
		ant.setThetaPlaneMaxima();
		ant.UpdateRadiation();
		ant.rcan1.redraw();//draw the radar canvas
		ant.rcan2.redraw();
		
		ant.outpanel.repaint();
		if(ant.dipolelengthA==0.0) ant.dipolelengthA=1.0e-10;
		if(ant.dipolelengthB==0.0) ant.dipolelengthB=1.0e-10;
                
           }
	}

}//End of SlidePanel
