为什么不是';我的Java生活游戏程序运行不正常吗?

为什么不是';我的Java生活游戏程序运行不正常吗?,java,swing,applet,initialization,cellular-automata,Java,Swing,Applet,Initialization,Cellular Automata,我正在开发一个程序,制作康威《生活的游戏》的图形模型,但一旦启动,它就不允许我做任何事情;按钮不起作用,网格也没有改变。我做错了什么 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 J

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

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

    }
}       
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseEvent;
公共类生命游戏扩展JApplet{
私有静态最终长serialVersionUID=1L;
细胞类细胞;
公共void init(){
容器contentWindow=getContentPane();
cell=新的cellClass(){{
setLayout(新的FlowLayout())};
contentWindow.add(单元格);
}
} 
类网格扩展JComponent{
私有静态最终长serialVersionUID=2L;
int XSIZE=500;
int-YSIZE=500;
私人int row;
私人国际学院;
私有整数大小=5;
c类私人牢房;
私有维度preferredSize=新维度(XSIZE、YSIZE);
公共空间绘制(图形a){
int x,y;

对于(x=0;x可能尝试init()中的super.init()?

可能尝试init()中的super.init()?

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

更改:

cell = new cellClass();
致:


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

更改:

cell = new cellClass();
致:


您的一个主要问题是,您试图在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)
循环和一个
线程。sleep(…)
都不应该在事件线程上调用

相反,您应该使用

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

还有(1),你在哪里允许细胞被初始化为活细胞?如果没有活细胞开始,所有代将只显示一个空网格?你需要在一个或多个组件中添加鼠标侦听器吗?我认为这是一个好主意

另外,(2)按钮通常在添加ActionListeners时工作得更好,如中所述。您看过Swing教程了吗?如果没有,请查看(找到它们),因为我认为它们会对您有很大帮助

还有(3)你一次要解决的问题太多了,你可能吃得太多了。当我创建一个类似的GUI时,我喜欢单独处理程序的每一部分,并在将其组合成一个大程序之前先让它工作。因此,例如,首先处理非GUI模型,然后生成通过运行调用模型方法的测试代码进行工作。接下来,对GUI的每个部分逐一进行工作,包括JButton、MouseListener、生命游戏的显示,然后实现生成


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

您的主要问题之一是,您试图在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)
循环和一个
线程。sleep(…)
都不应该在事件线程上调用

相反,您应该使用

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

还有(1),你在哪里允许细胞被初始化为活细胞?如果没有活细胞开始,所有代将只显示一个空网格?你需要在一个或多个组件中添加鼠标侦听器吗?我认为这是一个好主意

另外,(2)按钮通常在添加ActionListeners时工作得更好,如中所述。您看过Swing教程了吗?如果没有,请查看(找到它们),因为我认为它们会对您有很大帮助

还有(3)你一次要解决的问题太多了,你可能吃得太多了。当我创建一个类似的GUI时,我喜欢单独处理程序的每一部分,并在将其组合成一个大程序之前先让它工作。因此,例如,首先处理非GUI模型,然后生成通过运行调用模型方法的测试代码进行工作。接下来,对GUI的每个部分逐一进行工作,包括JButton、MouseListener、生命游戏的显示,然后实现生成


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

谢谢,我将它编译成一个新类(在一个新项目中)并加载。谢谢你指出我可能应该注意到的。谢谢,我将它编译成一个新类(在一个新项目中)它加载了。谢谢你指出我可能应该注意到的。这个答案有什么帮助的机会?说真的。@RayTayek-你真的应该在发布答案之前检查。这个答案有什么帮助的机会?说真的。@RayTayek-你真的应该在发布答案之前检查。小程序必须在,也是。请参阅此版本。小程序是一个高级主题。基于代码JFrame的应用程序。目前,小程序必须在上具有GUI对象。请参阅此版本。小程序是一个高级主题。基于代码JFrame的应用程序。目前。感谢您的帮助。这肯定会帮助我使其正常工作。@HovercraftFullOfEels你确定
while(paused=false)
等于
while(true)
?它是赋值而不是比较(
==
)。我甚至不知道这是有效的Java语法。我的IDE建议它等于
while(false)
iso
wh