Java panel/frame 图片不工作

Java panel/frame graphic not working

我正在尝试使用 Java 图形制作生活模拟游戏,但是当 运行 我的代码时,屏幕左侧的三分之一是 grey.I 希望整个屏幕是白色的,黑色方块代表生活方块。我对所有 java containers/panels 和框架感到困惑。

这是我的代码:

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

}
}

这是生成的图片:

永远不要使用 Graphics g = panel.getGraphics();。这不是自定义绘画在 Swing 中的工作方式。查看 Painting in AWT and Swing and Performing Custom 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);
                }
            }
        }

    }
}

现在,就我个人而言,我会创建一个自定义组件,它从 JPanel 扩展而来,并将您需要的所有逻辑放入 class