title: Modified Flood Fill algorithm produces an Image StackOverflowError
body: <p>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.</p>
  <p>'''</p>
  <pre><code>public static void edgeFill(BufferedImage img, int x, int y, Color findColor, Color replaceColor, ArrayList&lt;coordinate&gt; visited) {
          if (x&lt;0 || x&gt;img.getWidth() || y&lt;0 || y&gt;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);
      }
  </code></pre>
  <p>'''
  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.</p>
  <p>When I run it, it produces the error
  '''</p>
  <pre><code>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)
  </code></pre>
  <p>'''</p>
  <p>floodFillTesting.java:36 is the if(img.getRGB()...) line.
  I also have a recursive floodFill method that works just fine</p>
  <p>'''</p>
  <pre><code>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() &amp;&amp; o.getY() == this.getY()) {
              return true;
          }
          return false;
      }
      
  }
  </code></pre>
  <p>'''
  Example of error
  '''</p>
  <pre><code>File file = new File(&quot;C:\\Users\\johns\\OneDrive\\Desktop\\Other\\EU4 Mod\\test.png&quot;);
          BufferedImage img = ImageIO.read(file);
          edgeFill(img, 200, 200, new Color(255,255,255), new Color(255,0,0), new ArrayList&lt;Coordinate&gt;());
          
          file = new File(&quot;C:\\Users\\johns\\OneDrive\\Desktop\\Other\\EU4 Mod\\test_mod.png&quot;);
          ImageIO.write(img, &quot;png&quot;, file);
  </code></pre>
  <p>'''
  <a href="https://i.stack.imgur.com/OgqEu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OgqEu.png" alt="test image input" /></a>
  <a href="https://i.stack.imgur.com/D8Rfx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D8Rfx.png" alt="test output image, expected output, 1 pixel wide red on border" /></a>
  edit: added Coordinate class
  edit2: added example</p>