gpt4 book ai didi

java - 尝试画板

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

我有一个带有 if 语句的paintComponent。在每种情况下,方 block 都应显示在面板上。什么也没有出现。

 @Override
public void paintComponent(Graphics g) {

//for loop to draw current board
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= columns; j++) {

if (board[i][j] == '.') {
g.setColor(Color.black);
g.drawRect((xSize / 5) * i, (ySize / 6) * j, 50, 50);
}

if (board[i][j] == '#') {
g.setColor(Color.magenta);
g.drawRect((xSize / 5) * i, (ySize / 6) * j, 50, 50);
}

if (board[i][j] == '%') {
g.setColor(Color.orange);
g.drawRect((xSize / 5) * i, (ySize / 6) * j, 50, 50);
}

if (board[i][j] == '@') {
g.setColor(Color.pink);
g.drawRect((xSize / 5) * i, (ySize / 6) * j, 50, 50);
}

}
}

}//end of paint component

最佳答案

您有两个问题,

  1. 您不应使用魔数(Magic Number),而应使用可用的变量。

    g.fillRect(xSize * i, ySize * j, xSize, ySize);
  2. 不要过度循环您的列。您应该始终使用 <因为数组的索引从零开始。

    for (int j = 0; j < columns; j++) {

enter image description here

工作示例

import java.awt.*;
import javax.swing.*;

public class DrawPanel extends JPanel {
private static final long serialVersionUID = 568117316148341762L;

private int columns;
private int rows;
private int xSize;
private int ySize;
private char[][] board;

public DrawPanel(int columns, int rows, int xSize, int ySize) {
super();

this.columns = columns;
this.rows = rows;
this.xSize = xSize;
this.ySize = ySize;
this.board = new char[rows][columns];

this.setPreferredSize(new Dimension(columns * xSize, rows * ySize));

setRandomTiles();
}

private void setRandomTiles() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
board[i][j] = randTile();
}
}
}

private char randTile() {
switch ((int) (Math.random() * 4)) {
case 0: return '.';
case 1: return '#';
case 2: return '%';
case 3: return '@';
default: return ' ';
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

// for loop to draw current board
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
switch (board[i][j]) {
case '.':
g.setColor(Color.BLACK);
break;
case '#':
g.setColor(Color.MAGENTA);
break;
case '%':
g.setColor(Color.ORANGE);
break;
case '@':
g.setColor(Color.PINK);
break;
}

g.fillRect(xSize * i, ySize * j, xSize, ySize);
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Draw Panel");
DrawPanel p = new DrawPanel(30, 30, 10, 10);

f.setContentPane(p);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}
<小时/>

您可以使用 BufferedReader 从文件加载板。

import java.awt.*;
import java.io.BufferedReader;
import java.io.*;
import javax.swing.*;

public class DrawPanel extends JPanel {
private static final long serialVersionUID = 568117316148341762L;

private int cols;
private int rows;
private int xSize;
private int ySize;
private char[][] board;

public DrawPanel(int rows, int cols, int xSize, int ySize) {
super();

this.rows = rows;
this.cols = cols;
this.xSize = xSize;
this.ySize = ySize;
this.board = new char[rows][cols];

init();
initRandomTiles();
}

public DrawPanel(String filename) {
super();

try {
loadBoard(filename);
init();
} catch (IOException e) {
e.printStackTrace();
}
}

protected void init() {
this.setPreferredSize(new Dimension(cols * xSize, rows * ySize));
}

private void loadBoard(String filename) throws IOException {
InputStream is = this.getClass().getClassLoader().getResourceAsStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

String[] metadata = br.readLine().split("\\s+");

this.rows = Integer.parseInt(metadata[0], 10);
this.cols = Integer.parseInt(metadata[1], 10);
this.xSize = Integer.parseInt(metadata[2], 10);
this.ySize = Integer.parseInt(metadata[3], 10);
this.board = new char[rows][cols];

int row = 0;
String line = null;
while ((line = br.readLine()) != null) {
board[row++] = line.trim().toCharArray();
}
}

private void initRandomTiles() {
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
board[row][col] = randTile();
}
}
}

private char randTile() {
switch ((int) (Math.random() * 4)) {
case 0: return '.';
case 1: return '#';
case 2: return '%';
case 3: return '@';
default: return ' ';
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
switch (board[row][col]) {
case '.':
g.setColor(Color.BLACK);
break;
case '#':
g.setColor(Color.MAGENTA);
break;
case '%':
g.setColor(Color.ORANGE);
break;
case '@':
g.setColor(Color.PINK);
break;
}

g.fillRect(xSize * col, ySize * row, xSize, ySize);
}
}

g.dispose();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Draw Panel");
DrawPanel p = new DrawPanel("resources/board.txt");

f.setContentPane(p);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}

board.txt

13 19 10 10
...................
.%#######%##@####%.
.#...............#.
.@...............#.
.#...............#.
.#...............#.
.%...............%.
.#...............#.
.#...............#.
.#...............@.
.#...............#.
.%####@##%#######%.
...................

关于java - 尝试画板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43826590/

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