【发布时间】:2017-11-10 17:50:25
【问题描述】:
我正在尝试对洪水填充进行编程,但递归有任何问题。错误消息说:“线程中的异常“AWT-EventQueue-0”java.lang.StackOverflowError”
这是我的代码:
public class FillerSeedFill<PixelType> {
public RasterImage<PixelType> filler (RasterImage<PixelType> img,
int x, int y,
PixelType newPixel,
PixelType borderPixel,
PixelType currentPixel
){
RasterImage<PixelType> result = img;
if ( borderPixel != currentPixel){
if(currentPixel!=newPixel) {
result = result.withPixel(x, y, newPixel);
filler(img,x+1,y,newPixel,borderPixel,currentPixel);
filler(img,x-1,y,newPixel,borderPixel,currentPixel);
filler(img,x,y+1,newPixel,borderPixel,currentPixel);
filler(img,x,y-1,newPixel,borderPixel,currentPixel);
return result;
}
}
return result;
}
}
在画布中:
if(jComboBoxSelectColoring.getSelectedIndex()==0){
System.out.println("Seed fill");
int currentPixel = 0x2f2f2f;
System.out.println(currentPixel);
fillerSeedFill.filler(rasterImage,
previousX,previousY,
0xC4D4AF,
0x8AC249,
currentPixel);
System.out.println(previousX+" "+previousY);
panel.repaint();
}
IDEA 中是否可以更改 XSS?我记得在 Eclipse 中是这样的。(-XSS100M)
currentPixel 是画布背景的颜色 (0x2f2f2f)。
编辑: 在previousX 和Y 是从听者光标的int 位置。
编辑已解决: 问题是当前像素没有采用颜色的实际值。它有常量。 0x2f2f2f 所以比较很重要。 :).. 谢谢大家
【问题讨论】:
-
问题是你有 4 个递归调用,没有条件,当它命中第一个而不是调用方法直到你的堆栈溢出时
-
好的,那么您知道如何解决它吗?...我应该在每次递归调用之前添加条件吗? @maytham-ɯɐɥʇʎɐɯ
标签: java intellij-idea stack-overflow flood-fill