更新:我找出了问题并将其写在下面。如果您想在 GitHub 上查看完整的可编译代码:https : //github.com/scohen40/cohen-mco364-fall-2018/tree/ mazeGUI/src/main/java/cohen/maze
我有一个 2D 单元格阵列,每个单元格有 4 个墙。我的 generateMaze() 类从一个随机点开始并挖出一个迷宫。那部分工作正常,当我在控制台中打印出迷宫时,一切都很好。
我的下一个目标是在 JPanel 中使用 JComponent 绘制迷宫。问题是我得到的只是左上角的一个粗线框。这是绘画代码:
public class AnimatedMaze extends JComponent {
private Maze maze;
private int componentHeight;
private int componentWidth;
private int seventhHeight;
private int seventhWidth;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
componentHeight = this.getHeight();
componentWidth = this.getWidth();
seventhHeight = componentHeight/7;
seventhWidth = componentWidth/7;
maze = new Maze(7, 7);
g.setColor(Color.black);
paintMaze(g);
}
/**
* The paintMaze() method runs through the generated maze and paints the existing walls.
* @param g
*/
void paintMaze(Graphics g) {
for (int x = 0; x < maze.getHeight(); x++) {
System.out.println("|");
for (int y = 0; y < maze.getWidth(); y++) {
Cell current = maze.getMaze()[x][y];
if(current.isWestWall()) {
g.drawLine(x, y, x, y + seventhHeight);
}
if(current.isNorthWall()){
g.drawLine(x, y,x + seventhWidth, y);
}
if(current.isEastWall()) {
g.drawLine(x + seventhWidth, y, x+ seventhWidth, y + seventhHeight);
}
if(current.isSouthWall()) {
g.drawLine(x, y + seventhHeight, x + seventhWidth, y +seventhHeight);
}
}
}
}
}
您可以在控制台中看到生成的迷宫,但在 JPanel 中它只是一个盒子。
富国沪深
幕布斯6054654
相关分类