Java 在JPanel中画一个迷宫

Java 在JPanel中画一个迷宫,java,swing,maze,Java,Swing,Maze,我正在用Java制作一个随机迷宫生成器。用户可以选择算法,然后按下“生成”按钮,在JFrame的中心看到生成的迷宫。一旦迷宫生成,我必须在JPanel中绘制它。如果我们考虑具有回溯算法的DFS,对于每个单元格,我有4个布尔变量,表示单元是否有上、下、左、右的墙。 该算法运行并相应地移除这些墙(Dream Theater\m/)。现在每个细胞都应该有绘制迷宫所需的信息,但我不知道怎么做。我不能用索引来划分界限 这是守则草案: BufferedImage image = new BufferedIm

我正在用Java制作一个随机迷宫生成器。用户可以选择算法,然后按下“生成”按钮,在JFrame的中心看到生成的迷宫。一旦迷宫生成,我必须在JPanel中绘制它。如果我们考虑具有回溯算法的DFS,对于每个单元格,我有4个布尔变量,表示单元是否有上、下、左、右的墙。 该算法运行并相应地移除这些墙(Dream Theater\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
        }
    }
}
BufferedImage image=新的BufferedImage(迷宫面板尺寸,迷宫面板尺寸,BufferedImage.TYPE_INT_RGB);
Graphics g2=image.getGraphics();
g2.设置颜色(颜色为白色);
g2.fillRect(0,0,迷宫面板尺寸,迷宫面板尺寸);
g2.设置颜色(颜色为黑色);
对于(inti=0;i
更新

好的,解决方案应该是这样的

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);
        }
    }
}
for(inti=0;i
问题是没有绘制右边框和下边框。

图形类的

图形笔从它所穿过的路径向右下垂

因此,如果您试图在迷宫的右侧边缘绘制单元格的右侧边界,则
图形
笔将位于
缓冲图像
之外。解决方案是检查线段的坐标,并确保在图像中绘制所有直线

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

你说的
是什么意思?我不能用索引来画线。
?我不懂如何:g2.画线(?,,,,?)假设你有一张四格纸和一支铅笔。你怎么用手做这个?你如何决定在纸上画线的位置?代码是对应于每个单元格还是对应于JPanel?图形2d.绘制线实际记录在中。如果我理解正确的话,你想把整个单元格涂成白色,墙壁涂成黑色,以防它们出现,对吗?代码是为JPanel编写的。我得到了迷宫的所有单元(已经解了),我想画出迷宫的墙,迭代每个单元。用黑色画墙壁就足够了。