I am using the recursive Flood fill algorithm in Java to fill some areas of a image. With very small images it works fine, but when de image becomes larger the JVM gives me a Stack Over Flow Error.
That's the reason why I have to reimplement the method using a Flood Fill with my own stack. (I read that's the best way to do it in these kind of cases)
Can anyone explain me how to code it? (if you don't have the code at hand, with the pseudo-code of the algorithm will be fine)
I've read a lot in the Internet but I haven't understood it very well.
EDIT: I added my recursive code
public void floodFill(int x, int y, Color targetColor,Color replacementColor) {
if (img.getRGB(x, y) != targetColor.getRGB()) return;
img.setRGB(x, y, replacementColor.getRGB());
floodFill(x - 1, y, targetColor, replacementColor);
floodFill(x + 1, y, targetColor, replacementColor);
floodFill(x, y - 1, targetColor, replacementColor);
floodFill(x, y + 1, targetColor, replacementColor);
return;
}
Thanks!