Java 面板/框架图形不工作

标签 java swing graphics jpanel conways-game-of-life

我正在尝试使用 Java Graphics 制作一个生命模拟游戏,但是当运行我的代码时,屏幕的左侧三分之一是灰色的。我希望整个屏幕是白色的,黑色方 block 代表生命方 block 。我对所有 java 容器/面板和框架感到困惑。

这是我的代码:

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

}
}

这是生成的图片:

enter image description here

最佳答案

永远不要使用Graphics g = panel.getGraphics();。这不是 Swing 中自定义绘画的工作方式。看看Painting in AWT and SwingPerforming Custom Painting有关绘画工作原理的更多详细信息

例如...

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 扩展,并将您需要的所有逻辑放入该类中

关于Java 面板/框架图形不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33906017/

相关文章:

iphone - 如何在 iPhone 中仅对图像的一部分填充颜色

java - 找不到符号..(a.volume 错误并在编译器中显示为找不到符号请帮助)

java - NDK如何获取dvm点?

Java 验证 API : skip some rules without changing bean definition

java - JButton 上的图像

Java Swing : Remanence when removing a heavyweight jogl component to add a lightweight component at the same place

java - 更新子 JPanel 中的组件时如何在父 JPanel 中触发操作 (Java Swing)

c++ - gpu 蒙皮的矩阵计算

java - 为什么 Java 2D 原点在左上角?

java - 使用 JSOUP Java 更改 CSS