import java.applet.Applet; import java.awt.*; import java.awt.image.*; import java.awt.event.*; public class Life extends Applet implements MouseListener, MouseMotionListener, Runnable, ImageObserver { private Point clickPoint = null; private static final int RADIUS = 7; private cellsclass cells; private Thread lifethread; private byte paused = 1; private Image cellpic; public void init() { addMouseListener(this); addMouseMotionListener(this); cells = new cellsclass(getSize().width-140, getSize().height); } public void start() { cellpic = createImage(cells.width,cells.height); if (lifethread == null) { lifethread = new Thread(this, "life"); lifethread.setPriority(Thread.MIN_PRIORITY); lifethread.start(); } } public void run() { Thread myThread = Thread.currentThread(); while (lifethread == myThread) { if(paused == 0) { cells.doframe(); repaint(); } } } public void stop() { lifethread = null; } public void paint(Graphics g) { update(g); } public void update(Graphics g) { int x, y; Graphics g2; g2 = cellpic.getGraphics(); g2.setColor(new Color(0, 0, 96)); g2.fillRect(0,0,cells.width,cells.height); g2.setColor(new Color(0, 192, 0)); for(x = 0; x < cells.width; x++) for(y = 0; y < cells.height; y++) { if(cells.thisframe[x][y] > 0) { g2.drawLine(x,y,x,y); } } g2.dispose(); g.drawImage(cellpic,0,0,new Color(0, 0, 96),this); g.setColor(new Color(0, 0, 0)); g.fillRect(cells.width,0,getSize().width,getSize().height); g.setColor(new Color(255, 255, 255)); g.drawString("Options:", cells.width + 2, 15); g.drawString("No. Touching", cells.width + 2, 30); g.drawString("Action", cells.width + 82, 30); g.drawString("0", cells.width + 2, 45); g.drawString("1", cells.width + 2, 60); g.drawString("2", cells.width + 2, 75); g.drawString("3", cells.width + 2, 90); g.drawString("4", cells.width + 2, 105); g.setColor(new Color(0, 192, 0)); for(y = 0; y < 5; y++) { if(cells.rule[y] == 0) g.drawString("None", cells.width + 82, 45+15*y); if(cells.rule[y] == 1) g.drawString("Reproduce", cells.width + 82, 45+15*y); if(cells.rule[y] == 2) g.drawString("Die", cells.width + 82, 45+15*y); } g.setColor(new Color(255, 255, 255)); if(cells.type == 1) g.drawString("Cell type 1", cells.width + 2, 130); else g.drawString("Cell type 2", cells.width + 2, 130); if(paused == 1) g.drawString("Unpause", cells.width + 2, 150); else g.drawString("Pause", cells.width + 2, 150); } public void mousePressed(MouseEvent event) { int ruleno; clickPoint = event.getPoint(); if(clickPoint.x <= cells.width) cells.thisframe[clickPoint.x][clickPoint.y] = 1; else { if(clickPoint.y > 115 && clickPoint.y < 132) { if(cells.type == 1) cells.type = 2; else cells.type = 1; } if(clickPoint.y > 135 && clickPoint.y < 152) { if(paused == 1) { paused = 0; lifethread.notify(); } else paused = 1; } if(clickPoint.x > (cells.width + 80)) { ruleno = (clickPoint.y - 32)/15; if((ruleno >= 0) && (ruleno <= 4)) { cells.rule[ruleno]++; if(cells.rule[ruleno] > 2) cells.rule[ruleno] = 0; } } } repaint(); } public void mouseDragged(MouseEvent event) { clickPoint = event.getPoint(); if(clickPoint.x <= cells.width && clickPoint.x >= 0 && clickPoint.y <= cells.height && clickPoint.y >= 0) cells.thisframe[clickPoint.x][clickPoint.y] = 1; repaint(); } public void mouseReleased(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mouseClicked(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseMoved(MouseEvent event) {} public boolean imageUpdate( Image img, int infoflags, int x, int y, int width, int height ) { return false; } class cellsclass { public byte[][] thisframe; public byte[][] nextframe; public byte[] rule = new byte[5]; public int width, height, type; public cellsclass(int width, int height) { int x, y; this.width = width; this.height = height; this.type = 1; thisframe = new byte[width][height]; nextframe = new byte[width][height]; rule[0] = 2; rule[1] = 0; rule[2] = 1; rule[3] = 0; rule[4] = 2; for(x = 0; x< width; x++) for(y=0; y< height; y++) { thisframe[x][y] = 0; nextframe[x][y] = 0; } } public void doframe() { int adj, x, y; for(x = 0; x< width; x++) for(y=0; y< height; y++) { nextframe[x][y] = 0; } for(x = 0; x< width; x++) for(y=0; y< height; y++) { if(thisframe[x][y] == 1) { adj = 0; if(x > 0) if(thisframe[x-1][y] == 1) adj++; if(y > 0) if(thisframe[x][y-1] == 1) adj++; if(x < (width - 1)) if(thisframe[x+1][y] == 1) adj++; if(y < (height - 1)) if(thisframe[x][y+1] == 1) adj++; if(rule[adj] == 0) nextframe[x][y] = 1; if(rule[adj] == 1) { if(type == 1) nextframe[x][y] = 1; if(x > 0) nextframe[x-1][y] = 1; if(x < (width - 1)) nextframe[x+1][y] = 1; if(y > 0) nextframe[x][y-1] = 1; if(y < (height - 1)) nextframe[x][y+1] = 1; } } } for(x = 0; x< width; x++) for(y=0; y< height; y++) { thisframe[x][y] = nextframe[x][y]; } } } }