康威生命游戏迭代无法正常工作

时间:2015-12-03 18:48:41

标签: java swing multidimensional-array jpanel conways-game-of-life

我正在尝试与JPanel合作制作Conway的生活游戏版本。 但是,当我尝试迭代/进展我的游戏时,它不起作用。首先,我看到一个空白的白色屏幕5秒,然后我看到一个黑色的方块没有改变。有谁知道解决方案?

继承我的代码:

package newJAVart;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.awt.*;
import java.applet.*;




import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ConwayGame {

    static JPanel panel;
    static JFrame frame;

    static int[][] array = new int[40][40];
    public static void main(String[] args) throws InterruptedException {

          //Thread.sleep check if you can use it 
       array[5][5]  = 1;
       array[6][6]  = 1;
       //array[4][4] = 1;
       array[5][6] = 1;

        array[9][10] = 1;
       // System.out.println(Arrays.deepToString(array));
        //nextGeneration();
        //System.out.println(Arrays.deepToString(array));


        panel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                int stop = 0;
                while(stop<10){

                //nextGeneration();
                //array[34][34]  = 1;

                try {
                    nextGeneration();
                    System.out.println(Arrays.deepToString(array));
                     drawArray(array, g);

                     stop++;

                     array[5][6] = 0;
                    Thread.sleep(200);


                    g.dispose();

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }


            }


        };


        frame = new JFrame();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    /*
     * Creates the graphic for Conway's game of life.
     */







   //this finds the states of all the squares next to the given one and handle edge cases
    public static int nextTo(int a, int b){
        int sum = 0;
    //  for(int i)
        //if(a+1>array.length||)
        //this will handle edge cases
        int[] adjunc = new int[8];
        if(a==0||b==0||a==array.length-1||b==array.length-1){
            adjunc = new int[]{0,0,0,0,0,0,0,0};
        }else{
        /*
        if(a==array.length-1|| b == array.length-1){

            return nextTo(a-1,b-1);
        }else if(a==0){
  //adjunc = new int[]{array[a+1][b+1],array[a+1][b],array[a+1][b-1],array[a][b+1], 
        // array[array.length - 1 ][b-1],array[array.length -1 ][b],array[a-1][b+1],array[a][b-1]};
            //return nextTo(a-1,b-1);
            a = 
        }else if(b==0){
            adjunc = new int[]{array[a+1][b+1],array[a+1][b],array[a+1][b-1],array[a][b+1], 
                     array[a-1][b-1],array[a-1][b],array[a-1][b+1],array[a][b-1]};
        }else{
        */

            adjunc = new int[]{array[a+1][b+1],array[a+1][b],array[a+1][b-1],array[a][b+1], 
                     array[a-1][b-1],array[a-1][b],array[a-1][b+1],array[a][b-1]};  
        }

 //}
        for(int check: adjunc){
            if(check == 1){
            sum = sum +1;
            }
        }

        return sum ;
    }

    public static void nextGeneration(){
        int[][] grid = new int[40][40];
        for(int i=0;i<array.length-2;++i){
            for(int j=0;j<array[0].length-2;++j){
                //heres my error nexTo is being called on eveything including side elements
                int stuff = nextTo(i,j);
                if(array[i][j] ==1 && stuff < 2){
                    System.out.println("This is running");
   //Any live cell with fewer than two live neighbors dies,as if caused by under-population
                    if(i==34){
                        System.out.println("This is number 34");
                    }
                    grid[i][j] = 0;
                }else if(array[i][j]==1 && stuff>3){
                    System.out.println("This is running2");
                    grid[i][j] = 0;
                }
                else if(array[i][j] == 0 && stuff ==3){
                    System.out.println("This is running3");
                    grid[i][j] = 1;

                }
            }

        }
        array = grid;



    }










    /*
     * else if(array[i][j]==1 && (stuff<4 || stuff >2)){



                }
     * 
     */











 //draws Array    in JPanel
    public static void drawArray(int[][] array, Graphics g) {
        int BOX_DIM = 10;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                g.drawRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
                if (array[i][j] == 0) {
                    g.setColor(Color.WHITE);
                    g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
                }
                if (array[i][j] == 1) {
                    g.setColor(Color.BLACK);
                    g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
                }
            }
        }

    }
}

1 个答案:

答案 0 :(得分:4)

所有迭代和绘画以及等待都发生在paintComponent中 - 因此阻止 UI实际显示它。你应该想办法一次做一次迭代,然后让paintComponent绘制当前迭代,而外部线程执行等待和游戏处理。

基本上,从paintComponent中删除除drawArray()之外的所有内容,并在其他地方执行其余操作 - 一旦完成一次迭代,就在EDT中触发重绘。