I am currently trying to set up a Mini version of the Game of Life, but I'm struggling to get it visualised in a frame, using an array to produce a grid.
This is my first time using interfaces, so I've decided to protray the grid and array through a modified bar graph. However, I keep getting an error with the JComponent when I try and place the graph/grid into the frame, with a method called paintComponent being called. I haven't created Graphic g earlier, although I did import JFrame, JComponent and Graphic into the main body.
Main Body
JFrame frame = new JFrame();
frame.setSize(400,200);
frame.setTitle("Mini Game of Life");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int t = 0; t < 30; t++)
{
grid = LifeSweeper(grid, x, y); //Line 104
grid = next;
// Line below main Error
JComponent component = paintComponent(Graphics g; x; y; int grid[][]);
frame.add(component);
frame.setVisible(true);
}
paintComponent Method
public void paintComponent(Graphics g, int x, int y, int grid[][])
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (grid[i][j] == 1)
{
g.fillRect(x, y, 10, 10);
}
}
}
}
The plan was for the game of life to run inside a for loop which would update the frame graphic, with grid being the current version of the game array, x and y being the grid size and next being the returned array from the LifeSweeper Method. paintComponent would then generate the graphic and place it within the frame, updating within the for loop. However, the JComponent keeps returning an error, stating that the line isn't a statement and how there are missing semi colons. I imagine there is an easier way around this, but I can't find it in the javadoc or other pages.
EDIT: I have tried to run the program as it is, here is the current error/exception message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at lifegame.LifeGame.LifeSweeper(LifeGame.java:129)
at lifegame.LifeGame.main(LifeGame.java:104)
EDIT 2: The LifeSweeper Method added as requested. Notes have not been added yet, but it runs two series of nested for loops, the first to check and count the neighbouring cells, the second to change the cells according to the rules of life.
public static int[][] LifeSweeper(int[][] next, int a, int b)
{
int cellFate[][] = new int[a][b];
int cellsStatus[][] = new int [a][b];
for (int x = 1; x < next.length-1; x++)
{
for (int y = 1; y < next[0].length-1; y++)
{
int cellCounter = 0;
int cellStatus = 0;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; i++)
{
if (next[i][j] == 1) // Line 129
{
cellCounter++;
}
else if (next[0][0] == 1)
{
cellStatus = 1;
}
}
}
cellFate[x][y] = cellCounter - 1;
cellsStatus[x][y] = cellStatus;
}
}
for (int x = 1; x < next.length-1; x++)
{
for (int y = 1; y < next[0].length-1; y++)
{
if (cellsStatus[x][y] == 0)
{
if (cellFate[x][y] == 0)
{
next[x][y] = 0;
}
if (cellFate[x][y] == 3)
{
next[x][y] = 1;
}
}
if (cellsStatus[x][y] == 1)
{
if (cellFate[x][y] < 2)
{
next[x][y] = 0;
}
if (cellFate[x][y] == 2 || cellFate[x][y] == 3)
{
next[x][y] = 1;
}
if (cellFate[x][y] > 3)
{
next[x][y] = 0;
}
}
}
}
return next;
}