Loading...
Logo
Processing Forum
how to make a fill bucket tool

I actually already found some code but is is not mine and I don't even understand it
I got from openProcessing here 

http://www. openprocessing.org/sketch/67453

I also found one done by  PhiLho a user who has helped me about two or three times around here 


truthfully I have not looked through PhiLhos code as I just found and I thought wow that is long as it is 3 .PDE files I just wanted a short method like the one below 

so if you have any suggestions I will be gratefull

I understand the code below for most part I know it looks at the pixel 
where the mouse has clicked and checks that its color is not the same as the fill color and changes it if not, then does that for the surrounding pixels 
the part I don't get is  PXList and what it is used for and the shifting of vectors 
I'm very at shifting I read about on the Processing site but it is still confusing

remember I have just started doing stuff on processing just a bit over a week ago


Copy code
  1. public void Fill(int x, int y) {
  2.  
  3.  if(get(x,y) != SelectedColour) {
  4.   
  5.  int PXList[] = new int[2 * height * width];
  6.  int PXListSize = 0;

  7.  PXList[0] = (y << 16) + x;
  8.  PXListSize = 1;

  9.  set(x, y, SelectedColour);

  10.  while (PXListSize > 0){

  11.    x = PXList[0] & 0xffff;
  12.    y = (PXList[0] >> 16) & 0xffff;

  13.    PXListSize--;
  14.    PXList[0] = PXList[PXListSize];

  15.    if (x > 0) {
  16.      if ((get(x-1, y) == FillColour)) {
  17.        set(x-1, y, SelectedColour);
  18.        PXList[PXListSize] = (y << 16) + x-1;
  19.        PXListSize++;
  20.      }
  21.    }

  22.    if (y > 0) {
  23.      if ((get(x, y-1) == FillColour)) {
  24.        set(x, y-1, SelectedColour);
  25.        PXList[PXListSize] = ((y-1) << 16) + x;
  26.        PXListSize++;
  27.      }
  28.    }

  29.    if (x < width) {
  30.      if ((get(x+1, y) == FillColour)) {
  31.        set(x+1, y, SelectedColour);
  32.        PXList[PXListSize] = (y << 16) + x+1;
  33.        PXListSize++;
  34.      }
  35.    }

  36.    if (y < height) {
  37.      if ((get(x, y+1) == FillColour)) {
  38.        set(x, y+1, SelectedColour);
  39.        PXList[PXListSize] = ((y+1) << 16) + x;
  40.        PXListSize++;
  41.      }
  42.    }
  43.  }
  44.  }

  45. }

Replies(3)

You need to learn to walk before you can run :)

Analyse the task on paper and break it down into manageable chunks then figure out what aspects of Processing you need to learn.

For a simple flood-fill I don't think you'll need to know anything about vectors but learning about lists will be useful - one algorithm is to check adjacent pixels to see if they need changing and if so add them to a list. Once you've checked the adjacent pixels you step through the list and check the adjacent pixels of list items and if they need changing add them to the list. Eventually you end up with no more pixels to check and a list of pixels that need to be changed.

I have a slab of code that I could post (it's actually for path-finding) but I think it'll just cloud the issue for you.

Hope thats some help at least.
The PXList array is used to strore the coordinates of any pixel that has been flood filled BUT still needs ts neighbouring pixels to be checked and PXListsize the number of pixel positions in PXList still to test.

Each element of the array is a 32bit integer and the author has coded this so that the upper 16 bits are used for the y position and the lower 16 bits for the x position. It means that to encode /decode a pixel position requires bit shifting.