{
if(oldColor == newColor) return;
if(getColor(x, y) != oldColor) return;
int y1;
//draw current scanline from start position to the top
y1 = y;
while(y1 < height && getColor(x, y1) == oldColor)
{
setColor(x, y1, newColor);
y1++;
}
//draw current scanline from start position to the bottom
y1 = y - 1;
while(y1 >= 0 && getColor(x, y1) == oldColor)
{
setColor(x, y1, newColor);
y1--;
}
//test for new scanlines to the left
y1 = y;
while(y1 < height && getColor(x, y1) == newColor)
{
if(x > 0 && getColor(x - 1, y1) == oldColor)
{
floodFillScanLine(x - 1, y1, newColor, oldColor);
}
y1++;
}
y1 = y - 1;
while(y1 >= 0 && getColor(x, y1) == newColor)
{
if(x > 0 && getColor(x - 1, y1) == oldColor)
{
floodFillScanLine(x - 1, y1, newColor, oldColor);
}
y1--;
}
//test for new scanlines to the right
y1 = y;
while(y1 < height && getColor(x, y1) == newColor)
{
if(x < width - 1 && getColor(x + 1, y1) == oldColor)
{
floodFillScanLine(x + 1, y1, newColor, oldColor);
}
y1++;
}
y1 = y - 1;
while(y1 >= 0 && getColor(x, y1) == newColor)
{
if(x < width - 1 && getColor(x + 1, y1) == oldColor)
{
floodFillScanLine(x + 1, y1, newColor, oldColor);
}
y1--;
}
}
public void floodFillScanLine(int x, int y, int newColor, int oldColor)
{
if(oldColor == newColor) return;
if(getColor(x, y) != oldColor) return;
int y1;
//draw current scanline from start position to the top
y1 = y;
while(y1 < height && getColor(x, y1) == oldColor)
{
setColor(x, y1, newColor);
y1++;
}
//draw current scanline from start position to the bottom
y1 = y - 1;
while(y1 >= 0 && getColor(x, y1) == oldColor)
{
setColor(x, y1, newColor);
y1--;
}
//test for new scanlines to the left
y1 = y;
while(y1 < height && getColor(x, y1) == newColor)
{
if(x > 0 && getColor(x - 1, y1) == oldColor)
{
floodFillScanLine(x - 1, y1, newColor, oldColor);
}
y1++;
}
y1 = y - 1;
while(y1 >= 0 && getColor(x, y1) == newColor)
{
if(x > 0 && getColor(x - 1, y1) == oldColor)
{
floodFillScanLine(x - 1, y1, newColor, oldColor);
}
y1--;
}
//test for new scanlines to the right
y1 = y;
while(y1 < height && getColor(x, y1) == newColor)
{
if(x < width - 1 && getColor(x + 1, y1) == oldColor)
{
floodFillScanLine(x + 1, y1, newColor, oldColor);
}
y1++;
}
y1 = y - 1;
while(y1 >= 0 && getColor(x, y1) == newColor)
{
if(x < width - 1 && getColor(x + 1, y1) == oldColor)
{
floodFillScanLine(x + 1, y1, newColor, oldColor);
}
y1--;
}
}
基于递归实现的泛洪填充算法有个致命的缺点,就是对于大的区域填充时可能导致JAVA栈溢出
错误,对最后一种基于扫描线的算法,实现了一种非递归的泛洪填充算法。
[java]
public void floodFillScanLineWithStack(int x, int y, int newColor, int oldColor)
{
if(oldColor == newColor) {
System.out.println("do nothing !!!, filled area!!");
return;
}
emptyStack();
int y1;
boolean spanLeft, spanRight;
push(x, y);
while(true)
{
x = popx();
if(x == -1) return;
y = popy();
y1 = y;
while(y1 >= 0 && getColor(x, y1) == oldColor) y1--; // go to line top/bottom
y1++; // start from line starting point pixel
spanLeft = spanRight = false;
while(y1 < height && getColor(x, y1) == oldColor)
{
setColor(x, y1, newColor);
if(!spanLeft && x > 0 && getColor(x - 1, y1) == oldColor)// just keep left line once in the stack