title: Java Swing questions regarding GridLayout and paintComponent methods
body: <p>Within this program, we need to create an 8x8 grid of "LifeCell" widgets. The instructor did not mention that the widgets had to be an object of <code>Shape</code> so I went ahead and used the <code>GridLayout</code> class. The <code>GridLayout</code> class works fine (as well as I know, since there is no visual aid to confirm.) The object of the program is to play the Game of Life where a user can click on one of the LifeCell widgets and toggle between states being 'alive' or 'dead.</p>
  
  <p>My question relies heavily on getting the cells to be painted. It could be a problem with my code, but I am not 100% sure. </p>
  
  <h3>Program2.java</h3>
  
  <pre><code>public class Program2 extends JPanel implements ActionListener {
  private LifeCell[][] board; // Board of life cells.
  private JButton next; // Press for next generation.
  private JFrame frame; // The program frame.
  
  public Program2() {
      // The usual boilerplate constructor that pastes the main
      // panel into a frame and displays the frame. It should
      // invoke the "init" method before packing the frame
      frame = new JFrame("LIFECELL!");
      frame.setContentPane(this);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.init();
      frame.pack();
      frame.setVisible(true);
  }
      public void init() {
      // Create the user interface on the main panel. Construct
      // the LifeCell widgets, add them to the panel, and store
      // them in the two-dimensional array "board". Create the
      // "next" button that will show the next generation.
      LifeCell[][] board = new LifeCell[8][8];
      this.setPreferredSize(new Dimension(600, 600));
      this.setBackground(Color.white);
      this.setLayout(new GridLayout(8, 8));
      // here is where I initialize the LifeCell widgets
      for (int u = 0; u &lt; 8; u++) {
          for (int r = 0; r &lt; 8; r++) {
              board[u][r] = new LifeCell(board, u, r);
              this.add(board[u][r]);
              this.setVisible(true);
  
          }
      }
  </code></pre>
  
  <h3>LifeCell.java</h3>
  
  <pre><code> public class LifeCell extends JPanel implements MouseListener {
     private LifeCell[][] board; // A reference to the board array.
     private boolean alive;      // Stores the state of the cell.
     private int row, col;       // Position of the cell on the board.
     private int count;          // Stores number of living neighbors.
  
     public LifeCell(LifeCell[][] b, int r, int c) {
         // Initialize the life cell as dead.  Store the reference
         // to the board array and the board position passed as
         // arguments.  Initialize the neighbor count to zero.
         // Register the cell as listener to its own mouse events.
         this.board = b;
         this.row = r;
         this.col = c;
         this.alive = false;
         this.count = 0;
         addMouseListener(this);
     }   
  </code></pre>
  
  <p>and here is the <code>paintComponent</code> method:</p>
  
  <pre><code>   public void paintComponent(Graphics gr) {
         // Paint the cell.  The cell must be painted differently
         // when alive than when dead, so the user can clearly see
         // the state of the cell.
             Graphics2D g = (Graphics2D) gr;
             super.paintComponent(gr);
             g.setPaint(Color.BLUE);
     }
  </code></pre>
  
  <p>I do not need the exact solution to fix it, but I am at wits end trying to get it to work. </p>
  
  <p>Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>I added more segment of Program2.java class, I can check back tomorrow I am heading off to bed, I appreciate all the help guys.</p>
  
  <p>EDIT #2:</p>
  
  <p>My real confusion gets to when I populate my frame with an 8x8 <code>GridLayout</code> each individual "cell" for lack of better words is of type <code>LifeCell</code>. How can I paint each <code>LifeCell</code> different colors? If that makes any sense at all to you guys, I can try to revise it as much as I can. And camickr, I will look at that website, thank you.</p>
  
  <p>Assignment can be found <a href="http://www.ric.edu/faculty/emcdowell/cs221/program2.txt" rel="nofollow noreferrer">here</a> to avoid any and all confusion regarding my question and/or the code snippet.</p>