//****************************************************************************** // diffusion.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; import java.util.Random; //import gjt.*; //============================================================================== // Main Class for applet diffusion // //============================================================================== public class Frap4b extends Applet { ThreeDBorder tdb = null; Frap4bCanvasN dc = null; BarChart theChartRed=null; BarChart theChartBlue=null; Button StartStop=new Button("Start"); boolean running=false; Panel P_Chart; public void init(){ dc = new Frap4bCanvasN(this); tdb = new ThreeDBorder(dc,5); theChartRed=new BarChart(Color.red,"Number Red within Circle",100.0,0.0); theChartBlue=new BarChart(Color.blue,"Number Blue within Circle",20.0,0.0); tdb.inset(); P_Chart=new Panel(); P_Chart.setLayout(new BorderLayout()); P_Chart.add("North",theChartRed); P_Chart.add("South",theChartBlue); setLayout(new BorderLayout()); add("Center",tdb); add("South",P_Chart); add("North",StartStop); } public Insets insets(){ return new Insets(5,5,5,5); } public void start(){ dc.init(); //dc.start(); } public void stop(){ dc.stop(); } public boolean action(Event event, Object what){ if(event.target==StartStop){ if (running){ dc.stop(); running=false; StartStop.setLabel("Start"); }else{ dc.start(); running=true; StartStop.setLabel("Stop"); } } return true; } } class Frap4bCanvasN 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=480; private final int CIRC_DIAM=55; private final double Fast=5.0, Slow=2.0; 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=true; private boolean ImageReady=false; private int first=0, density=10, NumberIn1=0, NumberIn2=0; private int FrapCount=0; public Membrane m_Membrane; Circle m_circ; // diffusion Class Constructor //-------------------------------------------------------------------------- public Frap4bCanvasN(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: Frap 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); m_circ=new Circle(dim.width/2, dim.height/2,CIRC_DIAM); int charge=1; p = new Particle[NUM_PARTICLE]; for (int i=0; i399) charge=-1; p[i]= new Particle(applet,charge,Start,UL,LR); p[i].a_string="images/anion2.gif"; p[i].m_StdDev=Fast; p[i].BG=getBackground(); p[i].m_Barrier=nWidth*2; p[i].m_Membrane=m_Membrane; p[i].bouncing=false; if (i>399) p[i].m_StdDev=Slow; if (i>399) p[i].Active=false; //p[i].m_MembraneIndex=0; if (i<400) p[i].m_MembraneIndex=0; else p[i].m_MembraneIndex=1; } ResizeImage(); //m_bg=applet.getImage(applet.getCodeBase(),"images/graphBkgnd.gif"); } // 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; i399) charge=-1; p[i].Init(charge,Start,UL,LR); //if (i>NUM_PARTICLE/2) p[i].Active=false; if (i>399) p[i].m_StdDev=Slow; else p[i].m_StdDev=Fast; } ResetLocation=false; } m_g.setColor(BG); m_g.fillRect(0,0,m_dimImage.width,m_dimImage.height); //Util.wallPaper(this, m_g, m_bg); first=0; NumberIn1=0; NumberIn2=0; for (int i=0; i0) p[i].Active=false; } p[i].DrawParticle(m_g); } if (FrapCount>0){ FrapCount = ++FrapCount%6; m_circ.DrawFilled(m_g,Color.yellow); }else m_circ.Draw(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); ((Frap4b)applet).theChartRed.setFillValue(NumberIn1); ((Frap4b)applet).theChartBlue.setFillValue(NumberIn2); ImageReady=true; //Thread.sleep(1000); repaint(); } // } // 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=new Point(x,y); //ResetLocation=true; FrapCount=1; 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; } }