public void floodFill(int x, int y) {
if (x >= 0 && x <= 9 && y >= 0 && y <= 9) {
if (mines[x][y] == 0) {
btn[x][y].setBackground(Color.GRAY);
floodFill(x - 1, y);
// floodFill(x + 1, y);
floodFill(x, y - 1);
// floodFill(x, y + 1);
} else {
return;
}
}
}
这是我正在制作的类似扫雷游戏的 floodFill 代码。但是,如您所见,有 2 个部分被注释掉了。一旦我取消注释并运行程序。
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.awt.NullComponentPeer.setBackground(Unknown Source)
at java.awt.Component.setBackground(Unknown Source)
at javax.swing.JComponent.setBackground(Unknown Source)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:95)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:99)
at Minesweeper.BoardBuild.floodFill(BoardBuild.java:98)
我不确定为什么会这样,也不知道如何解决。任何帮助表示赞赏。
编辑:
Fls'Zen 在聊天中给出的答案,以防万一其他人遇到这个问题
public void floodFill(int x, int y) {
if (x >= 0 && x <= 9 && y >= 0 && y <= 9) {
if (mines[x][y] == 0 && btn[x][y].getBackground() != Color.GRAY) {
btn[x][y].setBackground(Color.GRAY);
floodFill(x - 1, y);
floodFill(x + 1, y);
floodFill(x, y - 1);
floodFill(x, y + 1);
} else {
return;
}
}
}