私は、JavaのBufferedImageクラス用のペイントバケットツールのように機能する関数を作成しています。これは再帰を使用して塗りつぶしを実行します。残念ながら、コードを実行すると、java.lang.StackOverflowErrorが返されます。私はBaseColorの "赤"カラーチャンネルをチェックするためにSystem.out.printlnを使用したときに、255であったはずの場所にゼロを与えたように、BaseColorを認識しないことも発見しました。白)。コードは次のとおりです。StackOverflowError Java "Fill Image"関数
public static void BufferedImageFill(BufferedImage bufferedImage, int FillX, int FillY, int FillRed, int FillGreen, int FillBlue, int FillAlpha, int Tolerance, boolean IsFirstPixel, Color BaseColor) {
if (IsFirstPixel == true) {
BaseColor = new Color(RGBAValuesToInt(BufferedImageGetPixelARGB(bufferedImage, "R", FillX, FillY), BufferedImageGetPixelARGB(bufferedImage, "G", FillX, FillY), BufferedImageGetPixelARGB(bufferedImage, "B", FillX, FillY), BufferedImageGetPixelARGB(bufferedImage, "A", FillX, FillY)));
}
if (FillX >= 0 && FillY >= 0 && FillX < bufferedImage.getWidth() && FillY < bufferedImage.getHeight()) {
int ThisR = BufferedImageGetPixelARGB(bufferedImage, "R", FillX, FillY);
int ThisG = BufferedImageGetPixelARGB(bufferedImage, "G", FillX, FillY);
int ThisB = BufferedImageGetPixelARGB(bufferedImage, "B", FillX, FillY);
if (Math.abs(ThisR - BaseColor.getRed()) <= Tolerance && Math.abs(ThisG - BaseColor.getGreen()) <= Tolerance && Math.abs(ThisB - BaseColor.getBlue()) <= Tolerance) {
bufferedImage.setRGB(FillX, FillY, RGBAValuesToInt(FillRed, FillGreen, FillBlue, FillAlpha));
BufferedImageFill(bufferedImage, FillX - 1, FillY - 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor);
BufferedImageFill(bufferedImage, FillX - 1, FillY, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor);
BufferedImageFill(bufferedImage, FillX - 1, FillY + 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor);
BufferedImageFill(bufferedImage, FillX, FillY + 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor);
BufferedImageFill(bufferedImage, FillX, FillY - 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor);
BufferedImageFill(bufferedImage, FillX + 1, FillY - 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor);
BufferedImageFill(bufferedImage, FillX + 1, FillY, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor);
BufferedImageFill(bufferedImage, FillX + 1, FillY + 1, FillRed, FillGreen, FillBlue, FillAlpha, Tolerance, false, BaseColor);
}
}
}
これはなぜ起こっているのですか?何か助けてくれてありがとう!
今私は洞察に感謝します! – neilf