Hello, I am trying to develop simple Conway's game of life and I need to import a file into the grid that I will display as GUI window then following the game's rules I need to change the cells position in order to have the next generation.

However I cannot display my initial grid and the next generation into the GUI window, I am only able to link an ActionListener button to perform new generation only on console.

This is the Component method:


class LifeComponent extends JComponent
{
private LifeGrid life;
private int squareSize;

public LifeComponent(LifeGrid life, int squareSize)
{
this.life = life; this.squareSize = squareSize;

setPreferredSize(new Dimension(life.getHeight() * squareSize, life.getWidth() * squareSize));
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);

for(int x = 0; x < life.getHeight(); x++)
for(int y = 0; y < life.getWidth(); y++)
{
if(life.getCell(x,y) == 1)
g.fillRect(x * squareSize, y * squareSize, squareSize, squareSize);
}
}
}

And here my two methods to write the grid and start the next generation:


class WriteListener implements ActionListener
{
LifeFrame f;
LifeGrid l;
PrintWriter outFile;

WriteListener(LifeGrid l, LifeFrame f, PrintWriter outFile)
{ this.l = l; this.f = f; this.outFile = outFile; }

public void actionPerformed(ActionEvent e)
{
for(int y=0; y < l.getWidth(); y++)
{
for(int x=0; x < l.getHeight(); x++)
{
if(l.getCell(x,y) == 1)
outFile.print("*");
else
outFile.print(" ");
}
System.out.println();
}
outFile.close();
f.repaint();
}
}

class StartGeneration implements ActionListener
{
LifeFrame f;
LifeGrid l;

StartGeneration(LifeGrid l, LifeFrame f)
{ this.l = l; this.f = f; }

public void actionPerformed(ActionEvent e)
{
l.run();
f.repaint();
}
}

If you need to look at the run() method or other part of the program please ask.