I recently coded a program to stimulate Conway's Game of Life. Originally, my program just printed an array of 1's and 0's where the 1's represented the "live" cells, but I tried to teach myself some graphics programming to add graphics to the game. Currently, my program draws a black rectangle to represent a live cell and a white one to represent a dead cell in a grid of rectangles. I am looking to improve my implementation of the graphics and do it in a more organized and traditional way.
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;
public class GameofLife {
static JPanel panel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException{
int [][] array = new int [28][28];
/*
* Set the pattern for Conway's Game of Life by manipulating the array below.
*/
/*Acorn
*/
array[19][15]=1;
array[19][16]=1;
array[19][19]=1;
array[19][20]=1;
array[19][21]=1;
array[18][18]=1;
array[17][16]=1;
panel = new JPanel();
Dimension dim = new Dimension(400, 400);
panel.setPreferredSize(dim);
frame = new JFrame();
frame.setSize(1000, 500);
Container contentPane = frame.getContentPane();
contentPane.add(panel);
frame.setVisible(true);
/*
* Runs the Game of Life simulation "a" number of times.
*/
int[][] end = new int[array.length][array[0].length];
int a = 0;
while (true) {
for (int i = 1; i <= array.length - 2; i++) {
for (int j = 1; j <= array[0].length - 2; j++) {
int counter = surround(array, i, j);
if (array[i][j] == 1 && counter <= 2) {
end[i][j] = 0;
}
if (array[i][j] == 1 && counter == 3) {
end[i][j] = 1;
}
if (array[i][j] == 1 && counter > 4) {
end[i][j] = 0;
}
if (array[i][j] == 0 && counter == 3) {
end[i][j] = 1;
}
if (array[i][j] == 1 && counter == 4) {
end[i][j] = 1;
}
}
}
Graphics g = panel.getGraphics();
Graphics(array, g);
a++;
/*
* printArray(array); System.out.println("");
* System.out.println("");
*/
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
array[i][j] = end[i][j];
end[i][j] = 0;
}
}
Thread.sleep(125);
g.dispose();
}
}
/*
* Checks the cells around a specific cell in the initial array and returns
* the number of live cells surrounding it.
*/
public static int surround(int[][] initial, int i, int j) {
int[][] surrounding = {
{ initial[i - 1][j - 1], initial[i - 1][j],
initial[i - 1][j + 1] },
{ initial[i][j - 1], initial[i][j], initial[i][j + 1] },
{ initial[i + 1][j - 1], initial[i + 1][j],
initial[i + 1][j + 1] } };
int counter = 0;
for (int a = 0; a <= 2; a++) {
for (int b = 0; b <= 2; b++) {
if (surrounding[a][b] == 1) {
counter++;
}
}
}
return counter;
}
/*
* Prints the 2D array.
*/
public static void printArray(int[][] input) {
for (int x = 0; x < input.length; x++) {
for (int y = 0; y < input[0].length; y++) {
System.out.print(input[x][y]);
}
System.out.println("");
}
}
/*
* Creates the graphic for Conway's game of life.
*/
public static void Graphics(int[][] array, Graphics g) {
int BOX_DIM = 10;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
g.drawRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
if (array[i][j] == 0) {
g.setColor(Color.WHITE);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}
}
}
}