import java.awt.*;

import java.awt.image.*;

import java.lang.*;

import java.util.*;

import java.applet.Applet;





public class Doppler extends Applet

{

	public void init() 

	{

		this.setLayout(new BorderLayout());

		this.resize(1000,400);

		this.layout();

		this.show();

		this.showStatus("Init Complete");

	}

	public void start()

	{

		this.showStatus("Starting Doppler");

		window win = new window("Doppler",300,300,10,100);

		this.add("Center",win);		

		this.layout();

		this.show();

		showStatus("Begining Doppler Effect");

		win.start();

	}	

	public static void main(String args[]) 

	{

		System.out.println( "Executing..." );

		for (int current = 0; current < args.length;current++)

		{

		

			System.out.println(args[current]);

		}

		if (args.length == 4)

		{

			int Os = Integer.parseInt(args[0]);

			int Ws = Integer.parseInt(args[1]);

			int Pt = Integer.parseInt(args[2]);

			int Td = Integer.parseInt(args[3]);

		

		Frame f = new Frame("Doppler");

		window win = new window("Doppler",Os,Ws,Pt,Td);

		f.add("Center",win);

		f.resize(1000,400);

//		f.pack();

		f.show();

		f.layout();

		//layout();

//		win.show();

		win.start();

		}

		else 

		System.out.println( "Object Speed, Wave Speed, Pulse Time, Time Delay ms" );

	}

	public boolean handleEvent(Event e)

	{

		System.out.println(e);

		if((e.target == this) & (e.id == 201))

		{

	//		this.dispose();

			System.exit(0);

			return true;

		}

		else if((e.target == this) & (e.id == 502))

		{



			this.show();

	//		win.show();

			return true;

		}

		else return false;

	 }



}





/*

This section will be replace with an applet start section....

*/

//public class Doppler {

//	public window win;





//} 

//---------------------------------------------------------------------------------

//This is the main class of the program....

//All the Panels are defined and sized and all the classes are loaded and initialized

//---------------------------------------------------------------------------------



public class window extends Panel

{

	public Panel ControlP;

	public Panel graphics;

	public Panel Scale;

	public DopplerWave Page1;

	public DopplerWave Page2;

	public DopplerScale scale1;

	public Controls controls;

	int Ospeed = 400;

	int  Wspeed = 300;

	long  timedelay= 100;

	int Pulsetime = 100;

	int ScrnSize = 200; //move to adjustable variable depending on resize event

	int ScrnLen = 1000; //

	int cycle = 0;		//number of loops preformed..

	int scale = 100000;	//Number of meters per screen	

	int position = 0;   //current Position of object in pixels

	int I = 0;

	int waves[][] = new int[11][2];

	public window(String title,int Ospeed,int Wspeed, int Pulsetime, int timedelay)

	{

		this.Ospeed = Ospeed;

		this.Wspeed = Wspeed;

		this.Pulsetime = Pulsetime;

		this.timedelay = timedelay;

		this.resize(this.ScrnLen,this.ScrnSize*2);

		this.hide();		



	}



	public void start()

	{

	this.setLayout(new BorderLayout());

	int Ospeed = this.Ospeed;

	int Wspeed = this.Wspeed;

	int Pulsetime = this.Pulsetime;

	long timedelay = this.timedelay;

	int scale = this.scale;

	int cycle = this.cycle;		//number of loops preformed..

	int position  = this.position;   //current Position of object in pixels

	int ScrnLen = this.ScrnLen;

	int ScrnSize = this.ScrnSize;

	TextArea textin =  new TextArea();

		

	graphics = new Panel();

	ControlP = new Panel();

	Scale = new Panel();

	

	Controls controls = new Controls();

	DopplerWave Page1 = new DopplerWave(0,0,ScrnSize,waves,Wspeed,scale,0);

	DopplerWave Page2 = new DopplerWave(0,0,ScrnSize,waves,Wspeed,scale,0);

	DopplerScale scale1 = new DopplerScale(scale);



	this.resize(ScrnLen,ScrnSize*2);

	

		





	graphics.setLayout(new BorderLayout());

	graphics.resize(ScrnLen,ScrnSize);

	Page1.resize(ScrnLen,ScrnSize);

	Page2.resize(ScrnLen,ScrnSize);

	graphics.add("Center",Page1);

	graphics.add("Center",Page2);

	this.repaint();

	ControlP.setLayout(new BorderLayout());

	ControlP.resize(ScrnLen,ScrnSize-100);

	controls.resize(ScrnLen,ScrnSize-100);

	

	ControlP.add("Center",controls);

	

	



	Scale.resize(ScrnLen,50);

	Scale.setLayout(new BorderLayout());

	Scale.add("Center",scale1);

	scale1.resize(ScrnLen,50);

	this.repaint();

	scale1.show();

	Scale.show();

	controls.show();	

	ControlP.show();

	Page1.show();

	graphics.show();

	

	



//Auto scale function-----------------------------------------

		if (Ospeed >= 100)

			scale = 1000;

		if (Ospeed >= 1000)

			scale = 10000;

		if (Ospeed >= 10000)

			scale = 100000;

		if (Ospeed >= 100000)

			scale = 1000000;

		Page1.scale = scale;

		Page2.scale = scale;

		scale1.Scale = scale;

//------------------------------------------------------------			



		scale1.resize(ScrnLen,50);

		scale1.show();

//------------------------------------------------------------------

//Timer function setup

		long timestamp = 0;

		long timenext = 0;

//		System.out.println("Timer:"+ timestamp);

	

	this.hide();

	this.resize(ScrnLen,ScrnSize);

	this.show();			

	//addGrid (this,graphics,0,0,ScrnLen,200,GridBagConstraints.NONE,GridBagConstraints.NORTH);

	//addGrid (this,Scale,0,201,ScrnLen,50,GridBagConstraints.NONE,GridBagConstraints.NORTH);

	//addGrid (this,ControlP,0,252,ScrnLen,100,GridBagConstraints.NONE,GridBagConstraints.NORTH);

	this.add("North",graphics);

	this.add("Center",Scale);

	this.add("South",ControlP);	



	layout();

	this.hide();

	this.resize(ScrnLen,ScrnSize*2);



	this.show();			

	this.repaint();

//------------------------------------------------------------------

//animation and movment functions

//Need a scale of graph setting

//

//-----------------test scale--------------------------

//scale:	1000m 	: cycle = .01  sec	timing =	1

//-----------------------------------------------------

//scale:	10000m	: cycle = .1   sec  timing =	10

//scale:	100000m	: cycle =  1   sec  timing =	100

//

//scale / 1000 = meters/cycle

//

//

//





// MpC is meters per cycle

		int MpC = scale / 10;

		Page1.MpC = MpC;

		Page2.MpC = MpC;

 

//-----------------------------------------------------------------	





			Page1.show();

		//	this.pack();

			this.show();

			while (cycle < ScrnLen + 2)

			{

				for (position=0;position<ScrnLen;position = position)

				{

					//System.out.println( "Time:" + time + " Position:" + position + " owdiff:" + OWdiffI + ":" + OWdiffCI +" I:" + i);

					Page1.hide();

					Page2.show();



					

//------------------timer usage---------------------------------------------

					Date timer = new Date();

					timestamp = timer.getTime();

					while ( timenext - timestamp <= controls.DelayTime || controls.DelayTime >= 495)

					{

						Date timer2 = new Date();

						timenext = timer2.getTime();

//						System.out.println("Timestamp:"+ timestamp + " Timenext:" + timenext);

					}

//-----------------------------------------------------------------------------	

// on 100,000 scale 1 cycle = 10 ms 

					if (cycle % (Pulsetime / 10) == 0 && this.waves[0][0] < 10)

					{

						this.waves[0][0] = this.waves[0][0] + 1;

						this.waves[this.waves[0][0]][0] = cycle;

						this.waves[this.waves[0][0]][1] = position;

						Page1.position = position;

						Page1.waves = this.waves;

					} 

					else

					{

						Page1.position = position;

						Page1.time = cycle;

					}

					Page2.hide();

					Page1.show();

					Page2.position = position;

					Page2.time = cycle;

					cycle++;

						position = position + (Ospeed / MpC);

				}

				cycle = 0;

				position = 0;

				this.waves[0][0] = 0;

		

			}

	

	}

	public boolean handleEvent(Event e)

	{

//		System.out.println(e);

		if((e.target == this) & (e.id == 201))

		{

//			this.dispose();

			System.exit(0);

			return true;

		}

		else return false;

	 }



}

