4

I have been working on a copy of Conway's GOL for class and I'm having an issue when the GUI renders.

Quick rundown: GUI Creates a Frame and a mainPanel, set to BorderLayout.

Once I instantiate the Grid itself and assign it to the mainPanel, it should show my 2D array in Grid, but it doesn't. Been banging my head against the wall for the past 2 hours.

FWIW, I cannot use an IDE for GUI building on this. Code below:

GUI

           import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.*;
import java.util.Observer;
import java.util.Observable;

public class GameOfLifeGUI extends JFrame implements Observer {

    private JPanel mainPanel;
    private JPanel gridPanel;
    private JPanel startPanel;
    private JPanel titlePanel;
    private JButton start;
    private Cell cell;
    private Grid grid;
    private MouseEvent mouseClicked;
    private MouseEvent mouseDragged;
    private MouseEvent mousePressed;
    private MouseEvent mouseRelease;
    private MouseListener mouseListener;

    public GameOfLifeGUI() {
        super("");
        //Create Start Button for startPanel
        JButton start = new JButton("Start");

        //Creates a Grid to add to the panel
        grid = new Grid(75,75);

        //Create JPanels
        mainPanel = new JPanel();
        gridPanel = new JPanel();
        startPanel = new JPanel();
        titlePanel = new JPanel();

        /**
         * Add Grid to gridPanel
         * Modify Grid(int, int) to change size of Grid. Per spec, this grid should always be 75x75
         */

        //Create gridPanel
        gridPanel.setLayout(new GridLayout(75,75));
        gridPanel.setBackground(Color.WHITE);
        gridPanel.add(grid);

        //Set Layout of Panels
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(gridPanel, BorderLayout.CENTER);
        mainPanel.add(startPanel, BorderLayout.SOUTH);
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        //Add Start Button to startPanel
        startPanel.add(start);

        //Creates a window for displaying the GUI
        this.setTitle("Conway's Game of Life");
        this.setSize(1000, 750);
        this.setLocationRelativeTo(null);
        this.add(mainPanel);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }//end Constructor

Grid

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Color;
import javax.swing.JPanel.*;
import java.util.Observer;
import java.util.Arrays;
import java.util.Observable;

public class Grid extends JPanel{

  private Cell[][] grid;
  private int column;
  private int row;

  /**
   * Constructs a Grid of Cells
   * columns is a column of cells
   * rows is a row of cells
   */

  public Grid(int column, int row){
    this.column = column;
    this.row = row;

    // create a grid of cells
    grid = new Cell[row][column];
    for (int r = 0; r < row; r++){
      for (int c = 0; c < column; c++){
        grid[r][c] = new Cell(r,c);
      }
    }
    //Creates a border of cells around grid for edge case handling
    //All cells in this border will be dead and incapable of living
    for (int c = 0; c < column; c++){
        grid[0][c] = new Cell(row, column);
    }
    for (int c = 0; c < column-1; c++){
        grid[row-1][c] = new Cell(row, column);
    }
    for (int r = 0; r < row; r++){
        grid[r][0] = new Cell(row, column);
    }
    for (int r = 0; r < row-1; r++){
        grid[r][column - 1] = new Cell(row, column);
    }
  }//end Constructor

If you need more info, please let me know - didn't want to code dump on my first post.

10
  • 1
    Where's the code that draws the grid? This is just layout. Calling setVisible on a child component, and doing so even before attaching it to the parent, makes no sense. Apr 30, 2012 at 10:12
  • The grid exists in its own class - did you want to see the Grid class or the rest of the GUI build?
    – hedrick
    Apr 30, 2012 at 10:15
  • Grid as a model class is not important, but the code that visualises it. Paste a relevant part of the visual code. Apr 30, 2012 at 10:49
  • The layout looks like it ought to work. What is the question? Apr 30, 2012 at 10:58
  • 1
    @shedri1 : Just a small suggestion, the Class that extends the ~JFrame~ should contain nothing else, prepare your Panels in other class, and at the last, call that class to make the content pane for this JFrame,this way you can easily make it work for JApplet, just in case in future you feel like implementing it as Java Web Start :-)
    – nIcE cOw
    Apr 30, 2012 at 11:53

1 Answer 1

2

The Grid class had no method to paintComponent. A simple nested for loop using the drawRect() method from the java graphics class fixed the issue.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.