在JPanel中画一个迷宫
Draw a maze in JPanel
我正在 Java 制作随机迷宫生成器。用户可以 select 算法,然后按 "Generate" 按钮在 JFrame 的中心查看生成的迷宫。生成迷宫后,我必须将其绘制在 JPanel 中。如果我们考虑带有回溯算法的 dfs,对于每个单元格,我有 4 个布尔变量,指示单元格是否有上、下、左、右墙。
该算法运行并相应地移除这些墙(梦剧院\m/)。现在每个单元格都应该有绘制迷宫所需的信息,但我不知道该怎么做。我不能用索引来画线。
这是代码草稿:
BufferedImage image = new BufferedImage(MAZE_PANEL_DIM, MAZE_PANEL_DIM,BufferedImage.TYPE_INT_RGB);
Graphics g2 = image.getGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, MAZE_PANEL_DIM, MAZE_PANEL_DIM);
g2.setColor(Color.BLACK);
for(int i = 0; i < Maze.DIM; i++) {
for(int j = 0; j < Maze.DIM; j++) { // Note: the size of the cell is CELL_DIM = 600 / Maze.DIM
Cell cell = cells[i][j];
if(cell.hasRightWall()) {
// draw vertical line on the right border of the cell
}
if(cell.hasDownWall()) {
// draw horizontal line on the bottom border of the cell
}
if(cell.hasLeftWall()) {
// draw vertical line on the left border of the cell
}
if(cell.hasUpWall()) {
// draw horizontal line on the top border of the cell
}
}
}
更新
好的,解决方案应该是这样的...
for(int i = 0; i < Maze.DIM; i++) {
for(int j = 0; j < Maze.DIM; j++) { // Note: the size of the cell is CELL_DIM = 600 / Maze.DIM
Cell cell = cells[i][j];
if(cell.hasRightWall()) {
// draw vertical line on the right border of the cell
g2.drawLine(j * CELL_DIM + CELL_DIM, i * CELL_DIM, CELL_DIM + j * CELL_DIM, CELL_DIM + i * CELL_DIM);
}
if(cell.hasDownWall()) {
// draw horizontal line on the bottom border of the cell
g2.drawLine(j * CELL_DIM, i * CELL_DIM + CELL_DIM, j * CELL_DIM + CELL_DIM, i * CELL_DIM + CELL_DIM);
}
if(cell.hasLeftWall()) {
// draw vertical line on the left border of the cell
g2.drawLine(j * CELL_DIM, i * CELL_DIM, j * CELL_DIM, CELL_DIM + i * CELL_DIM);
}
if(cell.hasUpWall()) {
// draw horizontal line on the top border of the cell
g2.drawLine(j * CELL_DIM, i * CELL_DIM , CELL_DIM + j * CELL_DIM, i * CELL_DIM);
}
}
}
问题是没有绘制右边框和底边框。
docs 为Graphics
class 说:
The graphics pen hangs down and to the right from the path it traverses.
因此,如果您尝试在迷宫的右手边绘制单元格的右手边,Graphics
笔将在您的 BufferedImage
之外。解决方案是边界检查线段的坐标,并确保所有线都绘制在图像内部。
if (cell.hasRightWall()) {
int fromX = j * CELL_DIM + CELL_DIM;
int fromY = i * CELL_DIM;
if (fromX >= image.getWidth()) {
fromX = image.getWidth() - 1;
}
g2.drawLine(fromX, fromY, fromX, fromY + CELL_DIM);
}
我正在 Java 制作随机迷宫生成器。用户可以 select 算法,然后按 "Generate" 按钮在 JFrame 的中心查看生成的迷宫。生成迷宫后,我必须将其绘制在 JPanel 中。如果我们考虑带有回溯算法的 dfs,对于每个单元格,我有 4 个布尔变量,指示单元格是否有上、下、左、右墙。 该算法运行并相应地移除这些墙(梦剧院\m/)。现在每个单元格都应该有绘制迷宫所需的信息,但我不知道该怎么做。我不能用索引来画线。
这是代码草稿:
BufferedImage image = new BufferedImage(MAZE_PANEL_DIM, MAZE_PANEL_DIM,BufferedImage.TYPE_INT_RGB);
Graphics g2 = image.getGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, MAZE_PANEL_DIM, MAZE_PANEL_DIM);
g2.setColor(Color.BLACK);
for(int i = 0; i < Maze.DIM; i++) {
for(int j = 0; j < Maze.DIM; j++) { // Note: the size of the cell is CELL_DIM = 600 / Maze.DIM
Cell cell = cells[i][j];
if(cell.hasRightWall()) {
// draw vertical line on the right border of the cell
}
if(cell.hasDownWall()) {
// draw horizontal line on the bottom border of the cell
}
if(cell.hasLeftWall()) {
// draw vertical line on the left border of the cell
}
if(cell.hasUpWall()) {
// draw horizontal line on the top border of the cell
}
}
}
更新
好的,解决方案应该是这样的...
for(int i = 0; i < Maze.DIM; i++) {
for(int j = 0; j < Maze.DIM; j++) { // Note: the size of the cell is CELL_DIM = 600 / Maze.DIM
Cell cell = cells[i][j];
if(cell.hasRightWall()) {
// draw vertical line on the right border of the cell
g2.drawLine(j * CELL_DIM + CELL_DIM, i * CELL_DIM, CELL_DIM + j * CELL_DIM, CELL_DIM + i * CELL_DIM);
}
if(cell.hasDownWall()) {
// draw horizontal line on the bottom border of the cell
g2.drawLine(j * CELL_DIM, i * CELL_DIM + CELL_DIM, j * CELL_DIM + CELL_DIM, i * CELL_DIM + CELL_DIM);
}
if(cell.hasLeftWall()) {
// draw vertical line on the left border of the cell
g2.drawLine(j * CELL_DIM, i * CELL_DIM, j * CELL_DIM, CELL_DIM + i * CELL_DIM);
}
if(cell.hasUpWall()) {
// draw horizontal line on the top border of the cell
g2.drawLine(j * CELL_DIM, i * CELL_DIM , CELL_DIM + j * CELL_DIM, i * CELL_DIM);
}
}
}
问题是没有绘制右边框和底边框。
docs 为Graphics
class 说:
The graphics pen hangs down and to the right from the path it traverses.
因此,如果您尝试在迷宫的右手边绘制单元格的右手边,Graphics
笔将在您的 BufferedImage
之外。解决方案是边界检查线段的坐标,并确保所有线都绘制在图像内部。
if (cell.hasRightWall()) {
int fromX = j * CELL_DIM + CELL_DIM;
int fromY = i * CELL_DIM;
if (fromX >= image.getWidth()) {
fromX = image.getWidth() - 1;
}
g2.drawLine(fromX, fromY, fromX, fromY + CELL_DIM);
}