Java面板/帧图形不起作用 [英] Java panel/frame graphic not working

查看:157
本文介绍了Java面板/帧图形不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java Graphics进行人生模拟游戏,但是当运行我的代码时,屏幕的左边三分之一是灰色的。我希望整个屏幕都是白色,并用黑色方块表示生活广场。我对所有的java容器/面板和框架感到困惑。



这是我的代码:

 公共类ConwayGame {
静态JPanel面板;
静态JFrame框架;
public static void main(String [] args)throws InterruptedException {
int [] [] array = new int [40] [40];
/ *
*通过操纵下面的数组,设置康威生命游戏的模式。
* /

/ *橡子
* /
数组[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);
容器contentPane = frame.getContentPane();
contentPane.add(panel);
frame.setVisible(true);

/ *
*运行生命游戏模拟a次。
* /



图形g = panel.getGraphics();

drawArray(array,g);

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

}














/ *
*创建康威生命游戏的图形。
* /
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);
}
}
}

}
}

下面是生成内容的图片:

$ b $不要使用 Graphics g = panel.getGraphics(); EVER。这不是定制绘画在Swing中的工作原理。看看

  import java.awt.Color; 
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

公共类ConwayGame {

静态JPanel面板;
静态JFrame框架;

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

/ *
*为Conway的生活游戏创建图形。
* /
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 扩展,并将所有需要的逻辑放入该类中。


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:

解决方案

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...

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

这篇关于Java面板/帧图形不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆