A Flood-Fill
Sometimes we'd like a area fill algorithm that replaces all
connected pixels of a selected color with a fill color.
The flood-fill algorithm does exactly that.
public void floodFill(int x, int y, int fill, int old) {
if ((x < 0) || (x >= width)) return;
if ((y < 0) || (y >= height)) return;
if (raster.getPixel(x, y) == old) {
raster.setPixel(fill, x, y);
floodFill(x+1, y, fill, old);
floodFill(x, y+1, fill, old);
floodFill(x-1, y, fill, old);
floodFill(x, y-1, fill, old);
}
}
Flood fill is a small variant on a boundary fill. It replaces old pixels with the fill color.
|