Hi,
I have a map made out of provinces. Those provinces look like this, for example:
http://upl.silentwhisper.net/uplfolders/upload0/shape_7428.gif
For filling the interrior of this shape I think the only solution is using flood fill. Since there isn’t any such method in Java, I made one myself
public void floodFill(BufferedImage img, int x, int y, Color color, Color stopColor)
{
img.setRGB(x,y,color.getRGB());
if ( (x-1 >= 0) && (img.getRGB(x-1,y) != stopColor.getRGB()) && (img.getRGB(x-1,y) != color.getRGB()) )
floodFill(img,x-1,y,color,stopColor);
if ( (x+1 < img.getWidth()) && (img.getRGB(x+1,y) != stopColor.getRGB()) && (img.getRGB(x+1,y) != color.getRGB()) )
floodFill(img,x+1,y,color,stopColor);
if ( (y-1 >= 0) && (img.getRGB(x,y-1) != stopColor.getRGB()) && (img.getRGB(x,y-1) != color.getRGB()) )
floodFill(img,x,y-1,color,stopColor);
if ( (y+1 < img.getHeight()) && (img.getRGB(x,y+1) != stopColor.getRGB()) && (img.getRGB(x,y+1) != color.getRGB()) )
floodFill(img,x,y+1,color,stopColor);
}
This does work in some cases, but in “larger” shapes (a little bit larger than the image I posted) I get a StackOverflowError. Obviously, I cannot fill the provinces like this.
And I would be doing this frequently, as the provinces change ownership, so I’d have to fill them with different color. I noticed that the Graphics2D class has a fill(Shape s) method, so prhaps this would be useful? But then I’d have to detect the edges of each province.
Any advice? ???