0
我正在实现一个简单的种子填充算法使用递归调用。问题是,它抛出这个异常的递归调用:种子填充使用递归调用在Java中 - Stackoverflow错误
异常在线程“AWT-EventQueue的 - 0” java.lang.StackOverflowError的
我试图填补只小的空间,所以堆栈大小不应该是一个问题。有人可以弄清楚,为什么抛出异常?
的seedfill类
package rasterops;
import rasterdata.RasterImage;
import java.awt.*;
import java.awt.image.Raster;
import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
public class SeedFill<PixelType> {
public RasterImage fill(final RasterImage<PixelType> img, final int x, final int y, PixelType borderColor, PixelType fillColor, PixelType bcgColor) {
RasterImage<PixelType> result = img;
PixelType color = (PixelType) img.getPixel(x,y);
if(bcgColor == color || color != borderColor){
result = result.withPixel(x,y, fillColor);
fill(img, x+1, y, borderColor, fillColor,bcgColor);
fill(img, x-1, y, borderColor, fillColor,bcgColor);
fill(img, x, y+1, borderColor, fillColor,bcgColor);
fill(img, x, y-1, borderColor, fillColor,bcgColor);
}
return result;
}
}
使用填充()方法在画布
if(tool == 3){
rasterImage = seedfill.fill(rasterImage, e.getX(), e.getY(), 0xffff00, 0x00ff00, 0x2f2f2f);
panel.repaint();
}