Java - 生命游戏 - 显示图形的麻烦

时间:2014-12-04 18:51:08

标签: java user-interface graphics conways-game-of-life

我很难完成我的生命游戏项目。我有所有的算法和功能,但我无法显示显示。有人在乎帮忙吗?提前谢谢!

这是我的(3)类和每个类的代码:

类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 :(得分:1)

有一些问题和不必要的事情。您覆盖setSize()的{​​{1}},因此无法正确计算框架的大小。您不需要Frame类,您可以设置默认关闭操作以退出框架。 Swing或多或少地弃用了AWT,因此您应该扩展ActiveFrame而不是JFrame。您可以覆盖Frame的{​​{1}}方法并将显示逻辑放在那里,调用paint()最终调用JFrame方法。您在显示方法中得到terrain.repaint(),因为paint()的大小与您设置的NullPointerExceptionbuckets不匹配,我将其移至构造函数。< / p>

GoLWorldCS

maxRow

GoLWorldMainCS

maxCol