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

public class LineofSight extends Frame implements ActionListener, AdjustmentListener, ItemListener, MouseListener, WindowListener{
    //About about;
    Instructions instructions;
    Panel instrFrame, instrFrame1, instrFrame2;
    TitlePanel titlepanel;
    NewGuideCanvas rwc;
    NewGuideInputPanel inputpanel;
    NewGuideInputPanel2 inputpanel2;
    
    NewGuide_State state;
    SetRangePanel a_panel, frequency_panel, distance_panel;
    NewGuideControlInput rwci;
    
    NewGuideControlPanel rwconp;
    NewGuideOutputPanel outputpanel;
    
    NewGuideGraphCanvasDir rwcca;
    NewGuideGraphCanvasF rwccf;
    NewGuideGraphCanvasG rwccg;
        
    public Label labtitle;
    Font ttfFont, phiFont, italicFont;
    final Font labfonte = new Font("Sanserif",Font.BOLD,16);
    
    
     /**
     * 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){
        LineofSight f = new LineofSight();
        int xmove = 20;
        int ymove = 41;
        f.setSize(980+xmove,660+ymove);
        f.setVisible(true);
        f.setLayout(null);
    }
    
    public LineofSight() {
	setLayout(null);
        
        int xmove = 10;
        int ymove = 34;
	
	titlepanel = new TitlePanel("Module 9.9","Line-of-Sight Transmission");
	//add(titlepanel);
	titlepanel.setBounds(10+xmove,9+ymove,300,41);
        
        instructions = new Instructions();
        //instructions.setBounds(11,261,908,298);
        instructions.setBounds(11+xmove,56+ymove,958,303);
	//instructions.setVisible(false);
        
        instrFrame = new Panel();
        instrFrame.setBackground(Color.black);
        //instrFrame.setBounds(10,260,910,30);
        instrFrame.setBounds(10+xmove,55+ymove,960,305);
        //instrFrame.setVisible(false);
        
        instrFrame1 = new Panel();
        instrFrame1.setBackground(new Color(255,255,245));
        instrFrame1.setBounds(316+xmove,11+ymove,653,56);
        
        instrFrame2 = new Panel();
        instrFrame2.setBackground(Color.black);
        instrFrame2.setBounds(315+xmove,10+ymove,655,57);
        
        labtitle = new Label("Instructions",Label.LEFT);
        labtitle.setFont(labfonte);
	labtitle.setForeground(Color.black);
        labtitle.setBackground(new Color(255,255,245));
        labtitle.setBounds(430+xmove,21+ymove,200,30);
        labtitle.setVisible(true);
        
        rwconp = new NewGuideControlPanel();
	rwconp.setBounds(317+xmove,12+ymove,296,36);
        
        rwci = new NewGuideControlInput();
        rwci.setBounds(137+xmove,63+ymove,162,30);
	        
	state = new NewGuide_State();
	
	rwc = new NewGuideCanvas(state);
	//add(rwc);
	//rwc.setBounds(12,12,956,241);
        rwc.setBounds(12+xmove,367+ymove,956,281);
	rwc.setVisible(true);
	
	inputpanel = new NewGuideInputPanel(state);
	//add(inputpanel);
	inputpanel.setBounds(12+xmove,57+ymove,296,301);
	inputpanel.setVisible(true);
        
        inputpanel2 = new NewGuideInputPanel2(state);
	//add(inputpanel2);
	inputpanel2.setBounds(12+xmove,57+ymove,296,301);
	inputpanel2.setVisible(true);
	
	outputpanel = new NewGuideOutputPanel(state);
	//add(outputpanel);
	outputpanel.setBounds(317+xmove,57+ymove,296,301);
	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));
	
	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,346,346);
        rwccg.setBounds(622+xmove,12+ymove,346,346);
	
        // backpanel for diagram
	Panel p01 = new Panel();
	    p01.setBackground(Color.white);
	    //add(p01);
	    //p01.setBounds(11,366,958,243);
            p01.setBounds(11+xmove,366+ymove,958,283);
	
	Panel p1 = new Panel();
	    p1.setBackground(Color.black);
	    //add(p1);
	    //p1.setBounds(10,365,960,245);
            p1.setBounds(10+xmove,365+ymove,960,285);
	    
        // 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 radial plot
	Panel p30 = new Panel();
	    p30.setBackground(Color.white);
	    //add(p30);
	    //p30.setBounds(621,261,348,348);
            p30.setBounds(621+xmove,11+ymove,348,348);
    
	Panel p3 = new Panel();
	    p3.setBackground(Color.black);
	    //add(p3);
	    //p3.setBounds(620,260,350,350);
	    p3.setBounds(620+xmove,10+ymove,350,350);
            
        //backpanel for outputpanel    
        Panel p20 = new Panel();
	    p20.setBackground(Color.white);
	    //add(p20);
	    p20.setBounds(316+xmove,56+ymove,298,303);
    
	Panel p2 = new Panel();
	    p2.setBackground(Color.black);
	    //add(p2);
	    p2.setBounds(315+xmove,55+ymove,300,305);
	    
	//backpanels for control panel
	Panel p40 = new Panel();
	    p40.setBackground(Color.white);
	    //add(p40);
	    p40.setBounds(316+xmove,11+ymove,298,38);
    
	Panel p4 = new Panel();
	    p4.setBackground(Color.black);
	    //add(p4);
	    p4.setBounds(315+xmove,10+ymove,300,40);
	
	//backpanels for inputpanel        
	Panel p50 = new Panel();
	    p50.setBackground(Color.white);
	    //add(p50);
	    //p50.setBounds(11,261,298,298);
            p50.setBounds(11+xmove,56+ymove,298,303);
    
	Panel p5 = new Panel();
	    p5.setBackground(Color.black);
	    //add(p5);
            //p5.setBounds(10,260,300,300);
	    p5.setBounds(10+xmove,55+ymove,300,305);
                        
	//backpanel for rwccs
	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,3,974,614);
            p0.setBounds(3+xmove,3+ymove,974,654);
	    
	Panel p00 = new Panel();
	    p00.setBackground(Color.black);
	    //add(p00);
	    //p00.setBounds(0,0,980,620);
            p00.setBounds(xmove,ymove,980,660);
            
            //if(state.this_year > line.magicyear || (state.this_year == line.magicyear && state.this_month > line.magicmonth)){
            //    state.LicenseExpired = true;
            //}
            //else
            //{ 	    
            
            add(labtitle);
            add(instructions);
            add(rwci);
            add(instrFrame1);
            add(instrFrame2);
            add(instrFrame);
	    add(titlepanel);
	    add(rwc);
	    add(inputpanel);
            add(inputpanel2);
	    add(outputpanel);
	    add(frequency_panel);
	    add(a_panel);
	    add(distance_panel);
	    add(rwconp);
	    //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);
        
        outputpanel.c1.addItemListener(this);
        rwci.c1.addItemListener(this);
        
	inputpanel.slider1.addAdjustmentListener(this);
	inputpanel.slider2.addAdjustmentListener(this);
	inputpanel.slider3.addAdjustmentListener(this);
        inputpanel.slider4.addAdjustmentListener(this);
	inputpanel.bupdate.addActionListener(this);
        
        inputpanel2.ground1.addActionListener(this);
        inputpanel2.ground2.addActionListener(this);
        inputpanel2.ground3.addActionListener(this);
        inputpanel2.ground4.addActionListener(this);
        inputpanel2.ground5.addActionListener(this);
        inputpanel2.ground6.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);

        rwc.repaint();
        
        outputpanel.rwocA.repaint();
        outputpanel.rwocB.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(state.phi_angle);
            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();

            rwccg.setPolarization(state.IsTE);
            rwccg.setVector(state.onlyDirect);
            rwccg.setPhiAngle(state.phi_angle);
            rwccg.setReflectionMag(state.ReflectionMag);
            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 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 adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==inputpanel.slider2 || evt.getSource()==inputpanel.slider3){
	    
            //rwconp.c1.setEnabled(false);
            //rwconp.c2.setEnabled(false);
            state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	    
            rwc.setFrequency(state.frequency);
	    rwc.setEpsilon(state.epsilon_r);
	    rwc.setAntennaLength(state.DipoleLength_lambda);
	    rwc.setAntennaRadius(state.wire_radius);
	    
	    rwc.repaint();
	    
	    outputpanel.rwocA.repaint();
            outputpanel.rwocB.repaint();
            
            rwccg.setPolarization(state.IsTE);
            rwccg.setVector(state.onlyDirect);
            rwccg.setPhiAngle(state.phi_angle);
            rwccg.setReflectionMag(state.ReflectionMag);
            rwccg.ignition();
	    rwccg.repaint();
	    
                  
    	}
        
	if(evt.getSource()==inputpanel.slider1 || evt.getSource()==inputpanel.slider4){
	    
            state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	    
            rwc.setFrequency(state.frequency);
	    rwc.setEpsilon(state.epsilon_r);
	    rwc.setAntennaLength(state.DipoleLength_lambda);
	    rwc.setAntennaRadius(state.wire_radius);
	    
	    rwc.repaint();
	    
	    outputpanel.rwocA.repaint();
            outputpanel.rwocB.repaint();
            
            rwccg.setPolarization(state.IsTE);
            rwccg.setVector(state.onlyDirect);
            rwccg.setPhiAngle(state.phi_angle);
            rwccg.setReflectionMag(state.ReflectionMag);
            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 actionPerformed(ActionEvent evt){
        //if(evt.getSource()==about.bupdate){
        if(evt.getSource()==instructions.bupdate){
            instructions.setVisible(false);
            instrFrame.setVisible(false);
            instrFrame1.setVisible(false);
            instrFrame2.setVisible(false);
            labtitle.setVisible(false);
        }
        
        if(evt.getSource()==rwconp.about){
            instructions.setVisible(true);
            instrFrame.setVisible(true);
            instrFrame1.setVisible(true);
            instrFrame2.setVisible(true);
            labtitle.setVisible(true);
        }
        
        if(evt.getSource()==inputpanel2.ground1 || evt.getSource()==inputpanel2.ground2 || evt.getSource()==inputpanel2.ground3 || 
                evt.getSource()==inputpanel2.ground4 || evt.getSource()==inputpanel2.ground5 || evt.getSource()==inputpanel2.ground6){
            
            state.ignition();
            rwccg.setPhiAngle(state.phi_angle);
            rwccg.setReflectionMag(state.ReflectionMag);
	    rwccg.setPolarization(state.IsTE);
            rwccg.setVector(state.onlyDirect);
            rwccg.ignition();
            rwccg.repaint();
            rwc.repaint();
    
	    outputpanel.rwocA.repaint();
            outputpanel.rwocB.repaint();
	    
        }
        
	if(evt.getSource()==inputpanel.bupdate){
	    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);
	    
	    rwc.repaint();
    
	    outputpanel.rwocA.repaint();
            outputpanel.rwocB.repaint();
	     
		    rwccf.plotZeroLine(false);
		    rwccf.plotRefPoint(false);
		    rwccf.setPolar(false);
		    rwccf.setPhiAngle((int)state.phi_angle);
		    
		    rwccg.setPhiAngle(state.phi_angle);
                    rwccg.setReflectionMag(state.ReflectionMag);
		    rwccg.setPolarization(state.IsTE);
                    rwccg.setVector(state.onlyDirect);
                    rwccg.ignition();
		    rwccg.repaint();
        }
    }
    
    public void itemStateChanged(ItemEvent evt){
	ItemSelectable ie = evt.getItemSelectable();
	
        if(evt.getSource()==rwci.c1){
		if(ie.getSelectedObjects()[0]==" Link Properties"){
                    inputpanel.setVisible(true);
		    inputpanel2.setVisible(false);
		}
		else if(ie.getSelectedObjects()[0]==" Ground Properties"){
		    inputpanel.setVisible(false);
		    inputpanel2.setVisible(true);
		}
	    }
        
        if(evt.getSource()==rwconp.c1){
            state.IsTE = 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.setPolarization(state.IsTE);
            rwccg.setVector(state.onlyDirect);
            rwc.setFrequency(state.frequency);
	    rwc.repaint();
	    
	    outputpanel.rwocA.repaint();
            outputpanel.rwocB.repaint();
	     
		    rwccf.plotZeroLine(false);
		    rwccf.plotRefPoint(false);
		    rwccf.setPolar(false);
		    rwccf.setPhiAngle((int)state.phi_angle);
		    
		    rwccf.setLabels("","","\u03b8  [degrees]");
		    rwccf.ignition();
		    rwccf.repaint();
                    
                    rwccg.setPolarization(state.IsTE);
                    rwccg.setVector(state.onlyDirect);
                    rwccg.setPhiAngle(state.phi_angle);
                    rwccg.setReflectionMag(state.ReflectionMag);
		    rwccg.ignition();
		    rwccg.repaint();
        }
        else if(evt.getSource()==rwconp.c2){
            state.IsTE = false;
            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.setPolarization(state.IsTE);
            rwccg.setVector(state.onlyDirect);
            rwc.setFrequency(state.frequency);
	    rwc.repaint();
	    
	    outputpanel.rwocA.repaint();
            outputpanel.rwocB.repaint();
	     
		    rwccf.plotZeroLine(false);
		    rwccf.plotRefPoint(false);
		    rwccf.setPolar(false);
		    rwccf.setPhiAngle((int)state.phi_angle);
		    rwccf.setLabels("","","\u03b8  [degrees]");
		    
		    rwccg.setLabels("","","H(C)");
		    rwccf.ignition();
		    rwccf.repaint();
                    
                    rwccg.setPolarization(state.IsTE);
                    rwccg.setVector(state.onlyDirect);
                    rwccg.setPhiAngle(state.phi_angle);
                    rwccg.setReflectionMag(state.ReflectionMag);
		    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() {
    }
}

