Floodfill StackOverFlow, IDEA

Viewed 132

I'm trying to programme flood fill but the is any problem with recursion. Error msg says: "Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError"

Here is my code:

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

and in canvas:

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

Is there any posible to change XSS in IDEA? I've remembered in Eclipse was something like it.(-XSS100M)

currentPixel is collor of canva's background (0x2f2f2f).

EDIT: In previousX and Y is int position of cursor from listener.

EDIT SOLVED: The problem was thar current pixel did not took actual value of color. It has const. 0x2f2f2f so comparation was nosence. :).. thanks everybody

2 Answers

To set the arg Xss, in intelliJ, you can do: To define Configuration options of a run/debug configuration

  1. Click Configuration tab of the Edit Run/Debug Configurationdialog.
  2. In the Main class field, specify the class that contains the main() method. To do that, type the fully qualified name manually, or click the ellipsis button and select the desired class from the Choose Main Class dialog. In the Choose Main Class dialog, you can locate the desired class using one of the following ways:
  3. Click the Project tab, and select class with the main() method from the project tree view.
  4. Click the Search by Name tab and start typing the class name. As you type, the list of available classes narrows down to match your entry. Click OK, or press Enter when ready.
  5. In the VM options field, type optional VM arguments, for example the heap size, garbage collection options, file encoding, etc. If the line of VM arguments is too long, click /help/img/idea/2017.2/editorIcon.gif and type the text in the editor dialog.
  6. In the Program parameters field, type optional list of parameters that should be passed to the main() method through the array of its arguments.
  7. In the Working directory field, specify the current directory that your application will use while running.
  8. In the Use classpath and SDK of module field, select the desired module from the list of modules existing in the project.

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

Increasing the stack size will likely not be enough, except for very small images, so you may want to change to an iterative algorithm instead. One simple option is to have a Deque into which you stuff coordinates, then pull them out, something like the following pseudo-code:

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 
}
Related