//SlidePanel.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;


public class SlidePanel extends Panel implements ActionListener, AdjustmentListener{

    private Image im;
    private Graphics buf;

	DipoleAnt ant;
	Scrollbar slider1, slider2;
	TextField text1, text2;
	Button button1;
	Rectangle r;
	final int ScrollMin=0;
        int xshift = 20;
        
	//public int ScrollMax=20001;
	
        SlidePanel(DipoleAnt ant){
		super();
		this.ant=ant;
		Color bgcolor = new Color(236,236,221);
		setBackground(bgcolor);

		setLayout(null);
		text1=new TextField("0.5",8);	
		text2=new TextField("1.0",8);	
		slider1=new Scrollbar(Scrollbar.HORIZONTAL,500,1,ScrollMin,ant.ScrollMax);
		slider2=new Scrollbar(Scrollbar.HORIZONTAL,10,1,1,21);
		
                button1=new Button("Update");

		text1.setBounds(175+xshift,17,60,25);
                Panel pt1 = new Panel();
		    pt1.setBackground(Color.black);
		    pt1.setBounds(174+xshift,16,62,27);
		text2.setBounds(5+xshift,115,70,25);
                Panel pt2 = new Panel();
		    pt2.setBackground(Color.black);
		    pt2.setBounds(4+xshift,114,72,27);
		slider1.setBounds(7+xshift-10,22,170,15);
		slider2.setBounds(7+xshift-10,67,170,15);
		slider1.setBackground(Color.white);
                slider2.setBackground(Color.white);
                button1.setBounds(173+xshift,110,70,30);
                button1.setBackground(Color.white);
                Panel pbut = new Panel();
		    pbut.setBackground(Color.black);
		    pbut.setBounds(172+xshift,109,72,32);
                
		add(text1); add(pt1);
		add(text2); add(pt2);
		add(slider1);
                add(slider2);
		add(button1);
                add(pbut);
		
		button1.addActionListener(this);
		slider1.addAdjustmentListener(this);
		slider2.addAdjustmentListener(this);
                
		Panel pslider2 = new Panel();
		    pslider2.setBackground(Color.cyan);
		    add(pslider2);
		    pslider2.setBounds(6+xshift-10,21,172,17);
		Panel pslider1 = new Panel();
		    pslider1.setBackground(Color.black);
		    add(pslider1);
		    pslider1.setBounds(5+xshift-10,20,174,19);
                
                Panel pslider3 = new Panel();
		    pslider3.setBackground(Color.cyan);
		    add(pslider3);
		    pslider3.setBounds(6+xshift-10,66,172,17);
		Panel pslider4 = new Panel();
		    pslider4.setBackground(Color.black);
		    add(pslider4);
		    pslider4.setBounds(5+xshift-10,65,174,19);
        }

    
    public void paint(Graphics g){

	if(im == null){
	    im = createImage(getSize().width,getSize().height);
	    buf = im.getGraphics();
	}
        drawScrn(buf);
    	g.drawImage(im,0,0,null);	    
    }
    

    private void drawScrn(Graphics g){
        r=getBounds();
		
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g.clearRect(0,0,getSize().width,getSize().height);
        
        g.setFont(new Font("Sanserif",Font.PLAIN,12));
	
        g.setColor(Color.red.darker());
        g.drawString("Dipole Length (wavelengths)",-5+xshift,15);
        g.drawString("Maximum Length (wavelengths)",-5+xshift,60);
        
        g.setColor(Color.red.darker());
        g.drawString("Maximum Current (Amps)",-5+xshift,108);
        g.setColor(Color.black);
        g.draw3DRect(0,0,r.width-1,r.height-1,false);
        g.setFont(new Font("Serif",Font.BOLD,16));
        g.drawString("\u03bb",240+xshift,38);
        g.setColor(Color.black);
        g.setFont(new Font("Serif",Font.BOLD,16));
        g.drawString(slider2.getValue()+" \u03bb",180+xshift,82);
        
    }

	
	public void OLDpaint(Graphics g){
		r=getBounds();
		
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.clearRect(0,0,getSize().width,getSize().height);
                
		g.setFont(new Font("Sanserif",Font.PLAIN,12));
		
		g.setColor(Color.red.darker());
		g.drawString("Dipole Length (wavelengths)",-5+xshift,15);
                g.drawString("Maximum Length (wavelengths)",-5+xshift,60);
                
		g.setColor(Color.red.darker());
		g.drawString("Maximum Current (Amps)",-5+xshift,108);
		g.setColor(Color.black);
		g.draw3DRect(0,0,r.width-1,r.height-1,false);
		g.setFont(new Font("Serif",Font.BOLD,16));
		g.drawString("\u03bb",240+xshift,38);
                g.setColor(Color.black);
                g.setFont(new Font("Serif",Font.BOLD,16));
                g.drawString(slider2.getValue()+" \u03bb",180+xshift,82);
                
	}
	
	
	public void actionPerformed(ActionEvent evt){
		ant.dipolelength=Float.valueOf(text1.getText()).floatValue();
		if(Float.valueOf(text2.getText()).floatValue() > 1.0e17){
		    ant.dipolecurrent = 1.0e17f;
		    text2.setText(String.valueOf(1.0E17));
		}
		else{
		    ant.dipolecurrent=Float.valueOf(text2.getText()).floatValue();
		}
		if(ant.dipolelength < 0.0f){
			 ant.dipolelength=0.0f;
		         text1.setText(String.valueOf(0.0));
		}
		if(ant.dipolelength > ant.dipolelengthmax){
			 ant.dipolelength=ant.dipolelengthmax;
		         text1.setText(String.valueOf(ant.dipolelengthmax));
		}
		slider1.setValue((int)(ant.ScrollMax*ant.dipolelength/ant.dipolelengthmax));
		ant.dcan.redraw(); //draw the dipole
		ant.UpdateRadiation();
		ant.rcan1.redraw();//draw the radar canvas
		ant.rcan2.redraw();
		
		ant.outpanel.repaint();
		//if(ant.dipolelength==0.0) ant.dipolelength=1.0e-10f;
	}
	
	
	public void adjustmentValueChanged(AdjustmentEvent evt){
	    //if((float)ant.dipolelengthmax*slider1.getValue()/(float)(ScrollMax-1)>ant.dipolelengthmax){
	    
            if(evt.getSource()==slider1){
                {
                    ant.dipolelength=(float)ant.dipolelengthmax*slider1.getValue()/(float)(ant.ScrollMax-1);
                }


                //if(ant.dipolelength==0.0) ant.dipolelength=1.0e-10f;
                    text1.setText(String.valueOf(ant.dipolelength));
                    ant.dcan.redraw();//draw the dipole
                    ant.UpdateRadiation();
                    ant.rcan1.redraw();//draw the radar canvas
                    ant.rcan2.redraw();

                    ant.outpanel.repaint();
            }
            if(evt.getSource()==slider2){
                ant.dipolelengthmax = slider2.getValue();
                ant.dipolelength = 0.5f;
                ant.ScrollMax = (slider2.getValue()*1000) + 1;
                slider1.setMaximum(ant.ScrollMax);
                //slider1.setValue((int)(ant.ScrollMax*ant.dipolelength/ant.dipolelengthmax));
                slider1.setValue(500);
                repaint();
                text1.setText(String.valueOf(ant.dipolelength));
                    ant.dcan.redraw();//draw the dipole
                    ant.UpdateRadiation();
                    ant.rcan1.redraw();//draw the radar canvas
                    ant.rcan2.redraw();

                    ant.outpanel.repaint();
            }
	}

} //End of SlidePanel
