【问题标题】:Draw a maze in JPanel在 JPanel 中绘制迷宫
【发布时间】:2015-10-23 12:07:16
【问题描述】:

我正在用 Java 制作一个随机迷宫生成器。用户可以选择算法,然后按“生成”按钮在 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);
        }
    }
}

问题是右边框和下边框没有画出来。

【问题讨论】:

  • I can't play with the indexes to draw the lines. 是什么意思?
  • 我无法理解如何:g2.drawLine(?, ?, ?, ?)
  • 假设您有一张四边形纸和一支铅笔。您将如何手动执行此操作?您将如何决定在纸上画线的位置?
  • 该代码是对应于每个单元格还是对应于 JPanel? Graphics2D.drawLine 实际上记录在Graphics 中。如果我理解正确,您想将整个单元格涂成白色,并将墙壁涂成黑色,以防它们在那里,对吗?
  • 该代码用于 JPanel。我得到了迷宫的所有单元格(已经解决),我想绘制迷宫的墙壁,迭代每个单元格。用黑色绘制墙壁就足够了。

标签: java swing maze


【解决方案1】:

Graphics 类的 docs 说:

图形笔从其经过的路径向右下方垂下。

因此,如果您尝试在迷宫的右侧边缘绘制单元格的右侧边框,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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2011-06-10
    • 1970-01-01
    • 2011-09-22
    相关资源
    最近更新 更多