Jared
Jared

Reputation: 588

Debugging Conway's Game of Life Graphics?

I am trying to make a simple version of Conway's Game of Life where the computer generates a grid of rectangles and fills in the rectangles that represent "live" cells. The problem I am having is that I cannot get the grid to clear after the first pattern, so all the patterns are generated on the same grid and it looks like a big blob of colored rectangles.

Here is my code:

public class GameofLife {
    static JPanel panel;
    static JFrame frame;
    public static void main(String[] args) throws InterruptedException{
        int [][] array = new int [17][17];
        /*
         * Set the pattern for Conway's Game of Life by manipulating the array below.
         */
        array[2][4]=1;
        array[2][5]=1;
        array[2][6]=1;
        panel = new JPanel();
        Dimension dim = new Dimension(400,400);
                panel.setPreferredSize(dim);
        frame = new JFrame();
        frame.setSize(1000, 500);
        Container contentPane =    frame.getContentPane();
        contentPane.add(panel);
        frame.setVisible(true);
        /*
        * Runs the Game of Life simulation "a" number of times.
        */
        int [][] end = new int [array.length][array[0].length];
        int a=0;
        while(a<4){
            for(int i=1; i<=array.length-2; i++)
            {
                for(int j=1; j<=array[0].length-2; j++)
                {
                    int counter = surround(array,i,j);
                    if(array[i][j]==1 && counter<=2)
                    {
                        end[i][j]=0;
                    }
                    if(array[i][j]==1 && counter==3)
                    {
                        end[i][j]=1;
                    }
                    if(array[i][j]==1 && counter>4)
                    {
                        end[i][j]=0;
                    }
                    if(array[i][j]==0 && counter==3)
                    {
                        end[i][j]=1;
                    }
                    if(array[i][j]==1 && counter==4)
                    {
                        end[i][j]=1;
                    }
                }
            }
            Graphics g = panel.getGraphics();
            Graphics(array,g);
            a++;
            for(int i=0; i<array.length; i++)
            {
                for(int j=0; j<array[0].length; j++)
                {
                    array[i][j]=end[i][j];
                    end[i][j]=0;
                }
            }
            Thread.sleep(1000);
            g.dispose();
        }
    }

        public static int surround(int[][] initial, int i, int j){
        int[][] surrounding = {{initial[i-1][j-1],initial[i-1][j],initial[i-1][j+1]},
                {initial[i][j-1],initial[i][j],initial[i][j+1]},
                {initial[i+1][j-1],initial[i+1][j],initial[i+1][j+1]}};
        int counter = 0;
        for(int a=0; a<=2; a++)
        {
            for(int b=0; b<=2; b++)
            {
                if(surrounding[a][b]==1)
                {
                    counter ++;
                }
            }
        }
        return counter;
    }

        public static void Graphics(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);
                    g.setColor(Color.BLACK);
                    if(array[i][j]==1)
                    {
                        g.fillRect(i*BOX_DIM, j*BOX_DIM, 10, 10);   
                    }
            }
        }

    }
}

Any help is much appreciated!

Upvotes: 0

Views: 127

Answers (2)

user700390
user700390

Reputation: 2339

You COULD fix this immediate concern by clearing grid at the top of the Graphics() function:

g.setColor(Color.??); // Choose desired background color here
g.fillRect( 0, 0, array.length * BOX_DIM, array[0].length * BOX_DIM);

BUT, consider that your approach is fighting against the natural way of handling graphics in Java. Any time your window gets clipped, it may not repaint in a timely manner.

You might want to consider sub-classing JPanel and overriding its onPaint() method, then invalidating the panel whenever the data model changes.

Upvotes: 0

mban
mban

Reputation: 442

You need to draw rectangles for both "alive" and "dead" cells but color them differently. Live cells could be black and dead cells white but if you don't redraw every cell during every iteration you'll run into the issue you've described. That being said...you seem to have answered your own question.

Upvotes: 1

Related Questions