2014-12-04 2 views
-1

내 Game of Life 프로젝트를 마무리하는 데 어려움을 겪고 있습니다. 알고리즘과 기능이 모두 있지만 디스플레이가 표시되지 않습니다. 누구든지 도와 주겠니? 미리 감사드립니다.Java - Life of Game - 그래픽 표시에 문제가 있습니다.

클래스 ActiveFrame

import java.awt.*; 
import java.awt.event.*; 

public class ActiveFrame extends Frame { 
//inherits from Frame class, but (Frame doesn't allow user to click out of extra window) 
//Allows user to "x" out of window 

//************ 
//Constructors 
//************ 

public ActiveFrame(){ 
    addWindowListener(new WindowAdapter(){ 
     public void windowClosing(WindowEvent e) 
     { 
      System.exit(0); 
     } 
    } 
    ); 

    setSize(300, 250); 
    setTitle(getClass().getName());} 
} 

클래스 GoLWorldMainCS

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 

import javax.swing.JOptionPane; 



public class GoLWorldMainCS extends ActiveFrame { 

public static void main(String[] args){ 

    boolean cont; 

    GoLWorldCS terrain = new GoLWorldCS(); 

    terrain.setSize(Integer.parseInt(JOptionPane.showInputDialog("Enter rows: ")), 
        Integer.parseInt(JOptionPane.showInputDialog("Enter cols: "))); 
    terrain.setGenerations(Integer.parseInt(JOptionPane.showInputDialog("Enter generations: "))); 

    if (terrain.getRow() > 60 || 
     terrain.getRow() < 1 || 
     terrain.getCol() > 60 || 
     terrain.getCol() < 1 || 
     terrain.getGenerations() < 1 
     ){ 

     System.out.println("Error: Rows and Columns must be 1-60, and Generations must be greater than 0."); 
    } 

    else { 

     terrain.setCellSize((600/terrain.getRow()), (600/terrain.getCol())); 
     terrain.clearGrid(); 

     cont = true; 
     while (cont == true){ 

      terrain.setCells(terrain.getC(), terrain.getR(), 1); 

      terrain.setCells(Integer.parseInt(JOptionPane.showInputDialog("Enter x-coordinate of live cell (999 to stop): ")), 
        Integer.parseInt(JOptionPane.showInputDialog("Enter y-coordinate of live cell (0 to stop): ")), 
        Integer.parseInt(JOptionPane.showInputDialog("Enter live row length (0 to stop)"))); 

      if(terrain.getR() == 999){ 
       cont = false; 
      } 

      else{ 

       if(terrain.getR() >= 0 && terrain.getR() <= terrain.getRow() && 
        terrain.getC() >= 0 && terrain.getC() <= terrain.getCol() && 
        terrain.getL() >= 0 && terrain.getL() <= (terrain.getRow() - terrain.getC() + 1)){ 

        terrain.markAlive(); 
        terrain.showDisplay(); 
       } 

       else{ System.out.println("Entered coordinate out of bounds");} 


      } 
     } 


    } 


    } 
    } 

클래스 GoLWorldCS 있었다

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 

public class GoLWorldCS extends ActiveFrame { 
int maxRow; 
int maxCol; 
int gen; 
int buckets [][] = new int [maxRow][maxCol]; 
int r=0, c=0, l=1; 
int xDist, yDist; 

public GoLWorldCS(){  
} 

public void clearGrid(){ 

    while (r<=maxRow-1){ 
     c=0; 

     while (c<=maxCol-1){ 
      buckets[r][c] = 0; 
      c=c+1; 
     } 

     r=r+1; 
    } 
} 

public void markAlive(){ 
    while (l > 1){ 
     buckets[r][c] = 1; 
     c++; 
     l--; 
    } 
} 

public void showDisplay(Graphics g){ 

    r = 0; 
    c = 0; 
    g.setColor(Color.blue); 

    while (r<=maxRow-1){ 
     c=0; 

     while (c<=maxCol-1){ 
      if (buckets[r][c] != 0) { 
      g.fillOval((c)*xDist + 50, (r)*yDist + 50, xDist, yDist); 
      System.out.println(r + " " + c); 
      } 

      c=c+1; 
     } 

     r=r+1; 
    } 


} 

public void setCellSize(int width, int height){ 
    this.xDist = width; 
    this.yDist= height; 
} 

public void setSize(int maxR, int maxC){ 
    this.maxRow = maxR; 
    this.maxCol = maxC; 
} 

public void setGenerations(int gens){ 
    this.gen = gens; 
} 

public void setCells(int R, int C, int L){ 
    this.r = R; 
    this.c = C; 
    this.l = L; 
} 

public int getRow(){ 
    return maxRow; 
} 

public int getCol(){ 
    return maxCol; 
} 

public int getGenerations(){ 
    return gen; 
} 

public int getR(){ 
    return r; 
} 

public int getC(){ 
    return c; 
} 

public int getL(){ 
    return l; 
} 
private static void main(String[] args) 
{ 
    GoLWorldMainCS aframe = new GoLWorldMainCS(); 

    aframe.setSize(700,700); 
    aframe.setLocation(50,50); 
    aframe.setTitle("Game of Life"); 
    aframe.show(); 
} 
} 
+1

당신은 코드를 통해 걸어? 어디에서 그래픽이 보이기를 기대합니까? 너 뭐 해봤 니? –

+0

아마도 [이 기사] (http://java-articles.info/articles/?p=504)에서 몇 가지 아이디어를 얻을 수 있습니다. –

답변

1

: 여기

내 (3) 클래스와 각 코드입니다 몇 가지 문제 및 불필요한 것들. setSize()Frame으로 재정의 했으므로 프레임 크기를 올바르게 계산하지 못했습니다. ActiveFrame 클래스가 필요하지 않은 경우 기본 닫기 작업을 설정하여 프레임에서 종료 할 수 있습니다. 스윙은 AWT가 다소 비추천이므로 Frame 대신 JFrame을 확장해야합니다. paint() 메서드를 JFrame 무시하고 그 안에 표시 논리를 넣을 수 있습니다. terrain.repaint()을 호출하면 paint() 메서드가 호출됩니다. buckets의 크기가 maxRowmaxCol과 일치하지 않으므로 표시 방법에 NullPointerException이 표시되고 생성자로 이동했습니다.

GoLWorldCS

import java.awt.Color; 
import java.awt.Graphics; 

import javax.swing.JFrame; 

public class GoLWorldCS extends JFrame { 

    private static final long serialVersionUID = -2833280938880400310L; 
    int maxRow; 
    int maxCol; 
    int gen; 
    int buckets[][]; 
    int r = 0, c = 0, l = 1; 
    int xDist, yDist; 

    public GoLWorldCS(int maxRow, int maxCol) { 
     this.maxRow = maxRow; 
     this.maxCol = maxCol; 
     this.buckets = new int[maxRow][maxCol]; 
    } 

    public void clearGrid() { 
     while (r <= maxRow - 1) { 
      c = 0; 
      while (c <= maxCol - 1) { 
       buckets[r][c] = 0; 
       c = c + 1; 
      } 
      r = r + 1; 
     } 
    } 

    public void markAlive() { 
     while (l > 1) { 
      buckets[r][c] = 1; 
      c++; 
      l--; 
     } 
    } 

    @Override 
    public void paint(Graphics g) { 
     super.paint(g); 
     r = 0; 
     c = 0; 
     g.setColor(Color.blue); 

     while (r <= maxRow - 1) { 
      c = 0; 
      while (c <= maxCol - 1) { 
       if (buckets[r][c] != 0) { 
        g.fillOval((c) * xDist + 50, (r) * yDist + 50, xDist, yDist); 
        System.out.println(r + " " + c); 
       } 
       c = c + 1; 
      } 
      r = r + 1; 
     } 
    } 

    public void setCellSize(int width, int height) { 
     this.xDist = width; 
     this.yDist = height; 
    } 

    public void setGenerations(int gens) { 
     this.gen = gens; 
    } 

    public void setCells(int R, int C, int L) { 
     this.r = R; 
     this.c = C; 
     this.l = L; 
    } 

    public int getRow() { 
     return maxRow; 
    } 

    public int getCol() { 
     return maxCol; 
    } 

    public int getGenerations() { 
     return gen; 
    } 

    public int getR() { 
     return r; 
    } 

    public int getC() { 
     return c; 
    } 

    public int getL() { 
     return l; 
    } 
} 

GoLWorldMainCS

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 

public class GoLWorldMainCS { 

    public static void main(String[] args) { 
     boolean cont; 

     GoLWorldCS terrain = new GoLWorldCS(Integer.parseInt(JOptionPane.showInputDialog("Enter rows: ")), Integer.parseInt(JOptionPane.showInputDialog("Enter cols: "))); 
     terrain.setGenerations(Integer.parseInt(JOptionPane.showInputDialog("Enter generations: "))); 

     terrain.setSize(700, 700); 
     terrain.setTitle("Game of Life"); 
     terrain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     terrain.setVisible(true); 

     if (terrain.getRow() > 60 || terrain.getRow() < 1 || terrain.getCol() > 60 || terrain.getCol() < 1 || terrain.getGenerations() < 1) { 
      System.out.println("Error: Rows and Columns must be 1-60, and Generations must be greater than 0."); 
     } else { 
      terrain.setCellSize((600/terrain.getRow()), (600/terrain.getCol())); 
      terrain.clearGrid(); 

      cont = true; 
      while (cont == true) { 
       terrain.setCells(terrain.getC(), terrain.getR(), 1); 
       terrain.setCells(Integer.parseInt(JOptionPane.showInputDialog("Enter x-coordinate of live cell (999 to stop): ")), Integer.parseInt(JOptionPane.showInputDialog("Enter y-coordinate of live cell (0 to stop): ")), Integer.parseInt(JOptionPane.showInputDialog("Enter live row length (0 to stop)"))); 

       if (terrain.getR() == 999) { 
        cont = false; 
       } else { 
        if (terrain.getR() >= 0 && terrain.getR() <= terrain.getRow() && terrain.getC() >= 0 && terrain.getC() <= terrain.getCol() && terrain.getL() >= 0 && terrain.getL() <= (terrain.getRow() - terrain.getC() + 1)) { 
         terrain.markAlive(); 
         terrain.repaint(); 
        } else { 
         System.out.println("Entered coordinate out of bounds"); 
        } 
       } 
      } 
     } 
    } 
} 
+0

대단히 감사합니다. 또한 세대에 변화가 있음을 보여주는 루프에 대한 조언이 있습니까? 다시 감사합니다. –

+0

나는 당신이 가고있는 논리를 정말로 이해하지 못했기 때문에 많은 것을 다시 작성했다. 다음은 http://pastebin.com/yacH2w7m 소스입니다. – yate

관련 문제