繁体   English   中英

Java面板/框架图形不起作用

[英]Java panel/frame graphic not working

我正在尝试使用Java Graphics进行生活模拟游戏,但是当我运行代码时,屏幕的左侧三分之一为灰色,我希望整个屏幕为白色,黑色正方形代表居住广场。 我对所有的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);
            }
        }
    }

}
}

这是生成的图片:

在此处输入图片说明

不要使用Graphics g = panel.getGraphics(); 永远 这不是Swing中自定义绘画的工作方式。 看一下AWT中的绘画以及“摇摆执行自定义绘画” ,以了解有关绘画工作原理的更多详细信息

例如...

绘画

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM