我试图制作一个Conway的生命游戏计划,并且遇到了实际出现在我的JFrame上的网格问题。当我编写如下代码时,我的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());
}
}
}
然而,为了解决我在代码中的评论中所写的困境,我知道我需要做这样的事情:
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);
}
}
}
然而,当我这样做时,右上角只有一个小方块实际上是画画。我的其余代码如下......提前谢谢你!
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;
}
}
答案 0 :(得分:1)
下面:
grid.neighborAnalysis();
setLayout(new GridLayout(30, 30, 2, 2));
for(int i = 0; i < 900; i++) {
add(grid);
}
您正尝试将多个相同的组件,网格多次添加到您的GUI中,由于组件只能在一个容器中显示,因此无法正常工作。
你的逻辑似乎有问题,因为RandomTrues类已经拥有一个30乘30的网格,所以假设RandomTrues有效,那么将它添加到JFrame而不是900次应该是足够的。
如果这是我的程序,我会尝试将程序逻辑与其GUI分开,或以另一种方式,将模型与视图分开。该视图将是一个JPanel,它包含一个单元网格,如果需要可以是JPanels,以及允许外部类(控件)设置这些单元格状态的方法。