We help IT Professionals succeed at work.

Check out this week's podcast, "Dairy Farms to Databases: Community's Hand in Technology"Listen Now


Flood Fill in Java

bmccorkle asked
Last Modified: 2008-03-10
Does anyone know of a routine to do a flood fill in Java?  By flood fill I mean the same as "paint can" fill in a graphics program.  Basically, a flood fill identifies the key color at a specified coordinate.  The routine then changes pixels of the key color to a new color spreading in all directions until a pixel not the key color is found.
Watch Question


When you say flood fill, do you mean filling a region
with color?  If so, then you want the fillRect() method
in the Graphics class.  If you mean something else, could
you clarify a bit?



Edited text of question

The biggest problem you will have with this is that Java has no way to read the current screen, so you have to use offscreen bitmaps.
This one is on us!
(Get your first solution completely free - no credit card required)
Actually, there is a way to get a pixels color in Java, its just not an obvious way:

public int getPixel(Graphics img, int x, int y, int w, int h){
// under ordinary circumstances w and h will be = 1 unless you need a larger pixel for some reason. Of course, you would then have to return either an int[] or the average of the pixel colors (after breaking them down to their rgb component and reconstituting) depending on your need.

   int[] pixels = new int[w * h];
   PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
   return pixels[0];

the return value contains an rgb value that contains the color of the pixel. channels and I'll post some more code).

Once you know how to read a pixel from the immage, doing a flood fill is fairly easy. Just use the following algorithm:

//Point Seed(x,y) is the seed pixel, i.e.: a coordinate pair known to be inside the surface to be filled.

//int colorNew = Is the fill color, Int colorBorder is the boundry color

//In a normal flood fill, you would not need the background color , but here you want to respect the shadows if they were drawn before hand
// so we will need to have a flag to treat shadows as background color (when drawing shadows) or as borders when drawing the points) bool flag;

// Say flag = false means overwrite the shadows.

// g is a Graphics param obtained from the pannels paint() method.

Stack St;
Point pixel(Seed);
while (St.empy()){
   pixel = St.Pop();
   int c = getPixel(g, pixel.x, pixel.y, 1, 1);
   if ((c <> colorNew) and !flag) img.drawLine(pixel.x, pixel.y, pixel.x, pixel.y);
      int c = getPixel(img, pixel.x + 1, pixel.y, 1, 1);
      if ((c <> colorNew) and ( c <> color Border)){
         Point newPixel(pixel);
         newPixel.x += 1;

      int c = getPixel(img, pixel.x, pixel.y + 1, 1, 1);
      if ((c <> colorNew) and ( c <> color Border)){
         Point newPixel(pixel);
         newPixel.y += 1;

      int c = getPixel(img, pixel.x - 1, pixel.y, 1, 1);
      if ((c <> colorNew) and ( c <> color Border)){
         Point newPixel(pixel);
         newPixel.x -= 1;

      int c = getPixel(img, pixel.x, pixel.y - 1, 1, 1);
      if ((c <> colorNew) and ( c <> color Border)){
         Point newPixel(pixel);
         newPixel.y -= 1;

Hope this added input helps.


The biggest problem with an approach like that is that
the PixelGrabber is *slow*.  If you have to do that on
any kind of a regular basis, your application or applet
will just *grind* to a halt.  At least, that's been my

When managing a screen like that, keeping track of what
you need to redraw will perform much better.

As rembo states quite correctly, pixelGrabber IS slow, and even if it were not, it is always wise to keep track of what your drawing whenever possible.

In a freehand draw program though, this is not always practical, so flood fills become more important. Regarding the grinding to a halt though, that would depend on how big a graphics area is beeing filled in. Since the pixelGrabber is beeing used to read the value of a single pixel, most of the delay will be caused by stack code overhead, rather than the method itself, so for small graphics areas it should work well enough.

At any rate, the purpose of my posting was to rectify russgold's comment regarding Java not beeing able to read the screen. It can, albeit not in an efficient manner.

 Since that kind of comment has caused confusion in the past in some questions from the Previously Answered list, I felt it was justified to clarify that particular point.


jpk is absolutely correct.  Unfortunately, these being
the only two solutions to this kind of problem is something
we should really bring up with JavaSoft.  Trying to do things
like real, useful paint programs in Java is a bit painful.

Sounds like you've done some stuff like this, I wrote a
paint program, and I had to store all this stuff in a Vector.
There really should be a better way.

Just venting :)

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.