我正在将Conway的生活游戏作为自己的个人项目进行Java实现。到目前为止,它仍然有效,但是规则出了错。预期的模式显示得不尽如人意。我的代码有问题吗?import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Cell extends JComponent implements MouseListener { private int row, col; private boolean isLiving; public Cell(int r, int c) { this.row = r; this.col = c; this.addMouseListener(this); } public void isAlive(int neighbors) { if (this.isLiving) { if (neighbors < 2) { this.isLiving = false; } else if (neighbors == 2 || neighbors == 3) { this.isLiving = true; } else if (neighbors > 3) { this.isLiving = false; } } else { if (neighbors == 3) { this.isLiving = true; } } } public boolean isLiving() { return this.isLiving; } public void paintComponent(Graphics g) { if (this.isLiving) { g.fillRect(0, 0, 10, 10); } else { g.drawRect(0, 0, 10, 10); } } public void mouseClicked(MouseEvent e) { this.isLiving = !this.isLiving; } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { }}
添加回答
举报
0/150
提交
取消