クラス用のConwayのGOLのコピーに取り組んでいますが、GUIのレンダリング時に問題が発生します。
クイックランダウン:GUIはBorderLayoutに設定されたフレームとmainPanelを作成します。
グリッド自体をインスタンス化してmainPanelに割り当てると、グリッドに2D配列が表示されますが、表示されません。過去2時間、壁に頭をぶつけてきました。
FWIW、これに基づいてGUIを構築するためにIDEを使用することはできません。以下のコード:
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
さらに情報が必要な場合は、私に知らせてください-私の最初の投稿でコードダンプをしたくありませんでした。