//******************************************************************************
// diffusion.java:	Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;
//import gjt.Stopwatch;
//import gjt.*;

//==============================================================================
// Main Class for applet diffusion
//
//==============================================================================
public class SimpleDiffusion extends Applet{

	ThreeDBorder tdb=null;
	SimpDiff SDiff=null;


	public void init(){
		String param;

		SDiff= new SimpDiff(this);
		//tdb = new ThreeDBorder(SDiff,5);
		//tdb.inset();
		setLayout(new BorderLayout());
		add("Center",SDiff);

		//param=getParameter("NumParticles");
		//if (param != null)
		//	SDiff.NUM_PARTICLE=Integer.parseInt(param);


		
}

	public void start(){
		SDiff.init();
		//SDiff.start();
	}

	public void stop(){
		SDiff.stop();
	}

	
}


class SimpDiff extends Canvas implements Runnable, StopwatchClient
{
	// THREAD SUPPORT:
	//		m_diffusion	is the Thread object for the applet
	//--------------------------------------------------------------------------
	Thread	 m_diffusion = null;
	private Thread m_watch = null;
	private Applet applet;
	public int NUM_PARTICLE=1;
	//change particle number to one 
	private Particle p[];

	private Graphics m_g;
	private Image m_image;  
	private Dimension m_dimImage;
	private int Tick=0;
	private boolean Background=true;
	private Color BG=new Color(153,204,204);
    private Point a,b,c;
	private boolean ResetLocation=false;
	private boolean ImageReady=false;



	// SimpleDiffusion Class Constructor
	//--------------------------------------------------------------------------
	public SimpDiff(Applet applet)
	{
		this.applet=applet;
	}

	// APPLET INFO SUPPORT:
	//		The getAppletInfo() method returns a string describing the applet's
	// author, copyright date, or miscellaneous information.
    //--------------------------------------------------------------------------
	public String getAppletInfo()
	{
		return "Name: diffusion\r\n" +
		       "Author: Joe Patlak\r\n" +
		       "Created with Microsoft Visual J++ Version 1.0";
	}


    //--------------------------------------------------------------------------
	public void init()
	{
		Dimension dim=size();
		int nWidth=dim.width;
		int nHeight=dim.height;
		
		a=new Point(15,15);
		b=new Point(0,0);
		c=new Point(nWidth-5,nHeight-5);


		int charge=1;
		p = new Particle[NUM_PARTICLE];

		for (int i=0; i<NUM_PARTICLE; i++){
			if (i>NUM_PARTICLE/2-1) charge=-1;
			p[i]= new Particle(applet,charge,a,b,c);
			p[i].m_StdDev=5.0;
			p[i].BG=BG;

		}
		ResizeImage();
            m_g.setColor(BG);
		    m_g.fillRect(0,0,m_dimImage.width,m_dimImage.height);
		    m_g.setColor(Color.black);
		   // m_g.drawLine(m_dimImage.width/2,0,m_dimImage.width/2,m_dimImage.height);
		  //  m_r.drawImage(m_image,0,0,null);
		    ImageReady=true;

	
}

	//-------------------------------------------------------------------------
	public void destroy(){
	}

	// diffusion Paint Handler
	//--------------------------------------------------------------------------
	public void paint(Graphics g)
	{
		if (m_image != null){
			g.drawImage(m_image,0,0,null);
		}
	}

	//--------------------------------------------------------------------------
	public void update(Graphics g)
	{
		ResizeImage();
		paint(g);
		for (int i=0; i<NUM_PARTICLE; i++){
			p[i].DrawParticle(g);
		}
	}

	//--------------------------------------------------------------------------
	private void ResizeImage(){
		Dimension dim = size();
		int nWidth = dim.width;
		int nHeight = dim.height;

		if (m_dimImage != null &&
			m_dimImage.width == nWidth &&
			m_dimImage.height == nHeight){

			return;
		}

		m_dimImage = new Dimension(nWidth, nHeight);
		m_image = createImage(nWidth, nHeight);
		m_g = m_image.getGraphics();
		Background=true;
		for (int i=0; i<NUM_PARTICLE; i++){
			p[i].NewLimits(new Point(0,0),new Point(nWidth-5,nHeight-5));
		}
	}

	//--------------------------------------------------------------------------
	public void tick(){
		Tick = ++Tick % 2;
		if (Tick == 0) repaint();
	}

	//		The start() method is called when the page containing the applet
	// first appears on the screen. The AppletWizard's initial implementation
	// of this method starts execution of the applet's thread.
	//--------------------------------------------------------------------------
	public void start()
	{
		if (m_diffusion == null)
		{
			m_diffusion = new Thread(this);
			m_watch = new Stopwatch(this);
			m_diffusion.start();
			m_watch.start();

		}
	}
	
	//		The stop() method is called when the page containing the applet is
	// no longer on the screen. The AppletWizard's initial implementation of
	// this method stops execution of the applet's thread.
	//--------------------------------------------------------------------------
	public void stop()
	{
		if (m_diffusion != null)
		{
			m_diffusion.stop();
			m_diffusion = null;
			m_watch.stop();
			m_watch = null;
		}

	}

	//--------------------------------------------------------------------------
	public void run()
	{
		while (true)
		{
			try
			{
				if (m_g != null){
					if (Background){
						m_g.setColor(BG);
						m_g.fillRect(0,0,m_dimImage.width,m_dimImage.height);
						Background=false;
					}
					
					
					
		    if (ResetLocation){
		      int charge=1;
		      for (int i=0; i<NUM_PARTICLE; i++){
			if (i>NUM_PARTICLE/2) charge=-1;
			p[i].Init(charge,a,b,c);
		      }
		      ResetLocation=false;
		    }
					
					
					
					for (int i=0; i<NUM_PARTICLE; i++){
						p[i].MoveParticle(m_g);
					}
					Thread.sleep(50);
				}
			}
			catch (InterruptedException e)
			{
				// TODO: Place exception-handling code here in case an
				//       InterruptedException is thrown by Thread.sleep(),
				//		 meaning that another thread has interrupted this one
				stop();
			}
		}
	}

	// MOUSE SUPPORT:
	//		The mouseDown() method is called if the mouse button is pressed
	// while the mouse cursor is over the applet's portion of the screen.
	//--------------------------------------------------------------------------
	public boolean mouseDown(Event evt, int x, int y)
	{
	    start();
	    
		ResetLocation=true;
		a=new Point(x,y);
		Background=true;
		return true;
	}

	// MOUSE SUPPORT:
	//		The mouseUp() method is called if the mouse button is released
	// while the mouse cursor is over the applet's portion of the screen.
	//--------------------------------------------------------------------------
	public boolean mouseUp(Event evt, int x, int y)
	{
		return true;
	}

	// MOUSE SUPPORT:
	//		The mouseDrag() method is called if the mouse cursor moves over the
	// applet's portion of the screen while the mouse button is being held down.
	//--------------------------------------------------------------------------
	public boolean mouseDrag(Event evt, int x, int y)
	{
		return true;
	}

	// MOUSE SUPPORT:
	//		The mouseMove() method is called if the mouse cursor moves over the
	// applet's portion of the screen and the mouse button isn't being held down.
	//--------------------------------------------------------------------------
	public boolean mouseMove(Event evt, int x, int y)
	{
		// TODO: Place applet mouseMove code here
		return true;
	}

	// MOUSE SUPPORT:
	//		The mouseEnter() method is called if the mouse cursor enters the
	// applet's portion of the screen.
	//--------------------------------------------------------------------------
	public boolean mouseEnter(Event evt, int x, int y)
	{
		
		return true;
	}
	
	
}

