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.