2012-04-30 3 views
4

나는 Conway의 GOL 복사본을 사용하고 있는데, GUI를 렌더링 할 때 문제가있다.Conway의 Life of Game GUI

빠른 실행 : GUI Frame 및 mainPanel을 만들고 BorderLayout으로 설정합니다.

일단 그리드 자체를 인스턴스화하고 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 

더 많은 정보가 필요하면 알려 주시기 바랍니다 - 내 첫 번째 게시물에 덤프를 코딩하고 싶지 않았다.

+1

그리드를 그리는 코드는 어디에 있습니까? 이것은 단지 레이아웃입니다. 자식 컴포넌트에서'setVisible'을 호출하고, 부모 컴포넌트에 연결하기 전에 그렇게합니다. –

+0

그리드가 자체 클래스에 존재합니다 - 그리드 클래스 또는 나머지 GUI 빌드를보고 싶습니까? – hedrick

+0

모델 클래스 인 Grid는 중요하지 않지만이를 시각화하는 코드입니다. 비주얼 코드의 관련 부분을 붙여 넣으십시오. –

답변

2

Grid 클래스에는 paintComponent에 대한 메소드가 없습니다. Java 그래픽 클래스의 drawRect() 메서드를 사용하여 간단한 중첩 for 루프가 문제를 해결했습니다.

+0

답을 표시하여 질문을 열지 마십시오 :-) – streppel

관련 문제