import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class SpreadOfStimulus extends Applet implements Runnable,MouseListener,MouseMotionListener { Image offscreen; Graphics g; boolean[][] map,trace; boolean spreading=false; public void init() { setBackground(Color.white); offscreen=createImage(256,256); g=offscreen.getGraphics(); addMouseListener(this); addMouseMotionListener(this); map=new boolean[64][64]; trace=new boolean[64][64]; for (int x=0;x<64;x++) for (int y=0;y<64;y++) { map[x][y]=false; trace[x][y]=false; } } public synchronized void run() { while(spreading) { boolean[][] offspring=new boolean[64][64]; for (int x=0;x<64;x++) for (int y=0;y<64;y++) { if (map[x][y]) g.setColor(Color.black); else if (trace[x][y]) g.setColor(Color.gray); else g.setColor(Color.white); g.fillRect(x*4,y*4,4,4); offspring[x][y]=( !map[x][y] && !trace[x][y] && (map[(x+63)%64][y] || map[(x+1)%64][y] || map[x][(y+63)%64] || map[x][(y+1)%64]) ); } trace=map; map=offspring; repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } } } public void update(Graphics g) { g.drawImage(offscreen,0,0,this); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) { spreading=false; try { map[e.getX()/4][e.getY()/4]=true; } catch (ArrayIndexOutOfBoundsException ex) { } } public void mouseReleased(MouseEvent e) { synchronized(this) { spreading=true; new Thread(this).start(); } } public void mouseDragged(MouseEvent e) { try { map[e.getX()/4][e.getY()/4]=true; } catch (ArrayIndexOutOfBoundsException ex) { } } public void mouseMoved(MouseEvent e) {} }