The Game of Life is not your typical computer game. It is a ‘cellular automaton’, and was invented by Cambridge mathematician John Conway.
This game became widely known when it was mentioned in an article published by Scientific American in 1970. It consists of a collection of cells which, based on a few mathematical rules, can live, die or multiply. Depending on the initial conditions, the cells form various patterns throughout the course of the game.
The popular Conway’s Game of Life that we seen all over the internet today mostly written in java applet. But the program below is written in JFrame class. I hope you enjoy it…
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ConwayGUI extends JFrame {
int gen;
int[][] conway = new int[50][50];
String[] speed = {“Slow”, “Fast”, “Hyper”};
javax.swing.Timer timerslow = new javax.swing.Timer(900, new startConway());
javax.swing.Timer timerfast = new javax.swing.Timer(100, new startConway());
javax.swing.Timer timerhyper = new javax.swing.Timer(1, new startConway());
javax.swing.Timer timerStart = new javax.swing.Timer(1, new mula());
public ConwayGUI () {
GUIComponent();
}
private void GUIComponent() {
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle(“Game Of Life”);
this.setResizable(false);
JP1 = new JPanel();
JP1.setLayout(new GridLayout(50, 50));
arrButton = new JButton[50][50];
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
final int m = i;
final int n = j;
JP1.add(arrButton[i][j] = new JButton(“”));
arrButton[i][j].setPreferredSize(new java.awt.Dimension(12, 12));
arrButton[i][j].setBackground(new java.awt.Color(128, 128, 128));
arrButton[i][j].addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
arrBtnActionPerformed(evt, arrButton[m][n]);
}
});
}
}
add(BorderLayout.CENTER, JP1);
java.awt.GridBagConstraints gridBagConstraints;
JP2 = new JPanel();
JP2.setLayout(new GridBagLayout());
clearBtn = new javax.swing.JButton();
nextBtn = new javax.swing.JButton();
stopBtn = new javax.swing.JButton();
startBtn = new javax.swing.JButton();
speedLabel = new javax.swing.JLabel();
speedCombo = new javax.swing.JComboBox();
genLabel = new javax.swing.JLabel();
genField = new javax.swing.JTextField();
clearBtn.setText(“Clear”);
clearBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = 5;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 16);
JP2.add(clearBtn, gridBagConstraints);
nextBtn.setText(“Next”);
nextBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 16);
JP2.add(nextBtn, gridBagConstraints);
startBtn.setText(“Start”);
startBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startBtnActionPerformed(evt);
}
});
JP2.add(startBtn, new java.awt.GridBagConstraints());
speedLabel.setText(“Speed:”);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = 3;
gridBagConstraints.insets = new java.awt.Insets(0, 21, 0, 0);
JP2.add(speedLabel, gridBagConstraints);
speedCombo.setModel(new javax.swing.DefaultComboBoxModel(speed));
speedCombo.setSelectedItem(speed[0]);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = 4;
gridBagConstraints.insets = new java.awt.Insets(0, 11, 0, 0);
JP2.add(speedCombo, gridBagConstraints);
genLabel.setText(“Generation:”);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(0, 16, 0, 5);
JP2.add(genLabel, gridBagConstraints);
genField.setColumns(7);
genField.setEditable(false);
JP2.add(genField, new java.awt.GridBagConstraints());
add(BorderLayout.SOUTH, JP2);
pack();
}
private void clearBtnActionPerformed(java.awt.event.ActionEvent evt) {
genField.setText(“”);
gen = 0;
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
arrButton[i][j].setBackground(new java.awt.Color(128, 128, 128));
}
}
}
private void nextBtnActionPerformed(java.awt.event.ActionEvent evt) {
neighbor();
}
private void startBtnActionPerformed(java.awt.event.ActionEvent evt) {
String str = startBtn.getText();
if (str.equalsIgnoreCase(“Start”)) {
timerStart.start();
startBtn.setText(“Stop”);
clearBtn.setEnabled(false);
nextBtn.setEnabled(false);
} else {
timerStart.stop();
timerslow.stop();
timerfast.stop();
timerhyper.stop();
startBtn.setText(“Start”);
clearBtn.setEnabled(true);
nextBtn.setEnabled(true);
}
}
private void set_arr() {
String str = “java.awt.Color[r=255,g=255,b=0]“;
for (int i = 0; i < conway.length; i++) {
for (int j = 0; j < conway[i].length; j++) {
String s = “” + arrButton[i][j].getBackground();
if (s.equalsIgnoreCase(str)) {
conway[i][j] = 1;
} else {
conway[i][j] = 0;
}
}
}
}
private void neighbor() {
set_arr();
gen++;
genField.setText(“” + gen);
for (int i = 0; i < arrButton.length; i++) {
for (int j = 0; j < arrButton[i].length; j++) {
int bil = cal_Neighbor(conway, i, j);
String str = “java.awt.Color[r=255,g=255,b=0]“;
String kosong = “java.awt.Color[r=128,g=128,b=128]“;
String s = “” + arrButton[i][j].getBackground();
if (s.equalsIgnoreCase(str)) {
if (bil == 2 || bil == 3) {
arrButton[i][j].setBackground(new java.awt.Color(255, 255, 0));
} else if (bil < 2 || bil > 3) {
arrButton[i][j].setBackground(new java.awt.Color(128, 128, 128));
}
}
if (s.equalsIgnoreCase(kosong)) {
if (bil == 3) {
arrButton[i][j].setBackground(new java.awt.Color(255, 255, 0));
}
}
}
}
}
private int cal_Neighbor(int[][] arr, int num, int i) {
int total = 0;
if ((num – 1 != -1) && (i – 1 != -1)) {
if (arr[num - 1][i - 1] == 1) {
total += 1;
}
}
if (num – 1 != -1) {
if (arr[num - 1][i] == 1) {
total += 1;
}
}
if ((num – 1 != -1) && (i + 1 < arr[num].length)) {
if (arr[num - 1][i + 1] == 1) {
total += 1;
}
}
if (i – 1 != -1) {
if (arr[num][i - 1] == 1) {
total += 1;
}
}
if (i + 1 < arr[num].length) {
if (arr[num][i + 1] == 1) {
total += 1;
}
}
if ((num + 1 < arr.length) && (i – 1 != -1)) {
if (arr[num + 1][i - 1] == 1) {
total += 1;
}
}
if ((num + 1 < arr.length)) {
if (arr[num + 1][i] == 1) {
total += 1;
}
}
if ((num + 1 < arr.length) && (i + 1 < arr[num].length)) {
if (arr[num + 1][i + 1] == 1) {
total += 1;
}
}
return total;
}
private void arrBtnActionPerformed(java.awt.event.ActionEvent evt, JButton arrayBtn) {
String str = “java.awt.Color[r=255,g=255,b=0]“;
String s = “” + arrayBtn.getBackground();
if (s.equalsIgnoreCase(str)) {
arrayBtn.setBackground(new java.awt.Color(128, 128, 128));
} else {
arrayBtn.setBackground(new java.awt.Color(255, 255, 0));
}
}
class startConway implements ActionListener {
public void actionPerformed(ActionEvent e) {
neighbor();
}
}
class mula implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
if (speedCombo.getSelectedIndex() == 0) {
timerhyper.stop();
timerfast.stop();
timerslow.start();
} else if (speedCombo.getSelectedIndex() == 1) {
timerhyper.stop();
timerslow.stop();
timerfast.start();
} else if (speedCombo.getSelectedIndex() == 2) {
timerslow.stop();
timerfast.stop();
timerhyper.start();
}
} catch (Exception ew) {
}
}
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
ConwayGUI showWin = new ConwayGUI ();
showWin.setVisible(true);
}
});
}
private JButton[][] arrButton;
private JPanel JP1;
private JPanel JP2;
private JButton clearBtn;
private JButton nextBtn;
private JButton stopBtn;
private JButton startBtn;
private JComboBox speedCombo;
private JLabel speedLabel;
private JTextField genField;
private JLabel genLabel;
}