Search code examples
javaswinggraphicsjpanelconways-game-of-life

Java panel/frame graphic not working


I am trying to make a game of life simulation using Java Graphics but when a run my code the left one third of the screen is grey.I want the whole screen to be white with black squares representing living squares. I am confused about all the java containers/panels and frames.

Here's my code:

public class ConwayGame {
static JPanel panel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException{
    int [][] array = new int [40][40];
    /*
     * 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(400,400 );
    Container contentPane = frame.getContentPane();
    contentPane.add(panel);
    frame.setVisible(true); 

    /*
     * Runs the Game of Life simulation "a" number of times.
     */



        Graphics g = panel.getGraphics();

     drawArray(array, g);

        //paint(g);
        //Thread.sleep(125);
        //g.dispose();

}














/*
 * Creates the graphic for Conway's game of life.
 */
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);
            }
        }
    }

}
}

Here's a picture of what is generated:

enter image description here


Solution

  • DON'T use Graphics g = panel.getGraphics(); EVER. This is not how custom painting works in Swing. Take a look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works

    For example...

    Painting

    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;
    
        public static void main(String[] args) throws InterruptedException {
            int[][] array = new int[40][40];
    
            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() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(400, 400);
                }
    
                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    drawArray(array, g);
                }
    
    
            };
            frame = new JFrame();
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
        }
    
        /*
         * Creates the graphic for Conway's game of life.
         */
        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);
                    }
                }
            }
    
        }
    }
    

    Now, personally, I would create a custom component, which extended from JPanel and would place all the logic you needed into that class