import java.util.*;

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

public class Antenna extends Frame implements ActionListener, WindowListener{

//The Canvas
//About about;
    Instructions instructions;
    Panel instrFrame, cyanFrame;
DrawCanvas dcan;
RadarCanvas rcan1, rcan2;

SlidePanel slidepanel;
RadarControls rcons;
PhiControls phicontrol;
OutPanel outputpanel;

OutPanel   outpanel;
TitlePanel titlepanel;
private final Color bcolor=new Color(179,230,255);

//dipolelength and  dipolecurrent
//antenna A
double dipolelengthA;
final double dipolelengthmaxA=3.0;
double dipolecurrentA;
double scalingA=1.0;

//These parameters determine maxima in theta plane for graph display
double TotalP, PlaneEMax, PlaneHMax, PlanePMax;

//Impedance of the medium
double SpaceImp;

//antenna B
double dipolelengthB;
final double dipolelengthmaxB=3.0;
double dipolecurrentB;
double scalingB=1.0;

double distancia, distanciamax, fase, R;
//distancia--> distance between dipoles
//fase --> phase between dipoles
//R    --> radius at which diagrams for radiation are computed. 

//arrays for electric field, magnetic field, and power for theta-plane
Complex efield[];
Complex hfield[];
double power[];
//arrays for electric field, magnetic field, and power for phi-plane
Complex eefield[];
Complex hhfield[];
double ppower[];
double radiation_powerA, directivityA, radiation_resistanceA;
double radiation_powerB, directivityB, radiation_resistanceB;
double directivityAB;

public boolean LicenseExpired;
    
public int this_month, today_week, this_year, this_hour, this_minute, today_month, 
	       today_year,this_zone, saving_time;
	       
GregorianCalendar Greg = new GregorianCalendar();

public static void main(String[] args){
        Antenna f = new Antenna();
        int xmove = 20;
        int ymove = 41;
        f.setSize(624+xmove,637+ymove);
        f.setVisible(true);
        f.setLayout(null);
}

public Antenna(){
	setLayout(null);
        String lcOSName = System.getProperty("os.name").toLowerCase();
        boolean MAC_OS_X = lcOSName.startsWith("mac os x");
        boolean PC_OS = lcOSName.startsWith("windows");
        TheFonts.setMAC_OS_X(MAC_OS_X);
        TheFonts.setPC_OS(PC_OS);
	//setBackground(bcolor);
	
        int xmove = 10;
        int ymove = 34;
        
	this_month = Greg.get(Calendar.MONTH);
	today_week = Greg.get(Calendar.DAY_OF_WEEK);
	this_year = Greg.get(Calendar.YEAR);
	this_hour = Greg.get(Calendar.HOUR_OF_DAY);
	this_minute = Greg.get(Calendar.MINUTE);
	today_month = Greg.get(Calendar.DAY_OF_MONTH);
	today_year = Greg.get(Calendar.DAY_OF_YEAR);
	this_zone = Greg.get(Calendar.ZONE_OFFSET);
	saving_time = Greg.get(Calendar.DST_OFFSET);
	
	efield = new Complex[361];
	hfield = new Complex[361];
	power  = new double[361];

	eefield = new Complex[361];
	hhfield = new Complex[361];
	ppower  = new double[361];
	
	dipolelengthA=0.5;
	dipolecurrentA=1.0;
	dipolelengthB=0.5;
	dipolecurrentB=1.0;
	distancia=0.5;
	fase=0.0;
	distanciamax=1.0;
	R=100.0;
	SpaceImp=(double)(120*Math.PI);
	
        //PhiControls
	phicontrol = new PhiControls(this);
	//add(phicontrol);
	phicontrol.setBounds(315+xmove,309+ymove,300,17);
        
	//Draw the Antenna Dipole
	dcan = new DrawCanvas(this);
	//add(dcan);
	dcan.setBounds(11+xmove,56+ymove,298,253);
        	
	setThetaPlaneMaxima();
	UpdateRadiation();
        
        //about = new About();
        //add(about);
        //about.setBounds(9,9,606,616);
        //about.setVisible(false);
        instructions = new Instructions();
        //instructions.setBounds(9,9,606,616);
        instructions.setBounds(11+xmove,315+ymove,603,309);
        //instructions.setVisible(false);
        instrFrame = new Panel();
        instrFrame.setBackground(Color.black);
        instrFrame.setBounds(10+xmove,314+ymove,605,311);
        cyanFrame = new Panel();
        cyanFrame.setBackground(Color.cyan);
        cyanFrame.setBounds(8+xmove,310+ymove,609,5);
        //instrFrame.setVisible(false);

	
        //Draw the radiation diagram 1
	rcan1 = new RadarCanvas(this,true);
	//add(rcan1);
	rcan1.setBounds(316+xmove,11+ymove,298,298);
       
	Panel prc1 = new Panel();
	    prc1.setBackground(Color.black);
	    //add(prc1);
	    prc1.setBounds(315+xmove,10+ymove,300,300);
	
	//Draw the radiation diagram 2
	rcan2 = new RadarCanvas(this,false);
	//add(rcan2);
	rcan2.setBounds(316+xmove,326+ymove,298,298);
        
        Panel prc2 = new Panel();
	    prc2.setBackground(Color.black);
	    //add(prc2);
	    prc2.setBounds(315+xmove,325+ymove,300,300);	

	
	//Draw the slide panel
	slidepanel = new SlidePanel(this);
	//add(slidepanel);
	slidepanel.setBounds(10+xmove,396+ymove,300,229);
	
	//Make radar controls
	rcons = new RadarControls(this);
	//add(rcons);
	rcons.setBounds(11+xmove,359+ymove,298,31);
        
        //Outpanel
	outpanel = new OutPanel(this);
        //add(outpanel);
	outpanel.setBounds(10+xmove,315+ymove,300,38);
        outpanel.setVisible(true);
        
	//TitlePanel
        
        titlepanel = new TitlePanel("Module 9.5","Two-dipole Array");
	//add(titlepanel);
	titlepanel.setBounds(10+xmove,10+ymove,300,40);
        
	Panel pgraph = new Panel();
	    pgraph.setBackground(Color.black);
	    //add(pgraph);
	    pgraph.setBounds(10+xmove,358+ymove,300,33);
	
	Panel pcanvas = new Panel();
	    pcanvas.setBackground(Color.black);
	    //add(pcanvas);
	    pcanvas.setBounds(10+xmove,55+ymove,300,255);
            
           
        // Applet Frame   	
	Panel p0 = new Panel();
	    p0.setBackground(Color.cyan);
	    //add(p0);
	    p0.setBounds(3+xmove,3+ymove,618,628);
	    
	Panel p00 = new Panel();
	    p00.setBackground(Color.black);
	    //add(p00);
	    p00.setBounds(xmove,ymove,624,634);
    

            //if(this_year > line.magicyear || (this_year == line.magicyear && this_month > line.magicmonth)){
            //    LicenseExpired = true;
            //}
            //else
            //{ 	    
            //add(about);
            add(instructions);
            add(instrFrame);
            add(cyanFrame);
           add(phicontrol);
	   add(dcan);
	   add(rcan1);
	   add(prc1);
	   add(rcan2);
	   add(prc2);
	   add(slidepanel);
	   add(rcons);
	   add(outpanel);
	   add(titlepanel);
	   add(pgraph);
	   add(pcanvas);
	   add(p0);
	   add(p00);
	    
	   LicenseExpired = false; 	
           //}
            
         //Listeners
        this.addWindowListener(this);
        instructions.bupdate.addActionListener(this);   
}


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,540,556);  	
	/*	
	if(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 > this_year){
            annonuovo = anno;
        }
        else{
            annonuovo = this_year;
        }
        */
        //g.setColor(Color.red.darker());
        //g.setFont(fontc);
        //g.drawString("\u00a9",10,553);
        //g.setFont(fonto);
        //g.drawString("Amanogawa, "+annonuovo+" - All Rights Reserved",30,551);
}

public void setThetaPlaneMaxima(){
	int i, j;
        TotalP=0.0;
	PlaneEMax=0.0;
	PlaneHMax=0.0;
	PlanePMax=0.0;
	double tmp1;
	double phi, theta;
	tmp1=(double)(Math.PI/180.0);
	for(j=0;j<360;j=j+1){
	phi=tmp1*j;
	  for(i=0;i<180;i=i+1){
		theta=tmp1*i;
		hfield[i]=getHfield(theta,phi);
                TotalP=TotalP+(double)((hfield[i]).Magnitude()*(hfield[i]).Magnitude()*Math.sin(theta));  
		if(PlaneHMax<hfield[i].Magnitude()) PlaneHMax=(double)(hfield[i].Magnitude());    	
	  }
	}
	if(PlaneHMax==0.0) PlaneHMax=1.0;
	
	PlaneEMax=PlaneHMax*SpaceImp;
	PlanePMax=PlaneEMax*PlaneHMax/2.0;
	TotalP=(double)(R*R*SpaceImp*TotalP*0.5*(Math.PI*Math.PI)/(180.0*180.0));
}

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()==about.bupdate){
    if(evt.getSource()==instructions.bupdate){
        //about.setVisible(false);
        instructions.setVisible(false);
        instrFrame.setVisible(false);
        cyanFrame.setVisible(false);
    }
 }

public void UpdateRadiation(){
	int i;
	double theta;
	double phi;
	double r1, r2;
	double SpaceImp=(double)(120.0*Math.PI);
	
	double PradA,PradB, U, UmaxA, UmaxB; 
	
	
	//radiation power, and field loop for theta plane
	phi=phicontrol.phi*Math.PI/180-Math.PI;
	
	for(i=0;i<360;i++){
		theta=2.0*Math.PI*i/360.0;
		hfield[i]=getHfield(theta,phi);  
		efield[i]=new Complex(SpaceImp,0.0);
		efield[i].Multiply(hfield[i]);
		power[i]=(double)(efield[i].Magnitude()*efield[i].Magnitude()/(2.0*SpaceImp));
	        	
	}
	//directivity loop
	UmaxA=0.0;
	PradA=0.0;
	for(i=0;i<180;i++){
		theta=(double)(i*Math.PI/180.0);
		//Avoid sin(theta)=0.0 situation
		if(theta!=0.0 && Math.abs(theta)!=(double)(Math.PI) ){
		    if(dipolelengthA > 0.0){
			U= (double)(SpaceImp*dipolecurrentA*dipolecurrentA*scalingA*scalingA*
			Math.pow((Math.cos(Math.PI*dipolelengthA*Math.cos(theta))
			-Math.cos(Math.PI*dipolelengthA))/Math.sin(theta),2.0)
			/(8.0*Math.PI*Math.PI));
		    }
		    else{
			U= (double)(SpaceImp*dipolecurrentA*dipolecurrentA*scalingA*scalingA*
			Math.pow((Math.cos(Math.PI*(dipolelengthA+0.000001)*Math.cos(theta))
			-Math.cos(Math.PI*(dipolelengthA+0.000001)))/Math.sin(theta),2.0)
			/(8.0*Math.PI*Math.PI));
		    }
		   if(UmaxA<Math.abs(U)) UmaxA=Math.abs(U);
		   PradA+=(double)(U*Math.sin(theta)*2.0*Math.PI*Math.PI/180.0);
		}
	}
	if(PradA >0.0){
		directivityA=(double)(4.0*Math.PI*UmaxA/PradA);
	}
	else{
	       directivityA=0.0;
	}
	UmaxB=0.0;
	PradB=0.0;
	for(i=0;i<180;i++){
		theta=(double)(i*Math.PI/180.0);
		//Avoid sin(theta)=0.0 situation
		if(theta!=0.0 && Math.abs(theta)!=(double)(Math.PI) ){
		    if(dipolelengthB > 0.0){
			U= (double)(SpaceImp*dipolecurrentB*dipolecurrentB*scalingB*scalingB*
			Math.pow((Math.cos(Math.PI*dipolelengthB*Math.cos(theta))
			-Math.cos(Math.PI*dipolelengthB))/Math.sin(theta),2.0)
			/(8.0*Math.PI*Math.PI));
		    }
		    else{
			U= (double)(SpaceImp*dipolecurrentB*dipolecurrentB*scalingB*scalingB*
			Math.pow((Math.cos(Math.PI*(dipolelengthB+0.000001)*Math.cos(theta))
			-Math.cos(Math.PI*(dipolelengthB+0.000001)))/Math.sin(theta),2.0)
			/(8.0*Math.PI*Math.PI));
		    }
		   if(UmaxB<Math.abs(U)) UmaxB=Math.abs(U);
		   PradB+=(double)(U*Math.sin(theta)*2.0*Math.PI*Math.PI/180.0);
		}
	}
	if(PradB >0.0){
		directivityB=(double)(4.0*Math.PI*UmaxB/PradB);
	}
	else directivityB=0.0;
	//radiation resistance and radiated power
	radiation_resistanceA=(double)(2.0*PradA/(dipolecurrentA*dipolecurrentA));
	radiation_resistanceB=(double)(2.0*PradB/(dipolecurrentB*dipolecurrentB));

        radiation_powerA=PradA;
	radiation_powerB=PradB;
	directivityAB=(double)(4*Math.PI*R*R*PlanePMax/TotalP);

	for(i=0;i<360;i++){
		 efield[i].Divide(PlaneEMax);
		 hfield[i].Divide(PlaneHMax);
		 power[i] = (double)(power[i]/PlanePMax);
	}

	theta=(Math.PI/2.0);
	//calculation in phi plane
	for(i=0;i<360;i++){
		phi=(2.0*Math.PI*i/360.0)-Math.PI/2;
		hhfield[i]=new Complex(0.0,0.0);
		hhfield[i]=getHfield(theta,phi);
		  eefield[i]=new Complex(SpaceImp,0.0);
		  eefield[i].Multiply(hhfield[i]);
		  ppower[i]=(double)(eefield[i].Magnitude()*eefield[i].Magnitude()/(2.0*SpaceImp));
	}	
		
		for(i=0;i<360;i++){
			 eefield[i].Divide(PlaneEMax);
			 hhfield[i].Divide(PlaneHMax);
			 ppower[i] = (double)(ppower[i]/PlanePMax);
		}

		efield[360]=efield[0];
		hfield[360]=hfield[0];
		eefield[360]=eefield[0];
		hhfield[360]=hhfield[0];
		power[360]=power[0];
		ppower[360]=ppower[0];
	
}

public Complex getHfield(double theta, double phi){
	int i;
	double targ1, targ2, targ3, targa, targb;
	Complex myhfield, carga, cargb;

	if(dipolelengthA<0.5){
		if(dipolelengthA > 0.0){
		scalingA=(double)(1.0/Math.sin(Math.PI*(dipolelengthA)));
		}
		else{
		    scalingA=(double)(1.0/Math.sin(Math.PI*(dipolelengthA+0.000001)));
		}
	}
	else{
		scalingA=1.0;
	}
	if(dipolelengthB<0.5){
		if(dipolelengthB > 0.0){
		    scalingB=(double)(1.0/Math.sin(Math.PI*(dipolelengthB)));
		}
		else{
		    scalingB=(double)(1.0/Math.sin(Math.PI*(dipolelengthB+0.000001)));
		}
	}
	else{
		scalingB=1.0;
	}
	//Avoid sin(theta)=0.0 situation
	if(theta!=0.0 && Math.abs(theta)!=(double)(Math.PI) ){
	  targ1=Math.PI*distancia*Math.sin(theta)*Math.sin(phi)-fase/2.0-2.0*Math.PI*R;
	  targ2=Math.PI*distancia*Math.sin(theta)*Math.sin(phi)-fase/2.0+2.0*Math.PI*R;
	  targ3=1.0/(2.0*Math.PI*R*Math.sin(theta));
	  
	  if (dipolelengthA > 0.0){
	    targa=dipolecurrentA*scalingA*(Math.cos(Math.PI*dipolelengthA*Math.cos(theta))
                                        -Math.cos(Math.PI*dipolelengthA));
	  }
	  else{
	    targa=dipolecurrentA*scalingA*(Math.cos(Math.PI*(dipolelengthA+0.000001)*Math.cos(theta))
                                        -Math.cos(Math.PI*(dipolelengthA+0.000001)));
	  }	
	  if (dipolelengthB > 0.0){  
	    targb=dipolecurrentB*scalingB*(Math.cos(Math.PI*dipolelengthB*Math.cos(theta))
                                        -Math.cos(Math.PI*dipolelengthB));
	  }
	  else{
	    targb=dipolecurrentB*scalingB*(Math.cos(Math.PI*(dipolelengthB+0.000001)*Math.cos(theta))
                                        -Math.cos(Math.PI*(dipolelengthB+0.000001)));
	  }
	  carga=new Complex(-Math.sin(targ1),Math.cos(targ1));
	  cargb=new Complex( Math.sin(targ2),Math.cos(targ2));
			
	  carga.Multiply(targa);
	  cargb.Multiply(targb);

	  myhfield=new Complex(0.0,0.0);
	  myhfield.Add(carga);
	  myhfield.Add(cargb);

	  myhfield.Multiply(targ3);
	}
	else{
		myhfield=new Complex(0.0,0.0);
	}

	  return(myhfield);
}
/*public void itemStateChanged(ItemEvent evt){
	    //ItemSelectable ie = evt.getItemSelectable();
	    if(evt.getSource()==controlpanel.c1){
		    slidepanel.setVisible(true);
		    outputpanel.setVisible(false);
		    slidepanel.repaint();
		 }    
	    else if(evt.getSource()==controlpanel.c2){
		    slidepanel.setVisible(false);
		    outputpanel.setVisible(true);
		    outputpanel.repaint();
		 }	
}*/




}//End of Antenna class
