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....

5
  • Why not Graphics2D#fill(Shape s) instead? Commented Apr 12, 2014 at 13:42
  • i have to use this algorithm only :( Commented Apr 12, 2014 at 13:43
  • Please edit your question to include an mcve that exhibits the problem you describe. In your code, access posted images via URL, as shown here; use synthetic images as shown here; or use UIManager icons, as shown here. Commented Apr 12, 2014 at 13:46
  • i have included the image.... Commented Apr 12, 2014 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. Commented Apr 12, 2014 at 13:59

1 Answer 1

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));
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.