有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 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;
   }
}    

共 (1) 个答案

  1. # 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,其中包含一个单元格网格,如果需要,可以是JPanel,并带有允许外部类(控件)设置这些单元格状态的方法