/* applet No. 1018 * * ink diffusion spread - Monte Carlo Method * - multi thread / Ball class implement * * Created by Ikeuchi Mitsuru on September 16 2006. * Copyright (c) 2006-2007 Ikeuchi Mitsuru. All rights reserved. * * ver 0.0.1 2006.09.16 created * ver 0.0.2 2007.06.08 improved code * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class inkMultiThread extends Applet implements ItemListener, ActionListener, AdjustmentListener, Runnable { // ------------------------------------ preset field ----------- Thread th = null; // for run()-paint() thread Walkers wkr[] = new Walkers[16]; // for event Choice ch_thread; Button bt_reset, bt_startStop, bt_step; Scrollbar sc_var; // for off-paint buffers Dimension dim; Image imgOff; Graphics gOff; // ink particles random work int height; int width; double t = 0.0; double dt = 0.001; int resetSW = 0; int started = 1; int stepSW = 0; int threadMode = 1; double scrVar = 50.0; int NNa = 1600; Balls ball[] = new Balls[2000]; // display int dispWidth = 630; int dispHeight = 420; int sleepTime = 50; // ------------------------------ applet main thread ----------- public void init() { resize(dispWidth,dispHeight); setBackground(Color.white); dim = getSize(); imgOff = createImage(dim.width,dim.height); gOff = imgOff.getGraphics(); ch_thread = new Choice(); ch_thread.add("single"); ch_thread.add("multi"); ch_thread.addItemListener(this); ch_thread.select("multi"); bt_reset= new Button("reset"); bt_reset.addActionListener(this); bt_startStop= new Button("start/stop"); bt_startStop.addActionListener(this); bt_step = new Button("step"); bt_step.addActionListener(this); sc_var= new Scrollbar(Scrollbar.HORIZONTAL,50,10,0,110); sc_var.addAdjustmentListener(this); setLayout(new BorderLayout()); Panel pnl = new Panel(); pnl.setLayout(new GridLayout(1,6,5,0)); /* pnl.add(new Label(" ")); */ pnl.add(bt_reset); pnl.add(bt_startStop); pnl.add(bt_step); pnl.add(new Label(" ")); pnl.add(new Label(" ")); pnl.add(ch_thread); add(pnl,"North"); setInitialCondition(); } public void start() { if (th == null) { th = new Thread(this); th.start(); } } public void stop() { if (th != null) th = null; } // ---------------------------------- event listener ----------- public void itemStateChanged(ItemEvent ev){ if (ev.getSource() == ch_thread) { threadMode = ch_thread.getSelectedIndex(); } } public void actionPerformed(ActionEvent ev){ if(ev.getSource() == bt_reset){ resetSW = 1; } else if (ev.getSource() == bt_startStop){ if (started==0) { started = 1; } else { started = 0; } } else if (ev.getSource() == bt_step){ if (started==0) stepSW = 1; } } public void adjustmentValueChanged(AdjustmentEvent ev){ if(ev.getSource() == sc_var) { scrVar = 1.0*(double)(sc_var.getValue()); } } // ========================= run() - paint() loop ============== public void run() { while (th != null) { timeEvolution(); offPaint(); repaint(); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(imgOff,0,0,this); } // --------------------------------------- off-paint ----------- void offPaint() { gOff.setColor(Color.white); gOff.fillRect(0,0,dim.width,dim.height); gOff.setColor(Color.black); gOff.drawString("t="+(int)(t*1000+0.5)+" (times)",10,40); if (started==0) { gOff.drawString("stopped",dispWidth/6*1+10,40); } else { gOff.drawString("started",dispWidth/6*1+10,40); } gOff.drawString("thread",dispWidth/6*5+10,40); dispWalkers(30, 80); } void dispWalkers(int gx, int gy) { int i,ir,ix,iy; double col; ir = 6; gOff.setColor(Color.gray); gOff.drawRect(gx-ir/2, gy-ir/2, 320+ir, 320+ir); for (i=0; i=4.0) id=3; if (id==0) { xx = (xx+1)%nx; } else if (id==1) { xx = (xx-1+nx)%nx; } else if (id==2) { yy = (yy+1)%ny; } else { yy = (yy-1+ny)%ny; } return(id); } public double ballColor() { double col; col = 0.05+kind/20.0; col = col - (int)(col); return( col ); } } // ======================================== Walkers ============== class Walkers extends Thread { int iBegin; int inn; Balls b[]; Walkers(Balls bll[], int ibgn, int nn) { b = bll; iBegin = ibgn; inn = nn; } public void run() { int i,ib,nk; for (ib=iBegin; ib