public void actionPerformed(ActionEvent ae) { if (ae.getSource() == this.buttonStart && !animate) { animate = true; animatorThread = new Thread(animator); animatorThread.start(); } if (ae.getSource() == this.buttonStop && animate) { animate = false; animatorThread = null; } if (ae.getSource() == this.buttonNextRound && !animate) { board.nextRound(); numOfGenerations++; grid.setBoard(board.getCurrentRound()); grid.repaint(); generationsLabel.setText("Generation " + numOfGenerations); } if (ae.getSource() == this.buttonClearBoard && !animate) { board.clear(); this.numOfGenerations = 0; grid.repaint(); generationsLabel.setText("Generation " + numOfGenerations); } if (ae.getSource() == this.drawingCheckBox) { draw = drawingCheckBox.isSelected(); } }
@Override public void run() { while (animate) { board.nextRound(); grid.setBoard(board.getCurrentRound()); numOfGenerations++; grid.repaint(); generationsLabel.setText("Generation " + numOfGenerations); try { Thread.sleep(250); } catch (InterruptedException e) { e.printStackTrace(); } } }
@Override public void mouseReleased(MouseEvent e) { if (animate || e.getButton() != MouseEvent.BUTTON1) { return; } int row = e.getY() / 10; int column = e.getX() / 10; if (row >= board.getNumOfRows() || column >= board.getNumOfColumns()) { return; } toggleCell(new Position(column, row)); numOfGenerations = 0; grid.repaint(); generationsLabel.setText("Generation " + numOfGenerations); }