• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flood fill Algorithm not working Properly

 
Digen Mahara
Greenhorn
Posts: 23
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Digen Mahara
Greenhorn
Posts: 23
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
sss.jpg
[Thumbnail for sss.jpg]
this is how the algorithm works see both the circles on the panel
 
Rob Camick
Ranch Hand
Posts: 2566
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Wikipedia Flood Fill for non-recursive algorithms. I'm sure if you then search the web you will find implementations of these algorithms.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic