import java.awt.*;
import java.awt.event.*;

public class ParabolicReflector extends Frame implements ActionListener, AdjustmentListener, ItemListener, MouseListener, WindowListener{
    //About about;
    Instructions instructions;
    Panel instrFrame;
    TitlePanel titlepanel;
    NewGuideCanvas rwc;
    NewGuideCanvas2 rwc2;
    NewGuideInputPanel inputpanel;
    NewGuideInputPanel2 inputpanel2;
    NewGuide_State state;
    SetRangePanel a_panel, frequency_panel, distance_panel;
    NewGuideControlPanel rwconp;
    
    NewGuideOutputPanel outputpanel;
    
    NewGuideGraphCanvasDir rwcca;
    NewGuideGraphCanvasF rwccf;
    NewGuideGraphCanvasG rwccg;
    ControlPanel3D cp3D;
    
     /**
     * Initializes the applet.  You never need to call this directly; it is
     * called automatically by the system once the applet is created.
     */
    
    public static void main(String[] args){
        ParabolicReflector f = new ParabolicReflector();
        int xmove = 20;
        int ymove = 41;
        f.setSize(930+xmove,573+ymove);
        f.setVisible(true);
        f.setLayout(null);
    }
    
    public ParabolicReflector() {
	setLayout(null);
	
        int xmove = 10;
        int ymove = 34;
        
        titlepanel = new TitlePanel("Module 9.4","Large Parabolic Reflector ( d \u2265 \u03bb )");
	//add(titlepanel);
	titlepanel.setBounds(10+xmove,9+ymove,300,41);
        
        //about = new About();
        //about.setBounds(9,9,912,552);
	//about.setVisible(false);
        instructions = new Instructions();
        //instructions.setBounds(9,9,912,552);
        instructions.setBounds(11+xmove,261+ymove,908,298);
	//instructions.setVisible(false);
        instrFrame = new Panel();
        instrFrame.setBackground(Color.black);
        instrFrame.setBounds(10+xmove,260+ymove,910,300);
        //instrFrame.setVisible(false);
        
	state = new NewGuide_State();
	
	rwc = new NewGuideCanvas();
	//add(rwc);
	rwc.setBounds(317+xmove,12+ymove,296,241);
	rwc.setVisible(true);
	
	rwc2 = new NewGuideCanvas2();
	//add(rwc2);
	rwc2.setBounds(317+xmove,56+ymove,297,198);
	rwc2.setVisible(false);
    
	inputpanel = new NewGuideInputPanel(state);
	//add(inputpanel);
	inputpanel.setBounds(12+xmove,262+ymove,296,296);
	inputpanel.setVisible(true);
	
	inputpanel2 = new NewGuideInputPanel2(state);
	//add(inputpanel2);
	inputpanel2.setBounds(12+xmove,262+ymove,296,296);
	inputpanel2.setVisible(true);
	
	outputpanel = new NewGuideOutputPanel(state);
	//add(outputpanel);
	outputpanel.setBounds(317+xmove,307+ymove,296,251);
	outputpanel.setVisible(true);
	
	frequency_panel = new SetRangePanel("Frequency","[ Hz ]");
	//add(frequency_panel);
	frequency_panel.setBounds(12+xmove,262+ymove,296,296);
	frequency_panel.setVisible(false);
	frequency_panel.setMinimum(state.frequency_minimum);
	frequency_panel.setMaximum(state.frequency_maximum);
	frequency_panel.setBackg(new Color(236,236,221));
	
	a_panel = new SetRangePanel("Antenna Length L ","");
	//add(a_panel);
	a_panel.setBounds(12+xmove,262+ymove,296,296);
	a_panel.setVisible(false);
	a_panel.setMinimum(state.a_minimum);
	a_panel.setMaximum(state.a_maximum);
	a_panel.setBackg(new Color(236,236,221));
	
	distance_panel = new SetRangePanel("Radial Distance r ","");
	//add(distance_panel);
	distance_panel.setBounds(12+xmove,262+ymove,296,296);
	distance_panel.setVisible(false);
	distance_panel.setMinimum(state.distance_minimum);
	distance_panel.setMaximum(state.distance_maximum);
	distance_panel.setBackg(new Color(236,236,221));
		
	rwconp = new NewGuideControlPanel();
	//add(rwconp);
	rwconp.setBounds(317+xmove,262+ymove,296,36);
	
	cp3D = new ControlPanel3D(state);
	//add(cp3D);
	cp3D.setVisible(false);
	cp3D.setBounds(317+xmove,57+ymove,296,196);
	
	rwcca = new NewGuideGraphCanvasDir(state);
	//add(rwcca);
	rwcca.setVisible(true);
	rwcca.setBounds(12+xmove,57+ymove,296,196);
	
	rwccf = new NewGuideGraphCanvasF(state);
	//add(rwccf);
	rwccf.setVisible(true);
	rwccf.setBounds(622+xmove,12+ymove,296,241);
	
	rwccg = new NewGuideGraphCanvasG(state);
	//add(rwccg);
	rwccg.setVisible(true);
	rwccg.setBounds(622+xmove,262+ymove,296,296);
	
	Panel p01 = new Panel();
	    p01.setBackground(Color.white);
	    //add(p01);
	    p01.setBounds(316+xmove,11+ymove,298,243);
	
	Panel p1 = new Panel();
	    p1.setBackground(Color.black);
	    //add(p1);
	    p1.setBounds(315+xmove,10+ymove,300,245);
	    
        // bakpanel for upper field plot
        Panel p3A = new Panel();
	    p3A.setBackground(Color.white);
	    //add(p3A);
	    p3A.setBounds(621+xmove,11+ymove,298,243);
    
	Panel p3B = new Panel();
	    p3B.setBackground(Color.black);
	    //add(p3B);
	    p3B.setBounds(620+xmove,10+ymove,300,245);
            
	//backpanels for lower field plot
	Panel p30 = new Panel();
	    p30.setBackground(Color.white);
	    //add(p30);
	    p30.setBounds(621+xmove,261+ymove,298,298);
    
	Panel p3 = new Panel();
	    p3.setBackground(Color.black);
	    //add(p3);
	    p3.setBounds(620+xmove,260+ymove,300,300);
	    
        //backpanel for outputpanel    
        Panel p20 = new Panel();
	    p20.setBackground(Color.white);
	    //add(p20);
	    p20.setBounds(316+xmove,306+ymove,298,253);
    
	Panel p2 = new Panel();
	    p2.setBackground(Color.black);
	    //add(p2);
	    p2.setBounds(315+xmove,305+ymove,300,255);
	    
	//backpanels for control panel
	Panel p40 = new Panel();
	    p40.setBackground(Color.white);
	    //add(p40);
	    p40.setBounds(316+xmove,261+ymove,298,38);
    
	Panel p4 = new Panel();
	    p4.setBackground(Color.black);
	    //add(p4);
	    p4.setBounds(315+xmove,260+ymove,300,40);
	
	//backpanels for inputpanel        
	Panel p50 = new Panel();
	    p50.setBackground(Color.white);
	    //add(p50);
	    p50.setBounds(11+xmove,261+ymove,298,298);
    
	Panel p5 = new Panel();
	    p5.setBackground(Color.black);
	    //add(p5);
	    p5.setBounds(10+xmove,260+ymove,300,300);
	//backpanel for cp3D  
	Panel p60 = new Panel();
	    p60.setBackground(Color.white);
	    //add(p60);
	    p60.setBounds(11+xmove,56+ymove,298,198);
	Panel p6 = new Panel();
	    p6.setBackground(Color.black);
	    //add(p6);
	    p6.setBounds(10+xmove,55+ymove,300,200);
	

	//backpanels for applet
	Panel p0 = new Panel();
	    p0.setBackground(Color.cyan);
	    //add(p0);
	    p0.setBounds(3+xmove,3+ymove,924,564);
	    
	Panel p00 = new Panel();
	    p00.setBackground(Color.black);
	    //add(p00);
	    p00.setBounds(xmove,ymove,930,570);
    
            //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(instrFrame);
	    add(titlepanel);
	    add(rwc);
	    add(rwc2);
	    add(inputpanel);
	    add(inputpanel2);
	    add(outputpanel);
	    add(frequency_panel);
	    add(a_panel);
	    add(distance_panel);
	    add(rwconp);
	    add(cp3D);
	    add(rwcca);
	    add(rwccf);
            add(rwccg);
	    add(p01);
	    add(p1);
	    add(p20);
	    add(p2);
            add(p30);
	    add(p3);
            add(p3A);
            add(p3B);
	    add(p40);
	    add(p4);
	    add(p50);
	    add(p5);
	    add(p60);
	    add(p6);
	    add(p0);
	    add(p00);
	    
	    state.LicenseExpired = false; 	
            //}

    
	//Listeners
        this.addWindowListener(this);
        instructions.bupdate.addActionListener(this);
	
        rwconp.about.addActionListener(this);
        rwconp.c1.addItemListener(this);
	rwconp.c2.addItemListener(this);
        rwconp.cdB.addItemListener(this);
        
	inputpanel.b1.addActionListener(this);
	inputpanel.b2.addActionListener(this);
	inputpanel.b3.addActionListener(this);
        
        // CHANGE ==============================================================
        inputpanel.b2a.addActionListener(this);
        inputpanel.b2b.addActionListener(this);
        inputpanel.b3a.addActionListener(this);
        inputpanel.b3b.addActionListener(this);
        inputpanel.b4a.addActionListener(this);
        inputpanel.b4b.addActionListener(this);
        inputpanel.b5a.addActionListener(this);
        inputpanel.b5b.addActionListener(this);
        inputpanel.b6a.addActionListener(this);
        inputpanel.b6b.addActionListener(this);
        //======================================================================
	
	inputpanel.slider1.addAdjustmentListener(this);
	inputpanel.slider2.addAdjustmentListener(this);
	inputpanel.slider3.addAdjustmentListener(this);
	inputpanel.sliderT.addAdjustmentListener(this);
	inputpanel.sliderT0.addAdjustmentListener(this);
        
	inputpanel.bupdate.addActionListener(this);
	inputpanel2.bupdate.addActionListener(this);
	
	a_panel.b1.addActionListener(this);
	frequency_panel.b1.addActionListener(this);
	distance_panel.b1.addActionListener(this);
	
	state.ignition();
        state.scanField();
        state.scan_coefficients();
        
        rwcca.setYRangeMax(1.0);
        rwcca.setYRangeMin(0.0);
        rwcca.plotZeroLine(false);
        rwcca.plotRefPoint(true);
        rwcca.setLabels("Antenna illumination","","","");
        rwcca.setRefPoint(state.a);
        rwcca.plot(state.leng,state.DVector, state.IsDirectivity);
        rwcca.ignition();
        rwcca.repaint();

        rwc.setFrequency(state.frequency);
        rwc.setEpsilon(state.epsilon_r);
        rwc.setAntennaLength(state.DipoleLength_lambda);
        rwc.setAntennaRadius(state.wire_radius);

        rwc2.setFrequency(state.frequency);
        rwc2.setEpsilon(state.epsilon_r);
        rwc2.setWavelength(state.wavelength);
        rwc2.setTheta(state.theta_angle);
        rwc2.setPhi(state.phi_angle);
        rwc2.setDistance(state.point_distance);

        rwc.repaint();
        rwc2.repaint();

        outputpanel.rwocA.repaint();
	    
            state.ignition();
            state.scanField();
            state.scan_coefficients();

            if(!state.Is_dB){
                rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM));
                rwccf.setYRangeMin(0.0);
                rwccf.setdB(false);
            }
            else{
                rwccf.setYRangeMax(0.0);
                rwccf.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                //rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                rwccf.setdB(true);
            }
            rwccf.plotZeroLine(false);
            rwccf.plotRefPoint(false);
            rwccf.setPolar(false);
            rwccf.setPhiAngle(0.0);
            rwccf.setType(1);
            rwccf.setLabels("","","\u03b8  [degrees]");

            state.Is_theta = true;
            state.Is_radius = false;

            if(!state.Is_dB){rwccf.plot(state.x_polar,state.ethetaM);}
            else{rwccf.plot(state.x_polar,state.ethetaM_dB);}
            rwccf.ignition();
            rwccf.repaint();

            if(!state.Is_dB){
                rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM));
                rwccg.setYRangeMin(0.0);
                rwccg.setdB(false);
            }
            else{
                rwccg.setYRangeMax(0.0);
                rwccg.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                //rwccg.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                rwccg.setdB(true);
            }
            
            rwccg.plotZeroLine(false);
            rwccg.plotRefPoint(false);
            rwccg.setPolar(true);
            rwccg.setPhiAngle(0.0);
            rwccg.setType(1);
            rwccg.setLabels("","","\u03b8  [degrees]");

            state.Is_theta = true;
            state.Is_radius = false;

            if(!state.Is_dB){rwccg.plot(state.x_polar,state.ethetaM);}
            else{rwccg.plot(state.x_polar,state.ethetaM_dB);}
            rwccg.ignition();
            rwccg.repaint();
	
    }
    /*
    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);
	} 
    } 
    */
    
    @Override
    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,626,595);  	
	/*	
	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,587);
        //g.setFont(fonto);
        //g.drawString("Amanogawa, "+annonuovo+" - All Rights Reserved",30,585);
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	
        if(evt.getSource()==inputpanel.sliderT || evt.getSource()==inputpanel.sliderT0){
                    rwccf.setRefPoint(state.theta_angle);
                    //rwccf.ignition();
		    rwccf.repaint();
                    //rwccg.ignition();
		    rwccg.repaint();
                    outputpanel.rwocA.repaint();
            
        }
	
	if(evt.getSource()==inputpanel.slider1 || evt.getSource()==inputpanel.slider2 
                            || evt.getSource()==inputpanel.slider3){
	    
            rwconp.c1.setEnabled(false);
            rwconp.c2.setEnabled(false);
            //state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	    
            //outputpanel.rwocA.repaint();
            
            /*
	    rwcca.setYRangeMax(1.0);
	    rwcca.setYRangeMin(0.0);
	    rwcca.plotZeroLine(false);
	    rwcca.plotRefPoint(true);
	    rwcca.setLabels("Antenna illumination","","x","");
	    rwcca.setRefPoint(state.a);
	    rwcca.plot(state.leng,state.DVector, state.IsDirectivity);
	    rwcca.ignition();
	    rwcca.repaint();
		    
	    rwc.setFrequency(state.frequency);
	    rwc.setEpsilon(state.epsilon_r);
	    rwc.setAntennaLength(state.DipoleLength_lambda);
	    rwc.setAntennaRadius(state.wire_radius);
	    
	    rwc2.setFrequency(state.frequency);
	    rwc2.setEpsilon(state.epsilon_r);
	    rwc2.setWavelength(state.wavelength);
	    rwc2.setTheta(state.theta_angle);
	    rwc2.setPhi(state.phi_angle);
	    rwc2.setDistance(state.point_distance);
	    
	    rwc.repaint();
	    rwc2.repaint();
	    
	    outputpanel.rwocA.repaint();
	    
                    state.ignition();
                    state.scanField();
                    state.scan_coefficients();
		    
		    rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM));
		    rwccf.setYRangeMin(0.0);
		    rwccf.plotZeroLine(false);
		    rwccf.plotRefPoint(false);
		    rwccf.setPolar(false);
		    rwccf.setPhiAngle(0.0);
		    rwccf.setType(1);
		    rwccf.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
		    rwccf.plot(state.x_polar,state.ethetaM);
		    rwccf.ignition();
		    rwccf.repaint();
                    
                    
                    rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM));
		    rwccg.setYRangeMin(0.0);
		    rwccg.plotZeroLine(false);
		    rwccg.plotRefPoint(false);
		    rwccg.setPolar(true);
		    rwccg.setPhiAngle(0.0);
		    rwccg.setType(1);
		    rwccg.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
		    rwccg.plot(state.x_polar,state.ethetaM);
		    rwccg.ignition();
		    rwccg.repaint();
              */      
    	}
    }
    
    public void mouseClicked(MouseEvent evt){/*myEventHandler(evt);*/}
    public void mouseEntered(MouseEvent evt){ }
    public void mouseExited(MouseEvent evt) { }
    //public void mousePressed(MouseEvent evt) {rwcc.repaint();}
    public void mouseReleased(MouseEvent evt) { }
    public void mousePressed(MouseEvent evt) {
	
    }
    
    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()==inputpanel.b2a || evt.getSource()==inputpanel.b2b ||
           evt.getSource()==inputpanel.b3a || evt.getSource()==inputpanel.b3b ||
           evt.getSource()==inputpanel.b4a || evt.getSource()==inputpanel.b4b )
        {
            rwconp.c1.setEnabled(false);
            rwconp.c2.setEnabled(false);
        }
        if(evt.getSource()==inputpanel.b5a || evt.getSource()==inputpanel.b5b ||
           evt.getSource()==inputpanel.b6a || evt.getSource()==inputpanel.b6b )
        {
            rwccf.setRefPoint(state.theta_angle);
                    //rwccf.ignition();
		    rwccf.repaint();
                    //rwccg.ignition();
		    rwccg.repaint();
                    outputpanel.rwocA.repaint();
        }
        
        
        //if(evt.getSource()==about.bupdate){
        if(evt.getSource()==instructions.bupdate){
            //about.setVisible(false);
            instructions.setVisible(false);
            instrFrame.setVisible(false);
        }
        
        if(evt.getSource()==rwconp.about){
            //about.setVisible(true);
            instructions.setVisible(true);
            instrFrame.setVisible(true);
        }
        
	if(evt.getSource()==inputpanel.bupdate){
	    rwconp.c1.setEnabled(true);
            rwconp.c2.setEnabled(true);
	    state.ignition();
	    state.scanField();
            state.scan_coefficients();
	    
            rwcca.setYRangeMax(1.0);
	    rwcca.setYRangeMin(0.0);
	    rwcca.plotZeroLine(false);
	    rwcca.plotRefPoint(true);
	    rwcca.setLabels("Antenna illumination","","","");
	    rwcca.setRefPoint(state.a);
	    rwcca.plot(state.leng,state.DVector, state.IsDirectivity);
	    rwcca.ignition();
	    rwcca.repaint();
	
		    
	    rwc.setFrequency(state.frequency);
	    rwc.setEpsilon(state.epsilon_r);
	    rwc.setAntennaLength(state.DipoleLength_lambda);
	    rwc.setAntennaRadius(state.wire_radius);
	    
	    rwc2.setFrequency(state.frequency);
	    rwc2.setEpsilon(state.epsilon_r);
	    rwc2.setWavelength(state.wavelength);
	    rwc2.setTheta(state.theta_angle);
	    rwc2.setPhi(state.phi_angle);
	    rwc2.setDistance(state.point_distance);
	    
	    rwc.repaint();
	    rwc2.repaint();
	   
	    outputpanel.rwocA.repaint();
	     
		    if(rwconp.c1.getState()){
                        if(!state.Is_dB){
                            rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM));
                            rwccf.setYRangeMin(0.0);
                            rwccf.setdB(false);
                        }
                        else{
                            rwccf.setYRangeMax(0.0);
                            rwccf.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccf.setdB(true);
                        }
                        rwccf.setType(1);
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){
                            rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM2));
                            rwccf.setYRangeMin(0.0);
                            rwccf.setdB(false);
                        }
                        else{
                            rwccf.setYRangeMax(0.0);
                            rwccf.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccf.setdB(true);
                        }
                        rwccf.setType(2);
                    } 
		    //rwccf.setYRangeMin(0.0);
		    rwccf.plotZeroLine(false);
		    rwccf.plotRefPoint(false);
		    rwccf.setPolar(false);
		    rwccf.setPhiAngle((int)state.phi_angle);
		    
		    rwccf.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
		    if(rwconp.c1.getState()){
                        if(!state.Is_dB){rwccf.plot(state.x_polar,state.ethetaM);}
                        else{rwccf.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){rwccf.plot(state.x_polar,state.ethetaM2);}
                        else{rwccf.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    
		    rwccf.ignition();
		    rwccf.repaint();
                    
                    if(rwconp.c1.getState()){
                        if(!state.Is_dB){
                            rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM));
                            rwccg.setYRangeMin(0.0);
                            rwccg.setdB(false);
                        }
                        else{
                            rwccg.setYRangeMax(0.0);
                            rwccg.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccg.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccg.setdB(true);
                        }
                        
                        rwccg.setType(1);
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){
                            rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM2));
                            rwccg.setYRangeMin(0.0);
                            rwccg.setdB(false);
                        }
                        else{
                            rwccg.setYRangeMax(0.0);
                            rwccg.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccg.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccg.setdB(true);
                        }
                        rwccg.setType(2);
                        
                    }
                    //rwccg.setYRangeMin(0.0);
		    rwccg.plotZeroLine(false);
		    rwccg.plotRefPoint(false);
		    rwccg.setPolar(true);
		    rwccg.setPhiAngle((int)state.phi_angle);
		    rwccg.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
                    if(rwconp.c1.getState()){
                        if(!state.Is_dB){rwccg.plot(state.x_polar,state.ethetaM);}
                        else{rwccg.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){rwccg.plot(state.x_polar,state.ethetaM2);}
                        else{rwccg.plot(state.x_polar,state.ethetaM_dB);}
                    }
		    rwccg.ignition();
		    rwccg.repaint();
        }
	
	else if(evt.getSource()==inputpanel.b1){
	    
	    a_panel.setUnit(true);
	    
		a_panel.setMinimum(state.a_minimum);
		a_panel.setMaximum(state.a_maximum);
	    
	    inputpanel2.setVisible(false);
	    a_panel.setVisible(true);
	    inputpanel.setVisible(false);
	    frequency_panel.setVisible(false);
	    distance_panel.setVisible(true);
	}

	else if(evt.getSource()==inputpanel.b2){
	    frequency_panel.setUnit(false);
	    
	    inputpanel2.setVisible(false);
	    a_panel.setVisible(false);
	    inputpanel.setVisible(false);
	    frequency_panel.setVisible(true);
	    distance_panel.setVisible(false);
	}
	
	else if(evt.getSource()==inputpanel.b3){
	    
	    inputpanel2.setVisible(false);
	    a_panel.setVisible(false);
	    inputpanel.setVisible(false);
	    frequency_panel.setVisible(false);
	    distance_panel.setVisible(false);
	}
	
	else if(evt.getSource()==a_panel.b1){
	    a_panel.setVisible(false);
	    inputpanel.setVisible(true);
	    frequency_panel.setVisible(false);
	    distance_panel.setVisible(false);
	  
		state.a_minimum = a_panel.getMinimum();
		state.a_maximum = a_panel.getMaximum();
	
		if(state.a >= state.a_minimum && state.a <= state.a_maximum){
		    inputpanel.slider1.setValue((int)(inputpanel.ScrollMax*(state.a-state.a_minimum)
						    /(state.a_maximum-state.a_minimum)));
		}
		else if(state.a < state.a_minimum){
		    state.a = state.a_minimum;
		    inputpanel.slider1.setValue(0);
		    inputpanel.text1.setText(""+state.a);
		}
	
		else if(state.a > state.a_maximum){
		    state.a = state.a_maximum;
		    inputpanel.slider1.setValue(inputpanel.ScrollMax);
		    inputpanel.text1.setText(""+state.a);
		}
	    
	    state.ignition();
            state.scanField();
            state.scan_coefficients();
	    
	    rwcca.setYRangeMax(1.0);
	    rwcca.setYRangeMin(0.0);
	    rwcca.plotZeroLine(false);
	    rwcca.plotRefPoint(true);
	    rwcca.setLabels("Antenna illumination","","","");
	    rwcca.setRefPoint(state.a);
	    rwcca.plot(state.leng,state.DVector, state.IsDirectivity);
	    rwcca.ignition();
	    rwcca.repaint();
	
	    rwc.setFrequency(state.frequency);
	    rwc.repaint();
	    rwc2.repaint();
	   
	    outputpanel.rwocA.repaint();
	    
	}
	else if(evt.getSource()==frequency_panel.b1){
	    a_panel.setVisible(false);
	    inputpanel.setVisible(true);
	    frequency_panel.setVisible(false);
	    distance_panel.setVisible(false);
	    
	    state.frequency_minimum = frequency_panel.getMinimum();
	    state.frequency_maximum = frequency_panel.getMaximum();
	    
	    if(state.frequency >= state.frequency_minimum && state.frequency <= state.frequency_maximum){
		inputpanel.slider2.setValue((int)(inputpanel.ScrollMax*(state.frequency-state.frequency_minimum)
						    /(state.frequency_maximum-state.frequency_minimum)));
	    }
	    else if(state.frequency < state.frequency_minimum){
		state.frequency = state.frequency_minimum;
		inputpanel.slider2.setValue(0);
		inputpanel.text4.setText(""+state.frequency);
	    }
	
	    else if(state.frequency > state.frequency_maximum){
		state.frequency = state.frequency_maximum;
		inputpanel.slider2.setValue(inputpanel.ScrollMax);
		inputpanel.text4.setText(""+state.frequency);
	    }
	    state.ignition();
            state.scanField();
            state.scan_coefficients();
	    
	    outputpanel.rwocA.repaint();
	    
	    rwc.setFrequency(state.frequency);
	    rwc.repaint(); 
	    rwc2.repaint();
	}
	
	else if(evt.getSource()==distance_panel.b1){
	    distance_panel.setVisible(false);
	    inputpanel2.setVisible(true);
	    a_panel.setVisible(false);
	    inputpanel.setVisible(false);
	    frequency_panel.setVisible(false);
	    
		state.distance_minimum = distance_panel.getMinimum();
		state.distance_maximum = distance_panel.getMaximum();
	
		if(state.point_distance >= state.distance_minimum && state.point_distance <= state.distance_maximum){
		    inputpanel2.slider2.setValue((int)(inputpanel2.ScrollMax*(state.point_distance-state.distance_minimum)
						    /(state.distance_maximum-state.distance_minimum)));
		}
		else if(state.point_distance < state.distance_minimum){
		    state.point_distance = state.distance_minimum;
		    inputpanel2.slider2.setValue(0);
		    inputpanel2.text4.setText(""+state.point_distance);
		}
	
		else if(state.point_distance > state.distance_maximum){
		    state.point_distance = state.distance_maximum;
		    inputpanel2.slider2.setValue(inputpanel.ScrollMax);
		    inputpanel2.text4.setText(""+state.point_distance);
		}
	    
	    state.ignition();
	    state.scanField();
            state.scan_coefficients();
        
	    rwc.setFrequency(state.frequency);
	    rwc.repaint();
	    rwc2.repaint();
	   
	    outputpanel.rwocA.repaint();    
	}
    }
    
    public void itemStateChanged(ItemEvent evt){
	ItemSelectable ie = evt.getItemSelectable();
	    
        if(evt.getSource()==rwconp.c1){
            
            rwconp.c1.setEnabled(true);
            rwconp.c2.setEnabled(true);
	    state.ignition();
	    state.scanField();
            state.scan_coefficients();
	    
            rwcca.setYRangeMax(1.0);
	    rwcca.setYRangeMin(0.0);
	    rwcca.plotZeroLine(false);
	    rwcca.plotRefPoint(true);
	    rwcca.setLabels("Antenna illumination","","","");
	    rwcca.setRefPoint(state.a);
	    rwcca.plot(state.leng,state.DVector, state.IsDirectivity);
	    rwcca.ignition();
	    rwcca.repaint();
	
		    
	    rwc.setFrequency(state.frequency);
	    rwc.setEpsilon(state.epsilon_r);
	    rwc.setAntennaLength(state.DipoleLength_lambda);
	    rwc.setAntennaRadius(state.wire_radius);
	    
	    rwc2.setFrequency(state.frequency);
	    rwc2.setEpsilon(state.epsilon_r);
	    rwc2.setWavelength(state.wavelength);
	    rwc2.setTheta(state.theta_angle);
	    rwc2.setPhi(state.phi_angle);
	    rwc2.setDistance(state.point_distance);
	    
	    rwc.repaint();
	    rwc2.repaint();
	   
	    outputpanel.rwocA.repaint();
	     
		    if(rwconp.c1.getState()){
                        if(!state.Is_dB){
                            rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM));
                            rwccf.setYRangeMin(0.0);
                            rwccf.setdB(false);
                        }
                        else{
                            rwccf.setYRangeMax(0.0);
                            rwccf.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccf.setdB(true);
                        }
                        rwccf.setType(1);
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){
                            rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM2));
                            rwccf.setYRangeMin(0.0);
                            rwccf.setdB(false);
                        }
                        else{
                            rwccf.setYRangeMax(0.0);
                            rwccf.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccf.setdB(true);
                        }
                        rwccf.setType(2);
                    } 
		    //rwccf.setYRangeMin(0.0);
		    rwccf.plotZeroLine(false);
		    rwccf.plotRefPoint(false);
		    rwccf.setPolar(false);
		    rwccf.setPhiAngle((int)state.phi_angle);
		    
		    rwccf.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
		    if(rwconp.c1.getState()){
                        if(!state.Is_dB){rwccf.plot(state.x_polar,state.ethetaM);}
                        else{rwccf.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){rwccf.plot(state.x_polar,state.ethetaM2);}
                        else{rwccf.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    
		    rwccf.ignition();
		    rwccf.repaint();
                    
                    if(rwconp.c1.getState()){
                        if(!state.Is_dB){
                            rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM));
                            rwccg.setYRangeMin(0.0);
                            rwccg.setdB(false);
                        }
                        else{
                            rwccg.setYRangeMax(0.0);
                            rwccg.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccg.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccg.setdB(true);
                        }
                        rwccg.setType(1);
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){
                            rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM2));
                            rwccg.setYRangeMin(0.0);
                            rwccg.setdB(false);
                        }
                        else{
                            rwccg.setYRangeMax(0.0);
                            rwccg.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccg.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccg.setdB(true);
                        }
                        rwccg.setType(2);
                    }
                    //rwccg.setYRangeMin(0.0);
		    rwccg.plotZeroLine(false);
		    rwccg.plotRefPoint(false);
		    rwccg.setPolar(true);
		    rwccg.setPhiAngle((int)state.phi_angle);
		    rwccg.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
                    if(rwconp.c1.getState()){
                        if(!state.Is_dB){rwccg.plot(state.x_polar,state.ethetaM);}
                        else{rwccg.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){rwccg.plot(state.x_polar,state.ethetaM2);}
                        else{rwccg.plot(state.x_polar,state.ethetaM_dB);}
                    }
		    rwccg.ignition();
		    rwccg.repaint();
        }
        else if(evt.getSource()==rwconp.c2){
            rwconp.c1.setEnabled(true);
            rwconp.c2.setEnabled(true);
	    state.ignition();
	    state.scanField();
            state.scan_coefficients();
	    
            rwcca.setYRangeMax(1.0);
	    rwcca.setYRangeMin(0.0);
	    rwcca.plotZeroLine(false);
	    rwcca.plotRefPoint(true);
	    rwcca.setLabels("Antenna illumination","","","");
	    rwcca.setRefPoint(state.a);
	    rwcca.plot(state.leng,state.DVector, state.IsDirectivity);
	    rwcca.ignition();
	    rwcca.repaint();
	
		    
	    rwc.setFrequency(state.frequency);
	    rwc.setEpsilon(state.epsilon_r);
	    rwc.setAntennaLength(state.DipoleLength_lambda);
	    rwc.setAntennaRadius(state.wire_radius);
	    
	    rwc2.setFrequency(state.frequency);
	    rwc2.setEpsilon(state.epsilon_r);
	    rwc2.setWavelength(state.wavelength);
	    rwc2.setTheta(state.theta_angle);
	    rwc2.setPhi(state.phi_angle);
	    rwc2.setDistance(state.point_distance);
	    
	    rwc.repaint();
	    rwc2.repaint();
	   
	    outputpanel.rwocA.repaint();
	     
		    if(rwconp.c1.getState()){
                        if(!state.Is_dB){
                            rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM));
                            rwccf.setYRangeMin(0.0);
                            rwccf.setdB(false);
                        }
                        else{
                            rwccf.setYRangeMax(0.0);
                            rwccf.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccf.setdB(true);
                        }
                        rwccf.setType(1);
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){
                            rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM2));
                            rwccf.setYRangeMin(0.0);
                            rwccf.setdB(false);
                        }
                        else{
                            rwccf.setYRangeMax(0.0);
                            rwccf.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccf.setdB(true);
                        }
                        rwccf.setType(2);
                    } 
		    //rwccf.setYRangeMin(0.0);
		    rwccf.plotZeroLine(false);
		    rwccf.plotRefPoint(false);
		    rwccf.setPolar(false);
		    rwccf.setPhiAngle((int)state.phi_angle);
		    
		    rwccf.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
		    if(rwconp.c1.getState()){
                        if(!state.Is_dB){rwccf.plot(state.x_polar,state.ethetaM);}
                        else{rwccf.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){rwccf.plot(state.x_polar,state.ethetaM2);}
                        else{rwccf.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    
		    rwccf.ignition();
		    rwccf.repaint();
                    
                    if(rwconp.c1.getState()){
                        if(!state.Is_dB){
                            rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM));
                            rwccg.setYRangeMin(0.0);
                            rwccg.setdB(false);
                        }
                        else{
                            rwccg.setYRangeMax(0.0);
                            rwccg.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccg.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccg.setdB(true);
                        }
                        rwccg.setType(1);
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){
                            rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM2));
                            rwccg.setYRangeMin(0.0);
                            rwccg.setdB(false);
                        }
                        else{
                            rwccg.setYRangeMax(0.0);
                            rwccg.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccg.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccg.setdB(true);
                        }
                        rwccg.setType(2);
                    }
                    //rwccg.setYRangeMin(0.0);
		    rwccg.plotZeroLine(false);
		    rwccg.plotRefPoint(false);
		    rwccg.setPolar(true);
		    rwccg.setPhiAngle((int)state.phi_angle);
		    rwccg.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
                    if(rwconp.c1.getState()){
                        if(!state.Is_dB){rwccg.plot(state.x_polar,state.ethetaM);}
                        else{rwccg.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){rwccg.plot(state.x_polar,state.ethetaM2);}
                        else{rwccg.plot(state.x_polar,state.ethetaM_dB);}
                    }
		    rwccg.ignition();
		    rwccg.repaint();
        }
        
        if(evt.getSource()==rwconp.cdB){
            if(rwconp.cdB.getState()){
                state.Is_dB = true;
            }
            else{
                state.Is_dB = false;
            }
            rwconp.c1.setEnabled(true);
            rwconp.c2.setEnabled(true);
	    state.ignition();
	    state.scanField();
            state.scan_coefficients();
	    
            rwcca.setYRangeMax(1.0);
	    rwcca.setYRangeMin(0.0);
	    rwcca.plotZeroLine(false);
	    rwcca.plotRefPoint(true);
	    rwcca.setLabels("Antenna illumination","","","");
	    rwcca.setRefPoint(state.a);
	    rwcca.plot(state.leng,state.DVector, state.IsDirectivity);
	    rwcca.ignition();
	    rwcca.repaint();
	
		    
	    rwc.setFrequency(state.frequency);
	    rwc.setEpsilon(state.epsilon_r);
	    rwc.setAntennaLength(state.DipoleLength_lambda);
	    rwc.setAntennaRadius(state.wire_radius);
	    
	    rwc2.setFrequency(state.frequency);
	    rwc2.setEpsilon(state.epsilon_r);
	    rwc2.setWavelength(state.wavelength);
	    rwc2.setTheta(state.theta_angle);
	    rwc2.setPhi(state.phi_angle);
	    rwc2.setDistance(state.point_distance);
	    
	    rwc.repaint();
	    rwc2.repaint();
	   
	    outputpanel.rwocA.repaint();
	     
		    if(rwconp.c1.getState()){
                        if(!state.Is_dB){
                            rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM));
                            rwccf.setYRangeMin(0.0);
                            rwccf.setdB(false);
                        }
                        else{
                            rwccf.setYRangeMax(0.0);
                            rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccf.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccf.setdB(true);
                        }
                        rwccf.setType(1);
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){
                            rwccf.setYRangeMax(MaestroA.getMax(state.ethetaM2));
                            rwccf.setYRangeMin(0.0);
                            rwccf.setdB(false);
                        }
                        else{
                            rwccf.setYRangeMax(0.0);
                            rwccf.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccf.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccf.setdB(true);
                        }
                        rwccf.setType(2);
                    } 
		    //rwccf.setYRangeMin(0.0);
		    rwccf.plotZeroLine(false);
		    rwccf.plotRefPoint(false);
		    rwccf.setPolar(false);
		    rwccf.setPhiAngle((int)state.phi_angle);
		    
		    rwccf.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
		    if(rwconp.c1.getState()){
                        if(!state.Is_dB){rwccf.plot(state.x_polar,state.ethetaM);}
                        else{rwccf.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){rwccf.plot(state.x_polar,state.ethetaM2);}
                        else{rwccf.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    
		    rwccf.ignition();
		    rwccf.repaint();
                    
                    if(rwconp.c1.getState()){
                        if(!state.Is_dB){
                            rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM));
                            rwccg.setYRangeMin(0.0);
                            rwccg.setdB(false);
                        }
                        else{
                            rwccg.setYRangeMax(0.0);
                            rwccg.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccg.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccg.setdB(true);
                        }
                        rwccg.setType(1);
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){
                            rwccg.setYRangeMax(MaestroA.getMax(state.ethetaM2));
                            rwccg.setYRangeMin(0.0);
                            rwccg.setdB(false);
                        }
                        else{
                            rwccg.setYRangeMax(0.0);
                            rwccg.setYRangeMin(Math.min(-100.0,MaestroA.getMin(state.ethetaM_dB)));
                            //rwccg.setYRangeMin((MaestroA.getMin(state.ethetaM_dB)));
                            rwccg.setdB(true);
                        }
                        rwccg.setType(2);
                    }
                    //rwccg.setYRangeMin(0.0);
		    rwccg.plotZeroLine(false);
		    rwccg.plotRefPoint(false);
		    rwccg.setPolar(true);
		    rwccg.setPhiAngle((int)state.phi_angle);
		    rwccg.setLabels("","","\u03b8  [degrees]");
		    
		    state.Is_theta = true;
		    state.Is_radius = false;
		    
                    if(rwconp.c1.getState()){
                        if(!state.Is_dB){rwccg.plot(state.x_polar,state.ethetaM);}
                        else{rwccg.plot(state.x_polar,state.ethetaM_dB);}
                    }
                    else if(rwconp.c2.getState()){
                        if(!state.Is_dB){rwccg.plot(state.x_polar,state.ethetaM2);}
                        else{rwccg.plot(state.x_polar,state.ethetaM_dB);}
                    }
		    rwccg.ignition();
		    rwccg.repaint();
        }
    }
    /**
     * Called to start the applet.  You never need to call this directly; it
     * is called when the applet's document is visited.
     */
    public void start() {
    }

    /**
     * Called to stop the applet.  This is called when the applet's document is
     * no longer on the screen.  It is guaranteed to be called before destroy()
     * is called.  You never need to call this method directly
     */
    public void stop() {
    }

    /**
     * Cleans up whatever resources are being held.  If the applet is active
     * it is stopped.
     */
    public void destroy() {
    }
}

