title: Java Game of Life using Swing
body: <p>I am trying to make a replica of the Game of Life using swing, I admit i have used code from some 1 else as i am trying to get my head around it and then proceed with my own implementation. I have some understanding of their code, yet i wanted to implements 2 additional features to their code. However i am finding that the way it is written is posing problems as i wanted to add a MouseListener(To make a cell come to life when clicked) and WindowListener(To make a start,pause and resume button).</p>
  
  <p>I do understand how they work to some extent, yet i need your help to get my headaround it.</p>
  
  <p>Here is the code: </p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.beans.Transient;
  import java.util.Random;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  
  @SuppressWarnings("serial")
  public class ConwaysGameOfLife extends JPanel implements MouseListener{
  
      private int[][] cellsGrid; // grid is the size of the 2d array
      private static final Random rnd = new Random(); // make a new random generator
      private int generationCounter; // counter for the generation
  
      public ConwaysGameOfLife(int width, int height) {
          this.cellsGrid = new int[width / 4][height / 4];// divides by 4 whatever the width and height set is 
          setupGrid();
      }// new method for creating the game with input sizes for the size of the game window
  
      /*The grid consists fully of cells, the grid size is divided by 4 to make the cells
       * setupGrid makes the grid of cells
       * 
       * */
      private void setupGrid() {
          for (int[] row : cellsGrid) {
              for (int j = 0; j &lt; row.length; j++) {
                  if (rnd.nextDouble() &lt; 0.92)
                      continue;
                  row[j] = rnd.nextInt(2);
                  //
              }
          }
      }
      /*
       * applies the rule to the existing cells changing their state depending on the position to neighbors set in the rules
       * */
      public void updateGrid() {
          for (int i = 0; i &lt; cellsGrid.length; i++) {
              for (int j = 0; j &lt; cellsGrid[i].length; j++) {
                  applyRule(i, j);
              }
          }
      }
  
      // Rules of game of life cells iterations
      private void applyRule(int i, int j) {
          int left = 0, right = 0, up = 0, down = 0;
          int dUpperLeft = 0, dUpperRight = 0, dLowerLeft = 0, dLowerRight = 0;
          //this shows the 8 possible neighbors in terms of position
  
          if (j &lt; cellsGrid.length - 1) {
              right = cellsGrid[i][j + 1];
              if(i&gt;0)
                  dUpperRight = cellsGrid[i - 1][j + 1];
              if (i &lt; cellsGrid.length - 1)
                  dLowerRight = cellsGrid[i + 1][j + 1];
          }
  
          if (j &gt; 0) {
              left = cellsGrid[i][j - 1];
              if (i &gt; 0)
                  dUpperLeft = cellsGrid[i - 1][j - 1];
              if (i&lt; cellsGrid.length-1)
                  dLowerLeft = cellsGrid[i + 1][j - 1];
          }
  
          if (i &gt; 0)
              up = cellsGrid[i - 1][j];
          if (i &lt; cellsGrid.length - 1)
              down = cellsGrid[i + 1][j];
  
          int sum = left + right + up + down + dUpperLeft + dUpperRight
                  + dLowerLeft
                  + dLowerRight;
  
          if (cellsGrid[i][j] == 1) {
              if (sum &lt; 2)
                  cellsGrid[i][j] = 0;
              if (sum &gt; 3)
                  cellsGrid[i][j] = 0;
          }
  
          else {
              if (sum == 3)
                  cellsGrid[i][j] = 1;
          }
  
      }
  
      @Override
      @Transient
      public Dimension getPreferredSize() {
          return new Dimension(cellsGrid.length * 4, cellsGrid[0].length * 4);
      }
  
      @Override
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Color gColor = g.getColor();
  
          g.drawString("Generation: " + generationCounter++, 0, 10);
          for (int i = 0; i &lt; cellsGrid.length; i++) {
              for (int j = 0; j &lt; cellsGrid[i].length; j++) {
                  if (cellsGrid[i][j] == 1) {
                      g.setColor(Color.black); // change colour
                      g.fillRect(j * 8, i * 8, 8, 8); // change size of cells
                  }
              }
          }
  
          g.setColor(gColor);
          //paint the cells to a colour
      }
  
      public static void main(String[] args) {
          final ConwaysGameOfLife c = new ConwaysGameOfLife(800, 800);
          JFrame frame = new JFrame();
          frame.getContentPane().add(c);
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
  
          JButton start=new JButton("START");
          /* This method specifies the location and size
           * of button. In method setBounds(x, y, width, height)
           * x,y) are cordinates from the top left 
           * corner and remaining two arguments are the width
           * and height of the button.
           */
          start.setBounds(80,0,80,20);
  
          //Adding button onto the frame
          //frame.add(start);
  
          new Timer(100, new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  frame.add(start);
                  c.updateGrid();
                  c.repaint();
              }
          }).start();
      }
  
  
      @Override
      public void mouseClicked(MouseEvent e) {
          // TODO Auto-generated method stub
  
  
  
      }
  
      @Override
      public void mouseEntered(MouseEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void mouseExited(MouseEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void mousePressed(MouseEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void mouseReleased(MouseEvent e) {
          // TODO Auto-generated method stub
  
      }
  }
  </code></pre>
  
  <p>First of all, The button flickers, and only appears when hovered over with the mouse, still flickering. Then i want the iterations to begin after start button is pressed, and pause button to pause it, and resume(Have a decent idea how it would work, but not how to implement it with the structure of swing that is done in this code.)</p>
  
  <p>Secondly, I wanted the cells to come to life when they are pressed with the mouse, But, i am unsure how to implement the mouseListener to do this.</p>
  
  <p>I tried something like cellsGrid[i][j] = 1; when clicked by mouse but i get errors, which is due to my lack of understanding of the implementation of cellsGrid.</p>
  
  <p>I am not expecting solutions to the problem, I would like some guidance to understand the Listeners better and maybe how to make this simpler to understand for me. Thank You :)</p>