康威的生命游戏GUI

时间:2012-04-30 10:09:39

标签: java swing jpanel conways-game-of-life

我一直在研究Conway的GOL课程副本,当GUI渲染时我遇到了问题。

快速破解: GUI创建一个Frame和一个mainPanel,设置为BorderLayout。

一旦我实例化Grid本身并将其分配给mainPanel,它应该在Grid中显示我的2D数组,但事实并非如此。在过去的两个小时里,我的头靠在墙上。

FWIW,我无法使用IDE进行GUI构建。代码如下:

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

网格

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

如果您需要更多信息,请告知我们 - 我不想在我的第一篇文章中编译转储。

1 个答案:

答案 0 :(得分:2)

Grid类没有 paintComponent 的方法。使用java图形类中的drawRect()方法的简单嵌套for循环修复了该问题。