gpt4 book ai didi

Java 面板/框架图形不工作

转载 作者:行者123 更新时间:2023-12-01 10:51:02 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com