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

//==============================================================================
// Main Class for applet diffusion
//
//==============================================================================
public class DiffNoMemb extends Applet {
	ThreeDBorder tdb = null;
	DiffCanvasN dc = null;

	public void init(){
		dc = new DiffCanvasN(this);
		//tdb = new ThreeDBorder(dc,5);
	//	tdb.inset();
		setLayout(new BorderLayout());
		add("Center",dc);
	}

	public Insets insets(){
		return new Insets(5,5,5,5);
	}

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

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



class DiffCanvasN extends Canvas implements Runnable   //, StopwatchClient
{
	// THREAD SUPPORT:
	//		m_diffusion	is the Thread object for the applet
	//--------------------------------------------------------------------------
	Thread	m_diffusion = null;
	Thread	m_watch = null;
	private Applet applet;
	private final int NUM_PARTICLE=200;
	private Particle p[];

	private Graphics m_g,m_r;
	private Image m_image,m_ready,m_bg;
	private Dimension m_dimImage;
	private int Tick=0;
	private Color BG=new Color(153,204,204);
	private Point Start,UL,LR;
	private boolean ResetLocation=false;
	private boolean ImageReady=false;

	public Membrane m_Membrane;

	// diffusion Class Constructor
	//--------------------------------------------------------------------------
	public DiffCanvasN(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";
	}


	// The init() method is called by the AWT when an applet is first loaded or
	// reloaded.  Override this method to perform whatever initialization your
	// applet needs, such as initializing data structures, loading images or
	// fonts, creating frame windows, setting the layout manager, or adding UI
	// components.
    //--------------------------------------------------------------------------
	public void init()
	{
		Dimension dim=size();
		int nWidth=dim.width;
		int nHeight=dim.height;
		Start= new Point(15,15);
		UL=new Point(0,0);
		LR=new Point(nWidth-5,nHeight-5);

		m_Membrane=new Membrane();
		m_Membrane.SetVoltage(0);
		m_Membrane.addElement(0.2);
		m_Membrane.addElement(0.0);


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

		for (int i=0; i<NUM_PARTICLE; i++){
			if (i>NUM_PARTICLE/2) charge=-1;
			p[i]= new Particle(applet,charge,Start,UL,LR);
			p[i].m_StdDev=5.0;
			p[i].BG=getBackground();
			p[i].m_Barrier=nWidth*2;
			p[i].m_Membrane=m_Membrane;
			//p[i].m_MembraneIndex=0;
			if (i<NUM_PARTICLE/2) p[i].m_MembraneIndex=0;
			else p[i].m_MembraneIndex=1;

		}

		ResizeImage();
		//m_bg=applet.getImage(applet.getDocumentBase(),"../images/graphBkgnd.gif");

            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;

	}

	// Place additional applet clean up code here.  destroy() is called when
	// when you applet is terminating and being unloaded.
	//-------------------------------------------------------------------------
	public void destroy(){
	}

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

	//--------------------------------------------------------------------------
	public void update(Graphics g)
	{
		ResizeImage();
		paint(g);
		ImageReady=false;
	}


	//--------------------------------------------------------------------------
	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_ready = createImage(nWidth, nHeight);
		m_g = m_image.getGraphics();
		m_r = m_ready.getGraphics();
		LR=new Point(nWidth-5,nHeight-5);	
		for (int i=0; i<NUM_PARTICLE; i++){
			p[i].NewLimits(UL,LR);
		}
	}



	//--------------------------------------------------------------------------
	public void start()
	{
		if (m_diffusion == null)
		{
			m_diffusion = new Thread(this);
			//m_watch = new Stopwatch(this);
			m_diffusion.start();
			//m_watch.start();
		}
	}
	
	//--------------------------------------------------------------------------
	public void stop()
	{
		if (m_diffusion != null)
		{
			m_diffusion.stop();
			//m_watch.stop();
			m_diffusion = null;
			//m_watch = null;
		}

	}


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

	//--------------------------------------------------------------------------
	public void run()
	{
		while (true)
		{
		  if (m_g != null){
		    if (ResetLocation){
		      int charge=1;
		      for (int i=0; i<NUM_PARTICLE; i++){
			if (i>NUM_PARTICLE/2) charge=-1;
			p[i].Init(charge,Start,UL,LR);
		      }
		      ResetLocation=false;
		    }
		    
		    m_g.setColor(BG);
		    m_g.fillRect(0,0,m_dimImage.width,m_dimImage.height);
		    
		    //Util.wallPaper(this, m_g, m_bg);
		    
		    for (int i=0; i<NUM_PARTICLE; i++){
		      p[i].NewMove();
		      p[i].DrawParticle(m_g);
		    }
		    //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;
		    repaint();
		  }
		}
	}

	// 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();
		Start=new Point(x,y);
		ResetLocation=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)
	{
		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)
	{		//start();
		return true;
	}

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


}

