//DipoleAnt.java
//This is a program for a simple dipole antenna - very old Java...

import java.util.*;

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

public class DipoleAnt extends Frame implements ActionListener, WindowListener{

//The Canvas
//About about;
    Instructions instructions;
    Panel instrFrame;
DrawCanvas dcan;
RadarCanvas rcan1, rcan2;
SlidePanel slidepanel;
RadarControls rcons;

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

//dipolelength and  dipolecurrent
float dipolelength;
public float dipolelengthmax=10.0f;
public int ScrollMax=10001;

float dipolecurrent;
float scaling=1.0f;

//arrays for electric field, magnetic field, and power for theta-plane
float efield[];
float hfield[];
float power[];
//arrays for electric field, magnetic field, and power for phi-plane
float eefield[];
float hhfield[];
float ppower[];
float radiation_power, directivity, radiation_resistance;

final int DrawCanvasHPos=10;
final int DrawCanvasVPos= 55;
final int DrawCanvasWidth=295;
final int DrawCanvasHeight=250;

final int RadarCanvasHPos1=5+DrawCanvasHPos+DrawCanvasWidth;
final int RadarCanvasVPos1=10;
final int RadarCanvasWidth1=295;
final int RadarCanvasHeight1=295;

final int RadarCanvasHPos2=5+DrawCanvasHPos+DrawCanvasWidth;
final int RadarCanvasVPos2= DrawCanvasVPos+DrawCanvasHeight+5; //RadarCanvasVPos1+RadarCanvasHeight1+5;
final int RadarCanvasWidth2=295;
final int RadarCanvasHeight2=295;


final int OutPanelHPos=DrawCanvasHPos;
final int OutPanelVPos=DrawCanvasVPos+DrawCanvasHeight+51;
final int OutPanelWidth=DrawCanvasWidth;
final int OutPanelHeight=95;

//Input Panel
final int SlidePanelHPos=10;
final int SlidePanelVPos=OutPanelVPos+OutPanelHeight+4;
final int SlidePanelWidth=DrawCanvasWidth;
final int SlidePanelHeight=150;

final int RadarControlsHPos =11;
final int RadarControlsVPos =DrawCanvasVPos+DrawCanvasHeight+6; //RadarCanvasVPos2+1;
final int RadarControlsWidth =DrawCanvasWidth-2;
final int RadarControlsHeight =40;

//final int ButtonPanelHPos=RadarCanvasHPos1;
//final int ButtonPanelVPos=5;
//final int ButtonPanelWidth=100;
//final int ButtonPanelHeight=30;

final int TitlePanelHPos=DrawCanvasHPos;
final int TitlePanelVPos=10;
final int TitlePanelWidth=295;
final int TitlePanelHeight=40;


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){
        DipoleAnt f = new DipoleAnt();
        int xmove = 20;
        int ymove = 41;
        f.setSize(615+xmove,618+ymove);
        f.setVisible(true);
        f.setLayout(null);
}

public DipoleAnt(){
	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);
	
	this_month = Greg.get(Calendar.MONTH);
	//System.out.println("  This is the month = "+this_month);
	today_week = Greg.get(Calendar.DAY_OF_WEEK);
	//System.out.println("  This is the day_week = "+today_week);
	this_year = Greg.get(Calendar.YEAR);
	this_hour = Greg.get(Calendar.HOUR_OF_DAY);
	//System.out.println("  This is the year = "+this_year);
	this_minute = Greg.get(Calendar.MINUTE);
	//System.out.println("  This is the minute = "+this_minute);
	today_month = Greg.get(Calendar.DAY_OF_MONTH);
	//System.out.println("  This is the day_month = "+today_month);
	today_year = Greg.get(Calendar.DAY_OF_YEAR);
	//System.out.println("  This is the day_year = "+today_year);
	this_zone = Greg.get(Calendar.ZONE_OFFSET);
	//System.out.println("  This is the zone_offset = "+this_zone/3600000);
	saving_time = Greg.get(Calendar.DST_OFFSET);
	//System.out.println("  This is the dst_offset = "+saving_time/3600000);
	
	//try{
	 efield = new float[361];
	 hfield = new float[361];
	 power  = new float[361];

	 eefield = new float[361];
	 hhfield = new float[361];
	 ppower  = new float[361];
        //}
        //catch(RuntimeException e){
        //	showStatus("Caught RuntimeException: "+e);
        //}
        //catch(Exception e){
        //	showStatus("Caught Exception: "+e);
        //}

	dipolelength=0.5f;
	dipolecurrent=1.0f;
        int xmove = 10;
        int ymove = 34;
        instructions = new Instructions();
        instructions.setBounds(DrawCanvasHPos+1+xmove,RadarCanvasVPos2+1+ymove,593,293);
        //instructions.setVisible(false);
        instrFrame = new Panel();
        instrFrame.setBackground(Color.black);
        instrFrame.setBounds(DrawCanvasHPos+xmove,RadarCanvasVPos2+ymove,595,295);
        //instrFrame.setVisible(false);

	//Draw the Antenna Dipole
	dcan = new DrawCanvas(this);
	//add(dcan);
	dcan.setBounds(DrawCanvasHPos+1+xmove,DrawCanvasVPos+1+ymove, DrawCanvasWidth-2, DrawCanvasHeight-2);

	//setThetaPlaneMaxima();
	UpdateRadiation();
	//Draw the radiation diagram 1
	rcan1 = new RadarCanvas(this,true);
	//add(rcan1);
	rcan1.setBounds(RadarCanvasHPos1+1+xmove,RadarCanvasVPos1+1+ymove, RadarCanvasWidth1-2, RadarCanvasHeight1-2);
	
	Panel prc1 = new Panel();
	    prc1.setBackground(Color.black);
	    //add(prc1);
	    prc1.setBounds(RadarCanvasHPos1+xmove,RadarCanvasVPos1+ymove, RadarCanvasWidth1, RadarCanvasHeight1);
	
	//Draw the radiation diagram 2
	rcan2 = new RadarCanvas(this,false);
	//add(rcan2);
	rcan2.setBounds(RadarCanvasHPos2+1+xmove,RadarCanvasVPos2+1+ymove, RadarCanvasWidth2-2, RadarCanvasHeight2-2);
	
	Panel prc2 = new Panel();
	    prc2.setBackground(Color.black);
	    //add(prc2);
	    prc2.setBounds(RadarCanvasHPos2+xmove,RadarCanvasVPos2+ymove, RadarCanvasWidth2, RadarCanvasHeight2);	

	
	//Draw the slide panel
	slidepanel = new SlidePanel(this);
	//add(slidepanel);
	slidepanel.setBounds(SlidePanelHPos+xmove, SlidePanelVPos+ymove,SlidePanelWidth,SlidePanelHeight);

	//Make radar controls
	rcons = new RadarControls(this);
	//add(rcons);
	rcons.setBounds(RadarControlsHPos+xmove, RadarControlsVPos+ymove, RadarControlsWidth, RadarControlsHeight);
    
	//Outpanel
	outpanel = new OutPanel(this);
        //add(outpanel);
	outpanel.setBounds(OutPanelHPos+xmove, OutPanelVPos+ymove, OutPanelWidth, OutPanelHeight);
        outpanel.setVisible(true);

	//TitlePanel
        
        titlepanel = new TitlePanel("Module 9.2","Linear Dipole Antenna");
	//titlepanel = new TitlePanel(this);
	//add(titlepanel);
	titlepanel.setBounds(TitlePanelHPos+xmove, TitlePanelVPos+ymove, TitlePanelWidth, TitlePanelHeight);
	
	Panel pgraph = new Panel();
	    pgraph.setBackground(Color.black);
	    //add(pgraph);
	    pgraph.setBounds(RadarControlsHPos-1+xmove,RadarControlsVPos-1+ymove,
			     RadarControlsWidth+2,RadarControlsHeight+2);
	
	Panel pcanvas = new Panel();
	    pcanvas.setBackground(Color.black);
	    //add(pcanvas);
	    pcanvas.setBounds(DrawCanvasHPos+xmove,DrawCanvasVPos+ymove, DrawCanvasWidth, DrawCanvasHeight);
	    	
	Panel p0 = new Panel();
	    p0.setBackground(Color.cyan);
	    //p0.setBackground(tinta);
	    //add(p0);
	    p0.setBounds(3+xmove,3+ymove,25+TitlePanelWidth+RadarCanvasWidth1-6,609);
	    
	Panel p00 = new Panel();
	    p00.setBackground(Color.black);
	    //add(p00);
	    p00.setBounds(xmove,ymove,25+TitlePanelWidth+RadarCanvasWidth1,615);
    
            //if(this_year > line.magicyear || (this_year == line.magicyear && this_month > line.magicmonth)){
            //    LicenseExpired = true;
            //}
            //else
            //{ 	    
	    //add(about);
            add(instructions);
            add(instrFrame);
            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){
	g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
	
	Font licensefont = new Font("SanSerif",Font.BOLD,18); 	
	g.setColor(Color.white);	 	
	g.fillRect(0,0,945,765); 	
	
	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);
	} 

}
*/
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,630,640);  	
	g.fillRect(0,0,615,615); 
        
	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,633);
        //g.setFont(fonto);
        //g.drawString("Amanogawa, "+annonuovo+" - All Rights Reserved",30,631);
    }
    
    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);
     }
 }

public void UpdateRadiation(){
	int i;
	float theta;
	float SpaceImp=(float)(120.0*Math.PI);
	float R=1.0f;
	float max1, max2, max3;
	float Prad, U, Umax; 
	
	if(dipolelength<0.5f){
		scaling=(float)(1.0/Math.sin(Math.PI*(dipolelength)));
	}
	else{
		scaling=1.0f;
	}
	
	//radiation power, and field loop.
	for(i=0;i<360;i++){
		theta=(float)(Math.PI*i/360.0f);
		//Avoid sin(theta)=0.0 situation
		if(theta!=0.0f && Math.abs(theta)!=(float)(Math.PI) ){
		  hfield[i]=(float)Math.abs((dipolecurrent*scaling*
		   ((Math.cos(Math.PI*dipolelength*Math.cos(theta))-Math.cos(Math.PI*dipolelength))/Math.sin(theta))
		   /(2.0*Math.PI*R)));
		  efield[i]=SpaceImp*hfield[i];
		  power[i]=efield[i]*efield[i]/(2.0f*SpaceImp);
		}
		else{
	         efield[i]=0.0f;
	   	 hfield[i]=0.0f;
	   	 power[i] =0.0f;
		}
	}
	//directivity loop
	Umax=0.0f;
	Prad=0.0f;
	for(i=0;i<180;i++){
		theta=(float)(i*Math.PI/180.0f);
		//Avoid sin(theta)=0.0 situation
		if(theta!=0.0f && Math.abs(theta)!=(float)(Math.PI) ){
		   U= (float)(SpaceImp*dipolecurrent*dipolecurrent*scaling*scaling*
		   Math.pow((Math.cos(Math.PI*dipolelength*Math.cos(theta))-Math.cos(Math.PI*dipolelength))/Math.sin(theta),2.0)
		   /(8.0*Math.PI*Math.PI));
		   if(Umax<Math.abs(U)) Umax=Math.abs(U);
		   Prad+=(float)(U*Math.sin(theta)*2.0*Math.PI*Math.PI/180.0f);
		}
	}
	if(Prad >0.0f){
		directivity=(float)(4.0f*Math.PI*Umax/Prad);
	}
	else directivity=0.0f;

	//radiation resistance and radiated power
	radiation_resistance=(float)(2.0*Prad/(dipolecurrent*dipolecurrent));
        radiation_power=Prad;

	max1=0.0f;
	max2=0.0f;
	max3=0.0f;
	for(i=0;i<360;i++){
		if(max1<Math.abs(efield[i])) max1=Math.abs(efield[i]);	
		if(max2<Math.abs(hfield[i])) max2=Math.abs(hfield[i]);	
		if(max3<Math.abs(power[i]) ) max3=Math.abs(power[i] );
	}
	for(i=0;i<360;i++){
		if(max1>0.0f) efield[i]=efield[i]/max1;
		if(max2>0.0f) hfield[i]=hfield[i]/max2;
		if(max3>0.0f) power[i] = power[i]/max3;
	}
	for(i=0;i<360;i++){
		eefield[i]=efield[180]*0.98f;
		hhfield[i]=hfield[180]*0.98f;
		ppower[i] =power[180]*0.98f;
		
		//eefield[i]=0.95f;
		//hhfield[i]=0.95f;
		//ppower[i] =0.95f;
	}
	efield[360]=efield[0];
	hfield[360]=hfield[0];
	power[360]=power[0];
	eefield[360]=eefield[0];
	hhfield[360]=hhfield[0];
	ppower[360]=ppower[0];
	

}


}//End of Antenna class
