Search code examples
javabufferedimagefillstack-overflow

StackOverflowError In Java "Fill Image" Function


I've been creating a function that acts like a paint bucket tool for the BufferedImage class in Java. It uses recursion to perform the fill. Sadly, though, when I execute the code, it gives me a java.lang.StackOverflowError. I also discovered that it doesn't recognize the BaseColor, as when I used System.out.println to check for the BaseColor's "red" color channel, it gave me a zero where there should've been a 255. (The color was white.) Here's the code:

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

Does anybody know why this is happening? Thanks for any help given!

-Neil


Solution

  • If I read the code correctly, when your method is called to fill pixel (0, 0), it will at some point call itself to fill (among other points), pixel (1, 0). That call, in turn, will call itself to fill pixel (0, 0) again. That's why you have infinite recursion. (The same problem occurs with the other adjacent points — each goes back and fills the point that led to it being filled.)