I'm having trouble with the flood fill algorithm. I keep getting StackOverflowError error and I can't figure out why. Sometimes it works and sometimes it doesn't. Is there something wrong with my code?
Thanks!
I've tried filling random things in my image and it seems to work for random cases, other times it doesn't.
public void fill(int x, int y, Color c1, Color c2){
if(x < 0) return;
if(y < 0) return;
if(x >= img.getWidth()) return;
if(y >= img.getHeight()) return;
if(c1.getRGB()==c2.getRGB()) return;
if(grid[x][y].getRGB()!=c1.getRGB())return;
grid[x][y] = c2;
fill(x+1,y,c1,c2);
fill(x,y+1,c1,c2);
fill(x,y-1,c1,c2);
fill(x-1,y,c1,c2);
}