2

i have implemented this algorithm that creates a shape using mouse clicks and then u can fill the shape with a color using the boundary fill algorithm.... Only part of the shape is filled and then i get this error : enter image description here Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at java.util.HashMap.getEntry(Unknown Source) at java.util.HashMap.get(Unknown Source) at sun.awt.AppContext.get(Unknown Source) at com.sun.java.swing.SwingUtilities3.getDelegateRepaintManager(Unknown Source) at javax.swing.RepaintManager.getDelegate(Unknown Source) at javax.swing.RepaintManager.addDirtyRegion(Unknown Source) at javax.swing.JComponent.repaint(Unknown Source) at java.awt.Component.repaint(Unknown Source)

Any idea whats wrong? Here is the boundary fill algorithm im using....

    public void BoundaryFill(int x, int y, Color bColor, Color fColor){
        int current = bI.getRGB(x, y);
        if((current != bColor.getRGB()) && (current != fColor.getRGB())){
            //bI.setRGB(x, y, fColor.getRGB());
            bI.setRGB(x, y, fColor.getRGB());

            repaint();

            BoundaryFill(x+1, y, bColor, fColor);

            BoundaryFill(x-1, y, bColor, fColor);

            BoundaryFill(x, y-1, bColor, fColor);

            BoundaryFill(x, y+1, bColor, fColor);


        }
        else
            return;
    }

Note that the x and y parameters are the coordinates where the mouse is clicked and the filling takes place....

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user2228135
  • 239
  • 6
  • 17
  • Why not `Graphics2D#fill(Shape s)` instead? – trashgod Apr 12 '14 at 13:42
  • i have to use this algorithm only :( – user2228135 Apr 12 '14 at 13:43
  • Please edit your question to include an [mcve](http://stackoverflow.com/help/mcve) that exhibits the problem you describe. In your code, access posted images via `URL`, as shown [here](http://stackoverflow.com/a/10862262/230513); use synthetic images as shown [here](http://stackoverflow.com/a/15982915/230513); or use `UIManager` icons, as shown [here](http://stackoverflow.com/a/12228640/230513). – trashgod Apr 12 '14 at 13:46
  • i have included the image.... – user2228135 Apr 12 '14 at 13:51
  • The method is called for one point, and that causes the method to be called for its neighbors, which causes the method to be called for its neighbors, etc. Given the big numbers of points to update, and the big number of operations executed for each point (repaint()), I'm not surprised you're hitting the limits of the stack. Don't use recursion. Use iteration. – JB Nizet Apr 12 '14 at 13:59

1 Answers1

7

Answer is simple, you are causing stack overflow. This algorithm is doing lot of recursive calls for big image. You can try similar algorithm but using points stack instead of calling recursive methods. Sample with points stack:

    public void BoundaryFill(int initialX, int initialY, Color bColor, Color fColor){
    Stack<Point> points = new Stack<>();
    points.add(new Point(initialX, initialY));

    while(!points.isEmpty()) {
        Point currentPoint = points.pop();
        int x = currentPoint.x;
        int y = currentPoint.y;

        int current = bI.getRGB(x, y);
        if((current != bColor.getRGB()) && (current != fColor.getRGB())){
            //bI.setRGB(x, y, fColor.getRGB());
            bI.setRGB(x, y, fColor.getRGB());

            repaint();

            points.push(new Point(x+1, y));
            points.push(new Point(x-1, y));
            points.push(new Point(x, y+1));
            points.push(new Point(x, y-1));
        }
    }
}
enterbios
  • 1,725
  • 12
  • 13
  • just upvoted! I don't know if it's the place but I need to know... is using a stack mostly the best solution to convert a recursive function/method that cause a stack overflow into a non recursive one? – Paiku Han Sep 03 '21 at 18:19