/* * * 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,i_bpos,i_bneg; public String c_string="images/cation.gif",a_string="images/anion2.gif",n_string="images/Neutral0.gif",bp_string="images/bcation.gif",bn_string="images/banion.gif"; public boolean Active=true, bouncing=true, bleached=false; public boolean Tether=false; //-------------------------------------------------------------------------- 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.getCodeBase(),c_string); i_anion = m_Parent.getImage(m_Parent.getCodeBase(),a_string); i_neutral = m_Parent.getImage(m_Parent.getCodeBase(),n_string); i_bpos = m_Parent.getImage(m_Parent.getCodeBase(),bp_string); i_bneg = m_Parent.getImage(m_Parent.getCodeBase(),bn_string); Active=true; } 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 void NewLocation(Point New){ m_CurrentLocation=New; m_OldLoc=New; Active=true; } //-------------------------------------------------------------------------- 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){ if (bouncing) m_CurrentLocation.x += 2*(m_MinLimit.x-m_CurrentLocation.x); else Active=false; } else if (m_CurrentLocation.x > m_MaxLimit.x) { if (bouncing) m_CurrentLocation.x += 2*(m_MaxLimit.x-m_CurrentLocation.x); else Active=false; } } //-------------------------------------------------------------------------- public void YBounce(){ if (m_CurrentLocation.y < m_MinLimit.y){ if (bouncing) m_CurrentLocation.y += 2*(m_MinLimit.y-m_CurrentLocation.y); else Active=false; } else if (m_CurrentLocation.y > m_MaxLimit.y) { if (bouncing) m_CurrentLocation.y += 2*(m_MaxLimit.y-m_CurrentLocation.y); else Active=false; } } //-------------------------------------------------------------------------- public void NewMove(){ if (!Active) return; if (Tether){ m_CurrentLocation.x = m_OldLoc.x+(int)(m_r.nextGaussian()*m_StdDev); m_CurrentLocation.y = m_OldLoc.y+(int)(m_r.nextGaussian()*m_StdDev); return; } 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){ if (!Active) return; Image i; if (m_Charge>0){ i=i_cation; if (bleached) i=i_bpos; } else if (m_Charge==0) i=i_neutral; else{ i=i_anion; if (bleached) i=i_bneg; } int x=i.getWidth(null)/2; int y=i.getHeight(null)/2; g.drawImage(i,m_CurrentLocation.x-x,m_CurrentLocation.y-y,null); } //-------------------------------------------------------------------------- public void MoveParticle(Graphics g){ if (!Active) return; 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); } }