/*
 *
 * Particle
 *
 */
import java.awt.*;
import java.applet.*;
import java.util.Random;

public class Particle 
{
	protected Applet m_Parent;
	protected Point m_CurrentLocation;
	protected Point m_MinLimit;
	protected Point m_MaxLimit;
	protected Point m_OldLoc;
	
	public int m_Barrier=10000;
	public int m_MembraneIndex=0;
	public Membrane m_Membrane=null;

	public int m_Charge;
	protected static Random m_r=new Random();
	public double m_StdDev=1.0;
	protected Color m_neg, m_pos, m_neut;
	public Color BG;
	public Image i_cation,i_anion,i_neutral;

	//--------------------------------------------------------------------------
	public Particle(){

	}

	//--------------------------------------------------------------------------
	public Particle(Applet Parent){
		m_Parent=Parent;
	}
	
	//--------------------------------------------------------------------------
	public Particle(Applet Parent, int charge, Point P, Point Min, Point Max){
		m_Parent=Parent;
		Init(charge,P,Min,Max);
	}

	//--------------------------------------------------------------------------
	public void Init(int charge, Point P, Point Min, Point Max){
		m_Charge=charge;
		m_CurrentLocation= new Point(P.x,P.y);
		m_MinLimit= new Point(Min.x,Min.y);
		m_MaxLimit= new Point(Max.x,Max.y);
		m_neg=new Color(0,0,255);
		m_pos=new Color(255,0,0);
		m_neut=new Color(0,255,0);
		m_OldLoc=new Point(P.x,P.y);
		i_cation = m_Parent.getImage(m_Parent.getDocumentBase(),"images/cation.gif");
		i_anion = m_Parent.getImage(m_Parent.getDocumentBase(),"images/anion.gif");
		i_neutral = m_Parent.getImage(m_Parent.getDocumentBase(),"images/neutral0.gif");


	}

	public void NewLimits(Point Min, Point Max) {
		m_MinLimit.x=Min.x; m_MinLimit.y=Min.y;
		m_MaxLimit.x=Max.x; m_MaxLimit.y=Max.y;
	}

	//--------------------------------------------------------------------------
	public Point Location(){
		return m_CurrentLocation;
	}


	public boolean IsInside(){
		if (m_Membrane==null) return false;
		if (m_CurrentLocation.x<= m_Barrier) return true;
		return false;
	}

	//--------------------------------------------------------------------------
	public void BarrierCross(){
		if (m_Membrane==null) return;
		if ( (m_OldLoc.x<=m_Barrier && m_CurrentLocation.x>m_Barrier) ){
			if (!m_Membrane.Permeate(m_MembraneIndex,m_Charge,true)){	  //from Inside
				m_CurrentLocation.x=m_OldLoc.x;
			}
		}else if ( (m_OldLoc.x>m_Barrier && m_CurrentLocation.x<=m_Barrier) ) {
			if (!m_Membrane.Permeate(m_MembraneIndex,m_Charge,false)){
				m_CurrentLocation.x=m_OldLoc.x;
			}
		}
	}
	
	//--------------------------------------------------------------------------
	public void XBounce(){
		if (m_CurrentLocation.x < m_MinLimit.x){
			m_CurrentLocation.x += 2*(m_MinLimit.x-m_CurrentLocation.x);
		} else if (m_CurrentLocation.x > m_MaxLimit.x) {
			m_CurrentLocation.x += 2*(m_MaxLimit.x-m_CurrentLocation.x);
		}
	}

	//--------------------------------------------------------------------------
	public void YBounce(){
		if (m_CurrentLocation.y < m_MinLimit.y){
			m_CurrentLocation.y += 2*(m_MinLimit.y-m_CurrentLocation.y);
		} else if (m_CurrentLocation.y > m_MaxLimit.y) {
			m_CurrentLocation.y += 2*(m_MaxLimit.y-m_CurrentLocation.y);
		}
	}
	//--------------------------------------------------------------------------
	public void NewMove(){
		m_OldLoc.x=m_CurrentLocation.x;
		m_OldLoc.y=m_CurrentLocation.y;
		m_CurrentLocation.x += (int)(m_r.nextGaussian()*m_StdDev);
		m_CurrentLocation.y += (int)(m_r.nextGaussian()*m_StdDev);
		XBounce();
		YBounce();
		BarrierCross();
	}

	//--------------------------------------------------------------------------
	public void DrawParticle(Graphics g){
		Image i;
		if (m_Charge>0) i=i_cation;
		else if (m_Charge==0) i=i_neutral;
		else i=i_anion;
		g.drawImage(i,m_CurrentLocation.x,m_CurrentLocation.y,null);
	}

	
	//--------------------------------------------------------------------------
	public void MoveParticle(Graphics g){
		Color c;
		if (m_Charge>0) c=m_pos;
		else if (m_Charge==0) c=m_neut;
		else c=m_neg;
		g.setColor(c);
		
		NewMove();
		g.drawLine(m_OldLoc.x,m_OldLoc.y,m_CurrentLocation.x,m_CurrentLocation.y);
	}




}


