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();
}