'Modified Flood Fill algorithm produces an Image StackOverflowError

I am writing a function to fill the edge of an area with another color. Basically, if I have a black donut with a white inside on a PNG and tell it to fill the center, the edge between the black and white should be another color, red in my test.

'''

public static void edgeFill(BufferedImage img, int x, int y, Color findColor, Color replaceColor, ArrayList<coordinate> visited) {
        if (x<0 || x>img.getWidth() || y<0 || y>img.getHeight()) { return; }
        if (visited.contains(new coordinate(x,y))) { return; }
        if (img.getRGB(x, y) != findColor.getRGB()) {
            img.setRGB(x, y, replaceColor.getRGB());
            return;
        }
        visited.add(new coordinate(x,y));
        edgeFill(img, x-1, y, findColor, replaceColor, visited);
        edgeFill(img, x+1, y, findColor, replaceColor, visited);
        edgeFill(img, x, y-1, findColor, replaceColor, visited);
        edgeFill(img, x, y+1, findColor, replaceColor, visited);
    }

''' img is a BufferedImage object of a png file (200x200). int x and y are the start cords. Color findColor is the color to find to be made a border around (white). Color replaceColor is the boarder color (black). visited is a arraylist of coordinates the function has been to.

When I run it, it produces the error '''

java.lang.StackOverflowError
    at java.desktop/java.awt.image.ComponentColorModel.getRGB(ComponentColorModel.java:1132)
    at java.desktop/java.awt.image.BufferedImage.getRGB(BufferedImage.java:917)
    at main.floodFillTesting.edgeFill(floodFillTesting.java:36)

'''

floodFillTesting.java:36 is the if(img.getRGB()...) line. I also have a recursive floodFill method that works just fine

'''

import java.awt.Color;

public class coordinate {
    private int x,y;
    public coordinate(int x, int y) {
        this.x = x; 
        this.y = y;
    }
    
    public int getX() { return this.x; }
    public int getY() { return this.y; }
    
    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
    
    @Override
    public boolean equals(Object O) {
        Coordinate o = (Coordinate) O;
        if (o.getX() == this.getX() && o.getY() == this.getY()) {
            return true;
        }
        return false;
    }
    
}

''' Example of error '''

File file = new File("C:\\Users\\johns\\OneDrive\\Desktop\\Other\\EU4 Mod\\test.png");
        BufferedImage img = ImageIO.read(file);
        edgeFill(img, 200, 200, new Color(255,255,255), new Color(255,0,0), new ArrayList<Coordinate>());
        
        file = new File("C:\\Users\\johns\\OneDrive\\Desktop\\Other\\EU4 Mod\\test_mod.png");
        ImageIO.write(img, "png", file);

''' test image input test output image, expected output, 1 pixel wide red on border edit: added Coordinate class edit2: added example



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source