Skip to main content
deleted 40 characters in body; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

I recently coded a program to stimulate Conway's Game of Life: http://en.wikipedia.org/wiki/Conway%27s_Game_of_LifeConway's Game of Life. Originally, my program just printed an array of 1's and 0's where the 1's represented the "live" cells, but I tried to teach myself some graphics programming to add graphics to the game. Currently, my program draws a black rectangle to represent a live cell and a white one to represent a dead cell in a grid of rectangles. I am looking to improve my implementation of the graphics and do it in a more organized and traditional way.

Here is my code:

Thanks for any help!

I recently coded a program to stimulate Conway's Game of Life: http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life. Originally, my program just printed an array of 1's and 0's where the 1's represented the "live" cells, but I tried to teach myself some graphics programming to add graphics to the game. Currently, my program draws a black rectangle to represent a live cell and a white one to represent a dead cell in a grid of rectangles. I am looking to improve my implementation of the graphics and do it in a more organized and traditional way.

Here is my code:

Thanks for any help!

I recently coded a program to stimulate Conway's Game of Life. Originally, my program just printed an array of 1's and 0's where the 1's represented the "live" cells, but I tried to teach myself some graphics programming to add graphics to the game. Currently, my program draws a black rectangle to represent a live cell and a white one to represent a dead cell in a grid of rectangles. I am looking to improve my implementation of the graphics and do it in a more organized and traditional way.

added 65 characters in body
Source Link
Jared
  • 711
  • 1
  • 7
  • 20
import javax.swing.JFrame;
 import javax.swing.JPanel;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;

public class GameofLife {
static JPanel panel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException{
    int [][] array = new int [28][28];
    /*
     * Set the pattern for Conway's Game of Life by manipulating the array below.
     */
    
    /*Acorn
     */
    array[19][15]=1;
    array[19][16]=1;
    array[19][19]=1;
    array[19][20]=1;
    array[19][21]=1;
    array[18][18]=1;
    array[17][16]=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 [][]int[][] end = new int [arrayint[array.length][array[0].length];
    int a=0;a = 0;
    while (true) {
        for (int i=1; i<=array.length-2;i i++)
= 1; i <= array.length - 2; i++) {
            for (int j=1; j<=array[0].length-2; j++)
   j = 1; j <= array[0].length - 2; j++) {
                int counter = surround(array, i, j);
                if(array[i][j]==1 && counter<=2)
         (array[i][j] == 1 && counter <= 2) {
                    end[i][j]=0;end[i][j] = 0;
                }
                if(array[i][j]==1 && counter==3)
         (array[i][j] == 1 && counter == 3) {
                    end[i][j]=1;end[i][j] = 1;
                }
                if(array[i][j]==1 && counter>4)
         (array[i][j] == 1 && counter > 4) {
                    end[i][j]=0;end[i][j] = 0;
                }
                if(array[i][j]==0 && counter==3)
         (array[i][j] == 0 && counter == 3) {
                    end[i][j]=1;end[i][j] = 1;
                }
                if(array[i][j]==1 && counter==4)
         (array[i][j] == 1 && counter == 4) {
                    end[i][j]=1;end[i][j] = 1;
                }
            }
        }
        Graphics g = panel.getGraphics();
        Graphics(array, g);
        a++;
        /*
         * printArray(array);
        System.out.println("");
         * System.out.println("");
        *
        */
        for (int i=0; i<array.length; i++)
 i = 0; i < array.length; i++) {
            for (int j=0; j<array[0].length; j++)
 j = 0; j < array[0].length; j++) {
    {
            array[i][j] = end[i][j];
  array[i][j]=end[i][j];
              end[i][j] = end[i][j]=0;0;
            }
        }
        Thread.sleep(125);
        g.dispose();
    }
}
 
/*
 * Checks the cells around a specific cell in the initial array and returns
 * the number
  * of live cells surrounding it. 
 */
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] 1][j + 1] },
            { initial[i][j - 1], initial[i][j],initial[i][j+1] initial[i][j + 1] },
            {initial[i+1][j initial[i + 1][j - 1],initial[i+1][j] initial[i + 1][j],initial[i+1][j+1]
                    initial[i + 1][j + 1] } };
    int counter = 0;
    for (int a=0;a a<=2;= a++)
0; a <= 2; a++) {
        for (int b=0; b<=2; b++)
 b = 0; b <= 2; b++) {
            if(surrounding[a][b]==1)
         (surrounding[a][b] == 1) {
                counter ++;counter++;
            }
        }
    }
    return counter;
}
 
/*
 * Prints the 2D array.
 */
public static void printArray(int[][] input)
  {
    for (int x =0;= x<input0; x < input.length; x++)
    {
        for (int y=0;y y<= 0; y < input[0].length; y++)
        {
            System.out.print(input[x][y]);
        }
        System.out.println("");
    }
}
 
/*
 * Creates the graphic for Conway's game of life.
 */
public static void Graphics(int[][] array, Graphics g)
  {
    int BOX_DIM=10;BOX_DIM = 10;
    for (int i=0;i i<array= 0; i < array.length; i++)
    {
        for (int j=0; j<array[0].length; j++)
 j = 0; j < array[0].length; j++) {
                g.drawRect(i*BOX_DIMi * BOX_DIM, j*BOX_DIMj * BOX_DIM, 10, 10);
                if(array[i][j]==0)
             (array[i][j] == 0) {
                    g.setColor(Color.WHITE);
                    g.fillRect(i*BOX_DIMi * BOX_DIM, j*BOX_DIMj * BOX_DIM, 10, 10);   
                }
                if(array[i][j]==1)
             (array[i][j] == 1) {
                    g.setColor(Color.BLACK);
                    g.fillRect(i*BOX_DIMi * BOX_DIM, j*BOX_DIMj * BOX_DIM, 10, 10);   
                }
        }
    }
    
}
}
import javax.swing.JFrame;
 import javax.swing.JPanel;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;

public class GameofLife {
static JPanel panel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException{
    int [][] array = new int [28][28];
    /*
     * Set the pattern for Conway's Game of Life by manipulating the array below.
     */
    
    /*Acorn
     */
    array[19][15]=1;
    array[19][16]=1;
    array[19][19]=1;
    array[19][20]=1;
    array[19][21]=1;
    array[18][18]=1;
    array[17][16]=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(true){
        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++;
        /*
         * printArray(array);
        System.out.println("");
        System.out.println("");
        *
        */
        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(125);
        g.dispose();
    }
}
/*
 * Checks the cells around a specific cell in the initial array and returns the number
  * of live cells surrounding it. 
 */
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;
}
/*
 * Prints the 2D array.
 */
public static void printArray(int[][] input)
 {
    for(int x =0; x<input.length; x++)
    {
        for(int y=0; y< input[0].length; y++)
        {
            System.out.print(input[x][y]);
        }
        System.out.println("");
    }
}
/*
 * Creates the graphic for Conway's game of life.
 */
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);
                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);   
                }
        }
    }
    
}
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;

public class GameofLife {
static JPanel panel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException{
    int [][] array = new int [28][28];
    /*
     * Set the pattern for Conway's Game of Life by manipulating the array below.
     */
    
    /*Acorn
     */
    array[19][15]=1;
    array[19][16]=1;
    array[19][19]=1;
    array[19][20]=1;
    array[19][21]=1;
    array[18][18]=1;
    array[17][16]=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 (true) {
        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++;
        /*
         * printArray(array); System.out.println("");
         * System.out.println("");
         */
        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(125);
        g.dispose();
    }
}
 
/*
 * Checks the cells around a specific cell in the initial array and returns
 * the number of live cells surrounding it.
 */
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;
}
 
/*
 * Prints the 2D array.
 */
public static void printArray(int[][] input) {
    for (int x = 0; x < input.length; x++) {
        for (int y = 0; y < input[0].length; y++) {
            System.out.print(input[x][y]);
        }
        System.out.println("");
    }
}
 
/*
 * Creates the graphic for Conway's game of life.
 */
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);
            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);
            }
        }
    }

}
}
Source Link
Jared
  • 711
  • 1
  • 7
  • 20

Graphics in Conway's Game of Life in Java

I recently coded a program to stimulate Conway's Game of Life: http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life. Originally, my program just printed an array of 1's and 0's where the 1's represented the "live" cells, but I tried to teach myself some graphics programming to add graphics to the game. Currently, my program draws a black rectangle to represent a live cell and a white one to represent a dead cell in a grid of rectangles. I am looking to improve my implementation of the graphics and do it in a more organized and traditional way.

Here is my code:

import javax.swing.JFrame;
 import javax.swing.JPanel;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;

public class GameofLife {
static JPanel panel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException{
    int [][] array = new int [28][28];
    /*
     * Set the pattern for Conway's Game of Life by manipulating the array below.
     */
    
    /*Acorn
     */
    array[19][15]=1;
    array[19][16]=1;
    array[19][19]=1;
    array[19][20]=1;
    array[19][21]=1;
    array[18][18]=1;
    array[17][16]=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(true){
        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++;
        /*
         * printArray(array);
        System.out.println("");
        System.out.println("");
        *
        */
        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(125);
        g.dispose();
    }
}
/*
 * Checks the cells around a specific cell in the initial array and returns the number
 * of live cells surrounding it. 
 */
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;
}
/*
 * Prints the 2D array.
 */
public static void printArray(int[][] input)
{
    for(int x =0; x<input.length; x++)
    {
        for(int y=0; y< input[0].length; y++)
        {
            System.out.print(input[x][y]);
        }
        System.out.println("");
    }
}
/*
 * Creates the graphic for Conway's game of life.
 */
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);
                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);   
                }
        }
    }
    
}
}

Thanks for any help!