Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Flood fill Algorithm not working Properly

 
Greenhorn
Posts: 23
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3263
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic