Skip to content
Snippets Groups Projects
Commit bb19284a authored by AZZOUG Lydia's avatar AZZOUG Lydia
Browse files

Replace GameOfLifeGUI.java

parent 19cf26f8
No related branches found
No related tags found
No related merge requests found
...@@ -11,22 +11,31 @@ public class GameOfLifeGUI extends JFrame { ...@@ -11,22 +11,31 @@ public class GameOfLifeGUI extends JFrame {
private JPanel gridPanel; private JPanel gridPanel;
private JFrame frame; private JFrame frame;
public GameOfLifeGUI(Grid g) { public GameOfLifeGUI(Grid g) {
this.numberOfRows = g.getNumberOfRows(); this.numberOfRows = g.getNumberOfRows();
this.numberOfColumns = g.getNumberOfColumns(); this.numberOfColumns = g.getNumberOfColumns();
gridLayout = new GridLayout(numberOfRows, numberOfColumns); gridLayout = new GridLayout(numberOfRows, numberOfColumns);
gridPanel = new JPanel(gridLayout); gridPanel = new JPanel(gridLayout);
labelGrid = new JLabel[numberOfRows][numberOfColumns]; labelGrid = new JLabel[numberOfRows][numberOfColumns];
for (int x = 0; x < numberOfColumns; x++)
for (int x = 0; x < numberOfColumns; x++){
for (int y = 0; y < numberOfRows; y++){ for (int y = 0; y < numberOfRows; y++){
labelGrid[x][y] = new JLabel("*"); labelGrid[x][y] = new JLabel("*");
JLabel label; JLabel label = labelGrid[x][y];
if(g.getCell(x,y).isAlive()) Cell cell = g.getCell(x,y);
labelGrid[x][y].setForeground(Color.red); if(cell.isAlive()) {
if (cell.isRed())
label.setForeground(Color.red);
else else
labelGrid[x][y].setForeground(Color.white); label.setForeground(Color.blue);
}else{
label.setForeground(Color.white);
}
gridPanel.add(labelGrid[x][y]); gridPanel.add(labelGrid[x][y]);
} }
}
frame = new JFrame("Game of Life"); frame = new JFrame("Game of Life");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(gridPanel); frame.setContentPane(gridPanel);
...@@ -36,13 +45,19 @@ public class GameOfLifeGUI extends JFrame { ...@@ -36,13 +45,19 @@ public class GameOfLifeGUI extends JFrame {
} }
public void update(Grid g){ public void update(Grid g){
for (int x = 0; x < numberOfColumns; x++) for (int x = 0; x < numberOfColumns; x++) {
for (int y = 0; y < numberOfRows; y++) { for (int y = 0; y < numberOfRows; y++) {
JLabel label = labelGrid[x][y]; JLabel label = labelGrid[x][y];
if(g.getCell(x,y).isAlive()) Cell cell = g.getCell(x,y);
if(cell.isAlive()) {
if (cell.isRed())
label.setForeground(Color.red); label.setForeground(Color.red);
else else
label.setForeground(Color.blue);
}else {
label.setForeground(Color.white); label.setForeground(Color.white);
} }
} }
} }
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment