// LifeBoard.java import java.awt.*; import java.awt.event.*; public class LifeBoard extends Canvas implements MouseListener, MouseMotionListener { private static final int FINITE = 0, TORUS = 1; private boolean[][] life, tempArray; private boolean changeable = true; private int boardType, width, height, imgHeight, imgWidth, ROWSIZE, COLSIZE; private Image image, offscreenImage; private ActionListener listener = null; public LifeBoard(int _boardType, Image _image, int _ROWSIZE, int _COLSIZE) { boardType = _boardType; image = _image; imgHeight = image.getHeight(this) + 1; imgWidth = image.getWidth(this) + 1; ROWSIZE = _ROWSIZE; COLSIZE = _COLSIZE; life = new boolean[ROWSIZE][COLSIZE]; tempArray = new boolean[ROWSIZE][COLSIZE]; setSize(getPreferredSize()); for(int row = 0; row < ROWSIZE; row++) for(int col = 0; col < COLSIZE; col++) life[row][col] = tempArray[row][col] = false; addMouseListener(this); addMouseMotionListener(this); width = this.getSize().width; height = this.getSize().height; } public Dimension getPreferredSize() { return new Dimension(COLSIZE * imgWidth + 1, ROWSIZE * imgHeight + 1); } public void setPresetConfig(String pattern) { int index = 0, pos; String sub; while (index < pattern.length()) { pos = pattern.indexOf(" ", index); sub = pattern.substring(index, pos); int row = Integer.parseInt(sub); index = pos + 1; pos = pattern.indexOf(" ", index); if (pos == -1) // at end of string, do not specify an endIndex for substring { sub = pattern.substring(index); index = pattern.length(); // end while loop } else { sub = pattern.substring(index, pos); index = pos + 1; } int col = Integer.parseInt(sub); life[row][col] = true; } repaint(); } public void setBoardType(int _boardType) { boardType = _boardType; } public boolean isOneAlive() { int row = 0; boolean done = false; while ((row < ROWSIZE) && (done == false)) { int col = 0; while ((col < COLSIZE) && (done == false)) if (life[row][col]) done = true; else col += 1; row += 1; } return done; } public void setChangeable(boolean canChange) { changeable = canChange; } public void clear() { for(int row = 0; row < ROWSIZE; row++) for(int col = 0; col < COLSIZE; col++) life[row][col] = tempArray[row][col] = false; repaint(); } public void randomize() { for(int row = 0; row < ROWSIZE; row++) for(int col = 0; col < COLSIZE; col++) { int number = (int)(Math.random() * 100); if (number < 20) life[row][col] = true; else life[row][col] = false; } repaint(); } public void makeNextGeneration() { // better to use 2 nearly identical for-loop structures // check boardType once rather than in the loop if (boardType == FINITE) for(int row = 0; row < ROWSIZE; row++) for(int col = 0; col < COLSIZE; col++) { int ct = 0; for(int i = row - 1; i <= row + 1; i++) for(int j = col - 1; j <= col + 1; j++) if (!((i == row) && (j == col))) if (((i != -1) && (j != -1) && (i != ROWSIZE) && (j != COLSIZE))) if (life[i][j] == true) ct += 1; if (life[row][col] == true) if ((ct == 2) || (ct == 3)) tempArray[row][col] = true; else tempArray[row][col] = false; else if (ct == 3) tempArray[row][col] = true; } else if (boardType == TORUS) for(int row = 0; row < ROWSIZE; row++) for(int col = 0; col < COLSIZE; col++) { int ct = 0; for(int i = row - 1; i <= row + 1; i++) for(int j = col - 1; j <= col + 1; j++) if (!((i == row) && (j == col))) { int tempI = i, tempJ = j; if (tempI == -1) tempI = ROWSIZE - 1; else if (tempI == ROWSIZE) tempI = 0; if (tempJ == -1) tempJ = COLSIZE - 1; else if (tempJ == COLSIZE) tempJ = 0; if (life[tempI][tempJ]) ct += 1; } if (life[row][col]) if ((ct == 2) || (ct == 3)) tempArray[row][col] = true; else tempArray[row][col] = false; else if (ct == 3) tempArray[row][col] = true; } for(int row = 0; row < ROWSIZE; row++) for(int col = 0; col < COLSIZE; col++) life[row][col] = tempArray[row][col]; repaint(); } public void mouseEntered(MouseEvent me){} public void mouseExited(MouseEvent me){} public void mousePressed(MouseEvent me) { if (changeable) { int row = me.getY() / imgHeight, col = me.getX() / imgWidth; if (life[row][col]) life[row][col] = false; else life[row][col] = true; repaint(); } } public void mouseClicked(MouseEvent me){} public void mouseReleased(MouseEvent me){} public void mouseDragged(MouseEvent me) { if (changeable) { int row = me.getY() / imgHeight, col = me.getX() / imgWidth; if (((row >= 0) && (row < ROWSIZE)) && ((col >= 0) && (col < COLSIZE))) if (!life[row][col]) { life[row][col] = true; repaint(); } } } public void mouseMoved(MouseEvent me){} public void update(Graphics g) { if (offscreenImage == null) offscreenImage = this.createImage(width, height); Graphics gr = offscreenImage.getGraphics(); paint(gr); g.drawImage(offscreenImage, 0, 0, this); } public void paint(Graphics g) { g.setColor(new Color(22, 107, 29)); g.fillRect(0, 0, width, height); g.setColor(SystemColor.activeCaptionBorder); for(int row = 0; row <= ROWSIZE; row++) g.drawLine(0, row * imgHeight, COLSIZE * imgWidth, row * imgHeight); for(int col = 0; col <= COLSIZE; col++) g.drawLine(col * imgWidth, 0, col * imgWidth, ROWSIZE * imgWidth); for(int row = 0; row < ROWSIZE; row++) for(int col = 0; col < COLSIZE; col++) if (life[row][col]) g.drawImage(image, col * imgWidth + 1, row * imgHeight + 1, this); } }