aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Flood fill Algorithm not working Properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Flood fill Algorithm not working Properly" Watch "Flood fill Algorithm not working Properly" New topic
Author

Flood fill Algorithm not working Properly

Digen Mahara
Greenhorn

Joined: Dec 23, 2011
Posts: 23

I want to use the flood fill Algorithm in my project which is a Paint Application
I searched on the Internet and got the following recursive code :


it works great with small shapes but problem arises with large shapes
alse it returns some error or exception
Please help me modify it


To every Tunnel there is always a way out
So keep Trying...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

What error / exception do you get?

My guess is that you get a StackOverflowError. When you call floodFill(3, 3, c1, c2), that will call floodFill(2, 3, c1, c2). If this also matches then it in turn will call floodFill(3, 3, c1, c2). This won't end.

You need to keep track of all the pixels you already handled, or otherwise ensure that you only handle each pixel once.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Digen Mahara
Greenhorn

Joined: Dec 23, 2011
Posts: 23

(DirectColorModel.java:455)
at java.awt.image.DirectColorModel.getRGB(DirectColorModel.java:721)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:888)
at paint.Painter.floodFill(Painter.java:406)
at paint.Painter.floodFill(Painter.java:408)
at paint.Painter.floodFill(Painter.java:411)
at paint.Painter.floodFill(Painter.java:409)


the above is the error that gets generated

and if the stack memory gets overflow the how to stop it is there any method or technique in Java

and its not possible to keep a track of each and every pixel since in large regular/irregular shapes it is impossible

I wish I could insert the image to make my error understand better??


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

That's not a stack trace, that's only part of it. Not even the most interesting part which always comes at the top.

It's most definitely possible to keep track of all the pixels you've already changed. You just need to find a way how to. The most basic way is to create a boolean[][] with the same dimensions as your image, then set the right value if you've encountered it. That's probably not the most efficient way though.

As for adding images, you can't link to images on your local hard drive. The Ranch allows you to upload image attachments; just check the Attachments tab just below the post field.
Digen Mahara
Greenhorn

Joined: Dec 23, 2011
Posts: 23

As for adding images, you can't link to images on your local hard drive. The Ranch allows you to upload image attachments; just check the Attachments tab just below the post field.



[Thumbnail for sss.jpg]

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
See Wikipedia Flood Fill for non-recursive algorithms. I'm sure if you then search the web you will find implementations of these algorithms.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flood fill Algorithm not working Properly