【问题标题】:Floodfill StackOverFlow, IDEA洪水填充 StackOverFlow,IDEA
【发布时间】: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


【解决方案1】:

要在 intelliJ 中设置 arg Xss,您可以: 定义运行/调试配置的配置选项

  1. 单击编辑运行/调试配置对话框的配置选项卡。
  2. 在 Main class 字段中,指定包含 main() 方法的类。为此,请手动键入完全限定名称,或单击省略号按钮并从“选择主类”对话框中选择所需的类。 在“选择主类”对话框中,您可以使用以下方法之一找到所需的类:
  3. 单击“项目”选项卡,然后从项目树视图中选择具有 main() 方法的类。
  4. 单击“按名称搜索”选项卡并开始输入班级名称。在您键入时,可用类的列表会缩小以匹配您的输入。 单击“确定”,或在准备好后按 Enter。
  5. 在 VM 选项字段中,键入可选的 VM 参数,例如堆大小、垃圾收集选项、文件编码等。如果 VM 参数行太长,请单击 /help/img/idea/2017.2/editorIcon .gif 并在编辑器对话框中输入文本。
  6. 在“程序参数”字段中,键入应通过其参数数组传递给 main() 方法的可选参数列表。
  7. 在工作目录字段中,指定您的应用程序在运行时将使用的当前目录。
  8. 在使用模块的类路径和 SDK 字段中,从项目中现有的模块列表中选择所需的模块。

来源: https://www.jetbrains.com/help/idea/setting-configuration-options.html

【讨论】:

  • 好吧,我添加到程序参数 -XSS100M,但仍然没有工作:/.. 你还有其他想法吗?
【解决方案2】:

增加堆栈大小可能还不够,除了非常小的图像,因此您可能希望改为使用迭代算法。一个简单的选择是有一个双端队列,你可以在其中填充坐标,然后将它们拉出来,类似于以下伪代码:

Deque<Point> queue = new ArrayDeque<>();
queue.add(new Point(x, y));
while (!queue.isEmpty()) {
    Point pt = queue.poll();
    // then do the same thing you were already doing, except use pt.x and pt.y,
    // and add new points to the queue instead of recursive calling 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多