[英]Conway's Game of Life graphics
I'm trying to make a Conway's Game of Life program, and am having trouble with the grid actually showing up on my JFrame. 我正在尝试制作一个Conway的“生活游戏”程序,并且在JFrame上实际显示网格时遇到了麻烦。 When I write the code like the following, my paintComponent works perfectly:
当我编写如下代码时,paintComponent可以正常工作:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
public class AutomataTheoryAssignmentTesting {
public static void main(String[] args) {
RandomTrues grid = new RandomTrues(); // I know these two lines don't affect the grid added
grid.neighborAnalysis(); // to my JPanel, hence why I'm trying to find an alternative solution
MakeTotalPanel frame = new MakeTotalPanel();
frame.setTitle("Game of Life");
frame.setSize(620, 620);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MakeTotalPanel extends JFrame {
MakeTotalPanel() {
setLayout(new GridLayout(30, 30, 2, 2));
for(int i = 0; i < 900; i++) {
add(new RandomTrues());
}
}
}
However, in order to fix the dilemma I wrote in the comment in the code, I know I need to do something like this: 但是,为了解决我在代码注释中写的难题,我知道我需要执行以下操作:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
public class AutomataTheoryAssignmentTesting {
public static void main(String[] args) {
MakeTotalPanel frame = new MakeTotalPanel();
frame.setTitle("Game of Life");
frame.setSize(620, 620);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MakeTotalPanel extends JFrame {
RandomTrues grid = new RandomTrues();
MakeTotalPanel() {
grid.neighborAnalysis();
setLayout(new GridLayout(30, 30, 2, 2));
for(int i = 0; i < 900; i++) {
add(grid);
}
}
}
However, when I do this, only a small square at the upper right hand corner actually paints. 但是,当我这样做时,实际上仅在右上角有一个小方块。 The rest of my code is as follows... Thank you in advance!
我其余的代码如下...预先谢谢!
class RandomTrues extends JPanel {
boolean[][] gridvalues;
int rowcounter = 0;
int colcounter = 0;
public RandomTrues() {
gridvalues = new boolean[30][30];
Random generator = new Random();
double b;
for(int i=0; i<30; i++) {
for(int j=0; j<30; j++) {
b = generator.nextDouble() * 100;
if (b <= 62)
gridvalues[i][j] = true;
else
gridvalues[i][j] = false;
}
}
}
// reading the values of the grid and marking to change or not to change
void neighborAnalysis() {
int g;
int h;
boolean[][] change = new boolean[30][30];
for(int k=0; k<30; k++) {
for(int l=0; l<30; l++) {
change[k][l] = false;
}
for(g=0; g<30; g++) {
for(h=0; h<30; h++) {
if(g==0) {
if(h==0) {
if(gridvalues[g][h] == true) {
if(gridvalues[g+1][h]==false && gridvalues[g][h+1]==false) {
change[g][h]=true;
}
}
}
else if(h==29) {
if(gridvalues[g][h]==true) {
if(gridvalues[g+1][h]==false && gridvalues[g][h-1]==false) {
change[g][h]=true;
}
}
}
else {
if(gridvalues[g][h]==false) {
if(gridvalues[g+1][h]==true && gridvalues[g][h+1]==true && gridvalues[g][h-1]==true) {
change[g][h]=true;
}
}
else if(gridvalues[g][h]==true) {
if((gridvalues[g+1][h]==false && gridvalues[g][h-1]==false) || (gridvalues[g+1][h]==false && gridvalues[g][h+1]==false) || (gridvalues[g][h+1]==false && gridvalues[g][h-1]==false)) {
change[g][h]=true;
}
}
}
}
if(g==29) {
if(h==0) {
if(gridvalues[g][h]==true) {
if(gridvalues[g-1][h]==false && gridvalues[g][h+1]==false) {
change[g][h]=true;
}
}
}
else if(h==29) {
if(gridvalues[g][h]==true) {
if(gridvalues[g-1][h]==false && gridvalues[g][h-1]==false) {
change[g][h]=true;
}
}
}
else {
if(gridvalues[g][h]==false) {
if(gridvalues[g-1][h]==true && gridvalues[g][h+1]==true && gridvalues[g][h-1]==true) {
change[g][h]=true;
}
}
else if(gridvalues[g][h]==true) {
if((gridvalues[g-1][h]==false && gridvalues[g][h-1]==false) || (gridvalues[g-1][h]==false && gridvalues[g][h+1]==false) || (gridvalues[g][h+1]==false && gridvalues[g][h-1]==false)) {
change[g][h]=true;
}
}
}
}
else if(g<29 && g>0) {
if(h==0) {
if(gridvalues[g][h]==false) {
if(gridvalues[g-1][h]==true && gridvalues[g][h+1]==true && gridvalues[g+1][h]==true) {
change[g][h]=true;
}
}
else if(gridvalues[g][h]==true) {
if((gridvalues[g-1][h]==false && gridvalues[g+1][h]==false) || (gridvalues[g-1][h]==false && gridvalues[g][h+1]==false) || (gridvalues[g][h+1]==false && gridvalues[g+1][h]==false)) {
change[g][h]=true;
}
}
}
if(h==29) {
if(gridvalues[g][h]==false) {
if(gridvalues[g-1][h]==true && gridvalues[g+1][h]==true && gridvalues[g][h-1]==true) {
change[g][h]=true;
}
}
else if(gridvalues[g][h]==true) {
if((gridvalues[g-1][h]==false && gridvalues[g][h-1]==false) || (gridvalues[g-1][h]==false && gridvalues[g+1][h]==false) || (gridvalues[g+1][h]==false && gridvalues[g][h-1]==false)) {
change[g][h]=true;
}
}
}
else if(h<29 && h>0) {
if(gridvalues[g][h]==false) {
if((gridvalues[g-1][h]==true && gridvalues[g+1][h]==true && gridvalues[g][h+1]==true)||(gridvalues[g+1][h]==true && gridvalues[g][h+1]==true && gridvalues[g][h-1]==true)||(gridvalues[g][h+1]==true && gridvalues[g][h-1]==true && gridvalues[g-1][h]==true)||(gridvalues[g][h-1]==true && gridvalues[g+1][h]==true && gridvalues[g-1][h]==true)) {
change[g][h]=true;
}
}
else if(gridvalues[g][h]==true) {
if((gridvalues[g-1][h]==false && gridvalues[g+1][h]==false && gridvalues[g][h+1]==false)||(gridvalues[g+1][h]==false && gridvalues[g][h+1]==false && gridvalues[g][h-1]==false)||(gridvalues[g][h+1]==false && gridvalues[g][h-1]==false && gridvalues[g-1][h]==false)||(gridvalues[g][h-1]==false && gridvalues[g+1][h]==false && gridvalues[g-1][h]==false)) {
change[g][h]=true;
}
else if(gridvalues[g-1][h]==true && gridvalues[g+1][h]==true && gridvalues[g][h+1]==true && gridvalues[g][h-1]==true) {
change[g][h]=true;
}
}
}
}
}
}
// changing the grid values
g = 0;
h = 0;
for(g=0; g<30; g++) {
for(h=0; h<30; h++) {
if(change[g][h]==true) {
gridvalues[g][h] = !gridvalues[g][h];
change[g][h] = false;
}
}
}
}
}
//paint component
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(1, 1, 30, 30);
if(gridvalues[rowcounter][colcounter] == false) {
g.setColor(Color.RED);
g.fillRect(1, 1, 30, 30);
}
else if(gridvalues[rowcounter][colcounter] == true) {
g.setColor(Color.WHITE);
g.fillRect(1, 1, 30, 30);
}
rowcounter = rowcounter + 1;
colcounter = colcounter + 1;
}
}
Here: 这里:
grid.neighborAnalysis();
setLayout(new GridLayout(30, 30, 2, 2));
for(int i = 0; i < 900; i++) {
add(grid);
}
You are trying to add the same component, grid multiple times to your GUI, and this won't work since a component can only be displayed in one container. 您试图将相同的组件添加到GUI多次网格化,但这将无法正常工作,因为一个组件只能显示在一个容器中。
Your logic seems faulty since the RandomTrues class already holds a 30 by 30 grid, and so adding it once to the JFrame and not 900 times should be adequate, assuming that RandomTrues works. 由于RandomTrues类已经拥有30 x 30的网格,因此您的逻辑似乎是错误的,因此假设RandomTrues可以正常运行,则将其添加到JFrame一次而不是900次就足够了。
If this were my program, I would try to separate the program logic from its GUI, or put in another way, to separate out the model from the view. 如果这是我的程序,我将尝试将程序逻辑与其GUI分开,或以另一种方式从视图中分离出模型。 The view would be a JPanel that held a grid of cells, which could be JPanels if desired, and with methods for allowing outside classes (the control) to set the state of these cells.
该视图将是一个包含单元格网格的JPanel,如果需要,可以是JPanels,并带有允许外部类(控件)设置这些单元格状态的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.