1

我正在开发一个制作康威生命游戏图形模型的程序,但它一旦启动就不允许我做任何事情;按钮不起作用,网格没有改变。我究竟做错了什么?

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;


public class gameOfLife extends JApplet{
    private static final long serialVersionUID = 1L;

cellClass cell;

public void init() {
    Container contentWindow = getContentPane();
    cell = new cellClass();{{
    setLayout(new FlowLayout()) }};
    contentWindow.add(cell);    
    }
} 

class grid extends JComponent{ 
    private static final long serialVersionUID = 2L;

    int XSIZE = 500;
    int YSIZE = 500;
    private int row;
    private int col;
    private int size = 5;
    private cellClass c;
    private Dimension preferredSize = new Dimension(XSIZE, YSIZE);

    public void paint(Graphics a) {
    int x, y;
        for(x=0; x<row; x++){
        for(y=0; y<col; y++){
            if(c.grid[x][y] != 0){
                a.drawRect(x * size, y * size, 5, 5);
            }
        }
    }
    a.drawRect(0, 0, XSIZE, YSIZE);

}

public grid(cellClass newGrid, int newRow, int newCol, int newSize) {
    setMinimumSize(preferredSize);
    setMaximumSize(preferredSize);
    setPreferredSize(preferredSize);
    this.row = newRow;
    this.col = newCol;
    this.size = newSize;
    this.c = newGrid;

}
}

class cellClass extends JPanel implements ActionListener{
private static final long serialVersionUID = 3L;

static final int ROW = 100;
static final int COL = 100;
static final int SIZE = 5;
static final int min = 2;
static final int max = 3;
static final int birth = 3;
public int genCount = 0;

public int[][] grid;
private int[][] nextGrid;

private GridBagLayout gridBag = new GridBagLayout();
private GridBagConstraints c = new GridBagConstraints();

JLabel title;
JLabel genCounter;
JButton oneGen;
JButton contPlay;
JButton stop;
public grid board;
public boolean paused = true;
public boolean canChange = true;

cellClass() {
    grid = new int [ROW][COL];
    nextGrid = new int[ROW][COL];

    makeGrid(grid);

    setLayout(gridBag);

    title = new JLabel("Game of Life Applet");
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;
    c.insets = new Insets(2,0,0,0);
    c.anchor = GridBagConstraints.WEST;
    add(title);

    board = new grid(this,ROW,COL,SIZE);
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 1;
    gridBag.setConstraints(board, c);
    add(board);

    oneGen = new JButton("Move one Generation");
    c.gridx = 0;
    c.gridy = 3;
    c.gridwidth = 1;
    gridBag.setConstraints(oneGen, c);
    add(oneGen);

    contPlay = new JButton("Play");
    c.gridx = 1;
    c.gridy = 3;
    c.gridwidth = 1;
            contPlay.setVisible(true);
    gridBag.setConstraints(contPlay, c);
    add(contPlay);

    stop = new JButton("Stop");
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 1;
            stop.setVisible(false);
    gridBag.setConstraints(stop, c);
    add(stop);

    genCounter = new JLabel("Generation: 0");
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    gridBag.setConstraints(genCounter, c);
    add(genCounter);
}

class ButtonListener {
    public void addActionListener(ActionEvent e) throws InterruptedException {
        JButton source = (JButton)e.getSource();

        if(source == oneGen){
            nextGen();
        }
        if(source == contPlay){
            paused = false;
            canChange = false;
                            contPlay.setVisible(false);
                            stop.setVisible(true);
            while (paused = false) {
                nextGen();
                Thread.sleep(1000);
            }
        }
        if(source == stop) {
            paused = true;
            canChange = false;
                            stop.setVisible(false);
                            contPlay.setVisible(true);
        }
    }
}

public void mouseClicked(MouseEvent e){
    int xco = e.getX() - board.getX();
    int yco = e.getY() - board.getY();
    if((e.getComponent() == board) && (paused == true)){
        if(grid[xco/5][yco/5] == 1){
            grid[xco/5][yco/5] = 0;
            board.repaint();
        }else if(grid[xco/5][yco/5] == 0){
            grid[xco/5][yco/5] = 1;
            board.repaint();
        }
    }
}

public void makeGrid(int[][] emptyGrid) {
    int x, y;
    for(x = 0; x < ROW; x++){
        for(y = 0; y < COL; y++){
            emptyGrid[x][y] = 0;
        }
    }
}

public void nextGen() {
    getNextGen();
    board.repaint();
    genCount++;
    genCounter.setText("Generation: " + Integer.toString(genCount));        
}

public void getNextGen() {
    int x, y, neighbor;
    makeGrid(nextGrid);
    for(x = 0; x < ROW; x++){
        for(y=0; y<COL; y++){
            neighbor = calculate(x,y);

            if(grid[x][y] != 0){
                if((neighbor >= min) && (neighbor <= max)) {
                    nextGrid[x][y] = neighbor;
                }
            }else {
                if(neighbor == birth){
                    nextGrid[x][y] = birth;
                }
            }
        }
    }
    makeGrid(grid);
    copyGrid(nextGrid,grid);
}

public void copyGrid(int[][] source, int[][] newGrid) {
    int x, y;
    for(x=0; x<ROW; x++){
        for(y=0; y<COL; y++){
            newGrid[x][y] = source[x][y];
        }
    }
}

private int calculate(int x, int y){
    int a, b, total;

    total = (grid[x][y]);
    for (a = -1; a<= 1; a++) {
        for (b = -1; b <= 1; b++){
            if(grid[(ROW + (x + a)) % ROW][(COL + (y + b)) % COL] != 0) {
                total++;
            }
        }
    }
    return total;
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

    }
}       

如果有人能告诉我它有什么问题,那就太棒了。

4

3 回答 3

5

您的主要问题之一是您试图在 Swing 事件线程(也称为事件调度线程或 EDT)上运行一个长进程,这实际上会冻结您的程序。我看到这里发生了这个问题:

class ButtonListener {
  public void addActionListener(ActionEvent e) throws InterruptedException {
     JButton source = (JButton) e.getSource();

     // ...

        while (paused = false) { // *******
           nextGen();
           Thread.sleep(1000);  // ******
        }
     }

     // ...

}

你有一个while(true)循环和一个Thread.sleep(...)都不应该在事件线程上调用。

相反,您应该使用Swing Timer

有关 Swing 事件线程的更多信息,请阅读Swing 中的并发

另外(1),你在哪里允许细胞被初始化为活着?如果没有活细胞从所有世代开始,只会显示一个空网格?您是否需要将 MouseListener 添加到您的一个或多个组件?我认为这将是一个好主意。

此外,在向按钮添加 ActionListener 时,(2)按钮通常会更好地工作,如Swing 按钮教程中所述。你读过 Swing 教程吗?如果没有,请查看它们(在此处找到它们),因为我认为它们会对您有所帮助。

另外(3)你可能会咬得比你能咀嚼的更多,因为你试图一次解决太多问题。当我创建一个与此类似的 GUI 时,我喜欢单独处理程序的每个部分,并在将它们全部组合成一个大程序之前先让它工作。因此,例如,首先处理非 GUI 模型,然后通过调用模型方法的测试代码运行它,从而使生成的代工作。接下来在 GUI 的每个部分上工作,包括 JButtons,然后是 MouseListener,然后是生命游戏的显示,然后是实现世代。

调试较小的测试程序比尝试调试整个shebang要容易得多,相信我。

于 2012-04-01T02:20:07.923 回答
1

您的程序为我启动,但还有其他问题。这将使您开始解决问题:

改变:

cell = new cellClass();

到:

cell = new cellClass(){{
  setLayout(new FlowLayout());
}}; 
于 2012-04-01T01:07:52.590 回答
0

也许在 init() 中尝试 super.init()?

于 2012-04-01T00:59:48.303 回答