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