//****************************************************************************** // diffusion.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; import java.util.Random; import graph.*; //============================================================================== // Main Class for applet diffusion // //============================================================================== public class Frap6a extends Applet { ThreeDBorder tdb = null; Frap6aCanvasN dc = null; BarChart theChartRed=null; //BarChart theChartBlue=null; Button StartStop=new Button("Start"); Button b_Reset=new Button("Reset"); Button b_Matrix=new Button("Show Matrix"); Button b_Fit=new Button("Fit Curve"); boolean running=false, prun=false, nevr=true, yesdr=true; Panel P_Button, le, panel; Graph2D graph1; GraphFrapCanvas6a Gr; PThread6a t; int i, min, max, PLOT_REAL=0, ERR=0, PLT=0, PLT2=0, is=0, it=0, lines[][]=new int[7][5], xs[]=new int[3000], ys[]= new int[3000], zs[]=new int[3000]; float X1, Y1, SX, SY, SX2, SX2Y, SXY, SX12, SXX2, SX22, a, b, c, d=(float)0.01; public void init(){ dc = new Frap6aCanvasN(this); tdb = new ThreeDBorder(dc,5); theChartRed=new BarChart(Color.blue,"Number Blue Within Circle",150.0,0.0); //theChartBlue=new BarChart(Color.blue,"Number Blue within Circle",50.0,0.0); tdb.inset(); P_Button=new Panel(); P_Button.setLayout(new GridLayout(0,4,0,0)); P_Button.add(StartStop); P_Button.add(b_Matrix); P_Button.add(b_Reset); P_Button.add(b_Fit); le=new Panel(); le.setLayout(new BorderLayout()); le.add("Center",tdb); le.add("South",theChartRed); le.add("North",P_Button); graph1 = new Graph2D(); graph1.drawzero = false; graph1.drawgrid = false; graph1.setDataBackground(new Color(255,230,200)); panel = new Panel(); panel.setLayout( new GridLayout(0,2,0,0) ); panel.add(graph1); setLayout(new BorderLayout()); Gr=new GraphFrapCanvas6a(this); Gr.resize(300,350); add("Center",le); add("East",Gr); Gr.repaint(); t= new PThread6a(this); } 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"); if(prun){ t.suspend(); } }else{ dc.start(); running=true; StartStop.setLabel("Stop"); if(prun){ t.resume(); } } }else if (event.target==b_Reset){ dc.ResetLocation=true; if (prun) { t.suspend(); t.c=0; prun=false; } t.c=0; Gr.data1 = new DataSet(); Gr.Str0=" "; is=0; Gr.repaint(); }else if (event.target==b_Matrix){ dc.Matrix = ! dc.Matrix; }else if (event.target==b_Fit){ SX=0; SY=0; SX2=0; SXY=0; SX12=0; SX2Y=0; SXX2=0; SX22=0; min=100000; max=-10000; if(is<3){ ERR=1; }else{ for (i=0;imax) max=xs[i]; if(xs[i]=0) 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>=0) p[i].m_StdDev=Slow; if (i>175) p[i].Tether=true; // p[i].m_MembraneIndex=0; // if (i<250) p[i].m_MembraneIndex=0; else p[i].m_MembraneIndex=0; } 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; i=0) charge=-1; p[i].Init(charge,Start,UL,LR); //if (i>NUM_PARTICLE/2) p[i].Active=false; if (i>=0){ p[i].m_StdDev=Slow; if (i>=200) p[i].Tether=true; }else p[i].m_StdDev=Fast; p[i].bleached=false; p[i].m_MembraneIndex=0; } ResetLocation=false; } if (!Matrix){ m_g.setColor(BG); m_g.fillRect(0,0,m_dimImage.width,m_dimImage.height); }else Util.wallPaper(this, m_g, m_bg); first=0; NumberIn1=0; // NumberIn2=0; for (int i=0; i (m_dimImage.width - 4)) || (p[i].Location().y < 4) || (p[i].Location().y > (m_dimImage.height-4))){ p[i].bleached = false; p[i].NewLocation(NewEntry()); } if (p[i].Active && !p[i].bleached && m_circ.Inside(p[i].Location())){ if (p[i].m_Charge==-1) NumberIn1++; //else NumberIn2++; if (FrapCount>0) p[i].bleached=true; } 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); ((Frap6a)applet).theChartRed.setFillValue(NumberIn1); //((Frap6a)applet).theChartBlue.setFillValue(NumberIn2); NumberIn2=NumberIn1; if(!((Frap6a)applet).yesdr){ ((Frap6a)applet).yesdr=true; } 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) { ((Frap6a)applet).yesdr=false; //Start=new Point(x,y); //ResetLocation=true; ((Frap6a)applet).dc.NumberIn2=0; if (((Frap6a)applet).nevr){ ((Frap6a)applet).t.start(); ((Frap6a)applet).nevr=false; ((Frap6a)applet).prun=true; }else if (!((Frap6a)applet).prun) { ((Frap6a)applet).t.resume(); ((Frap6a)applet).prun=true; } ((Frap6a)applet).prun=true; ((Frap6a)applet).t.c=0; ((Frap6a)applet).is=0; ((Frap6a)applet).Gr.Str0=" "; ((Frap6a)applet).Gr.data1 = new DataSet(); FrapCount=1; ((Frap6a)applet).xs[((Frap6a)applet).is]=0; ((Frap6a)applet).ys[((Frap6a)applet).is]=((Frap6a)applet).t.c; ((Frap6a)applet).is++; 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; } } class GraphFrapCanvas6a extends G2Dint { Frap6a app1; static final int POSX = 300; static final int NEGX = 15; static final int POSY = 150; static final int NEGY = 8; int a,b,N,x1,x2,y1,y2,ind,i,j; int np=300; double data[] = new double[2*np]; DataSet data1 = new DataSet(); String Str0=" "; String Str1=" "; String Str2=" "; Color C0,C; Color C1 = Color.black; Color C2 = Color.black; int maxe=0; Axis xaxis; Axis yaxis; double xstep; double ystep; public GraphFrapCanvas6a (Applet a){ app1=(Frap6a)a; } public synchronized void paint (Graphics g) { Dimension dm=size(); frame=false; drawzero = false; drawgrid = false; borderRight = 0; borderLeft = 0; borderTop = 0; borderBottom = 0; int x,y,i,j; // ForceSquare(g, new Rectangle(150,150)); square=true; paintAll=true; // attachDataSet(data1); // super.paint(g); // double data[] = new double[2*np]; xstep=(double)dm.width/(double)(POSX+NEGX); ystep=(double)dm.height/(double)(POSY+NEGY); int xax=(int)Math.round(ystep*POSY); int yax=(int)Math.round(xstep*NEGX); g.setColor(Color.blue); g.drawLine(yax,0,yax,dm.height); g.drawLine(0,xax,dm.width,xax); for(i=5;i<(NEGX+POSX+1);i+=10){ x=(int)Math.round(i*xstep); g.drawLine(x,xax-2,x,xax+2); } for(i=0;i<(NEGY+POSY+1);i+=10){ y=(int)Math.round(xax+i*ystep); g.drawLine(yax-2,y,yax+2,y); } for(i=0;i<(POSY+1);i+=10){ y=(int)Math.round(xax-i*ystep); g.drawLine(yax-2,y,yax+2,y); } g.setColor(Color.black); Font f = new Font("TimesRoman",Font.PLAIN,11); g.setFont(f); for(i=10;i<300;i+=30){ g.drawString(" "+i,yax+(int)Math.round(i*xstep)-10,xax+12); } f = new Font("TimesRoman",Font.PLAIN+Font.BOLD,13); g.setColor(Color.blue); g.setFont(f); g.drawString("t",dm.width-13,xax-7); g.drawString("N",yax-12,12); f = new Font("TimesRoman",Font.PLAIN,11); g.setFont(f); g.setColor(Color.black); for(i=150;i>0;i-=10){ g.drawString(" "+i,yax+4,(int)Math.round((POSY-i)*ystep)+5); } /* if(app1.PLOT_AV == 1){ a=10-app1.Particles[app1.Num_Particles.getSelectedIndex()]; b=(int)Math.round(app1.CDiff.total_sum/app1.CDiff.n_runs); app1.xs[app1.is]=a; app1.ys[app1.is]=b; app1.zs[app1.is]=1; app1.is++; app1.PLOT_AV=0; } */ if((app1.PLOT_REAL == 1) && app1.yesdr){ a=app1.t.c; b=app1.dc.NumberIn2; app1.xs[app1.is]=a; app1.ys[app1.is]=b; // app1.zs[app1.is]=app1.Num_Particles.getSelectedIndex(); app1.is++; // g.drawString("Calc",2,30); app1.PLOT_REAL=0; } if(app1.PLT == 1){ if(app1.c>=0) { Str0="F(t)= "+(float)((float)Math.round(app1.a*100)/(float)100.0)+" - "+(float)((float)Math.round(app1.b*100)/(float)100.0)+" /(1 + "+(float)((float)Math.round(app1.d*100)/(float)100.0)+"t) + "+(float)((float)Math.round(app1.c*100)/(float)100.0)+" / (1 + "+(float)((float)Math.round(app1.d*100)/(float)100.0)+"t)^2"; }else{ Str0="F(t)= "+(float)((float)Math.round(app1.a*100)/(float)100.0)+" - "+(float)((float)Math.round(app1.b*100)/(float)100.0)+" /(1 + "+(float)((float)Math.round(app1.d*100)/(float)100.0)+"t) "+(float)((float)Math.round(app1.c*100)/(float)100.0)+" / (1 + "+(float)((float)Math.round(app1.d*100)/(float)100.0)+"t)^2"; } // np=300; // maxe=-10; xaxis = createXAxis(); yaxis = createYAxis(); data1 = new DataSet(); xaxis.attachDataSet(data1); yaxis.attachDataSet(data1); attachDataSet(data1); // data1.deleteData(); for(i=j=0; i maxe){ // maxe=(int)data[j+1]; // } } maxe=0; for(i=app1.is-app1.is/5; imaxe){ // maxe=(int)Math.round(b*ystep); // } // } // maxe = (int)Math.round((app1.a-app1.b/(1+app1.d*500)+app1.c/((1+app1.d*500)*(1+app1.d*500)))*ystep); // here g.drawString(Str0,dm.width-265,15); // g.drawString(""+maxe,dm.width-265,15); // data1.draw_data(g, new Rectangle(15,350-maxe-30,300,maxe-30+30)); // data1.draw_data(g, new Rectangle(15,150,300,175)); // this.repaint(); for(i=0;i<(app1.is);i++){ a=app1.xs[i]; b=app1.ys[i]; N=4; int hit=0; for(j=i;j>=0;j--){ if (a == app1.xs[j]){ if(b == app1.ys[j]){ if(j != i){ hit++; if(hit == 3){ N++; hit=0; } } } } } g.setColor(Color.black); // g.drawString(Str0,dm.width-265,15); g.setColor(Color.blue); g.fillOval((int)Math.round((NEGX+a)*xstep),(int)Math.round((POSY-b)*ystep),N,N); } data1.draw_data(g, new Rectangle(15,(int)Math.round(xax-maxe*ystep),(int)Math.round((NEGX+app1.t.c-5)*xstep),(int)Math.round(maxe*ystep))); }else{ g.setColor(Color.black); g.drawString("Not enough data",dm.width-95,12); app1.ERR=0; } } } class PThread6a extends Thread{ Frap6a a; public int c=0, d=0; public PThread6a(Applet a){ this.a=(Frap6a)a; } public void run(){ while(true){ try{ sleep(2500); } catch(InterruptedException e) {}; if(a.running){ a.prun=true; if(c<301){ a.PLOT_REAL=1; a.Gr.repaint(); c+=5; } // else{ // a.prun=false; // } } try{ sleep(2500); } catch(InterruptedException e) {}; } } }