Changing the color of black pixels in area

Hi,

I loaded a binary image (only black and white pixels) into processing. I want the user to be able to click on an area of the image (say a circle around the cursor) and each black pixel within that area then changes color.
I’ve tried to do it the following way: When a pixel is clicked it first checks whether the clicked pixel is black. If that is the case, it checks the surrounding area for black pixels and adds those to an array. After that those pixels get painted in the other color. The problem with this is that the are around the cursor can’t be too large, else the program noticeably stutters and sometimes crashes. I guess this method is too inefficient.

Is there a more efficient way to do this?

This is the code of the painting function, I hope it makes some sense:

void paintSurroundingPixels_snake(float xx, float yy, int max_dist, color cur_color) {
    //println("----------------------");
    int center = get1dcoord(int(xx),int(yy),w);
    cur_shape.pixels[center] = cur_color;
     
    ArrayList<int[]> paint_list = new ArrayList<int[]>(); // List of pixels to be painted and then checked for surrounding black pixels
    int[] coords = {int(xx),int(yy)};
    paint_list.add(coords);
    int curX, curY;
    int check_coord;
    int cur_coord_x;
    int cur_coord_y; // new Coord in snake-grid 
    ArrayList<int[]> surround_list = new ArrayList<int[]>(); // List of pixels surrounding painted pixels, to be painted in next loop iteration
    
    int check_coord_temp;
    
    for (int step = 0; step < max_dist; step++) {
      //println("----------------------");
      //println("Step: "+step+" , Paint: "+paint_list.size());
      
      // Go through each pixel in array
      for (int p = 0; p < paint_list.size(); p++) {
        curX = paint_list.get(p)[0];
        curY = paint_list.get(p)[1];
        
        // Paint current pixel
        check_coord = get1dcoord(curX, curY, w);
        cur_shape.pixels[check_coord] = cur_color;
        
        // Check surrounding pixels of current pixel
        for (int xs = -snake_grid_size; xs < snake_grid_size + 1; xs++) {
          for (int ys = -snake_grid_size; ys < snake_grid_size + 1; ys++) {
            cur_coord_x = curX + xs;
            cur_coord_y = curY + ys;
            check_coord = get1dcoord(curX + xs, curY + ys, w);
            if(int(cur_coord_x) > 0 && int(cur_coord_x) < w) {
              if(int(cur_coord_y) > 0 && int(cur_coord_y) < h) {
                  if (cur_shape.pixels[check_coord] < -2) {
                    if (cur_shape.pixels[check_coord] != cur_color) { // Dont add already painted pixels
                      coords[0] = curX + xs;
                      coords[1] = curY + ys;
                      check_coord_temp = get1dcoord(curX + xs, curY + ys, w);
                      //test.pixels[check_coord_temp] = green;
                      int[] coords_temp = new int[2];
                      coords_temp[0] = curX + xs;
                      coords_temp[1] = curY + ys;
                      surround_list.add(coords_temp);}
                  }
              }
            }
          }
        }
      }
      cur_shape.updatePixels();
      // Make new pixel list the old one and clear the new one
      paint_list = (ArrayList<int[]>)surround_list.clone();
      surround_list.clear();
    }
}

boolean check_surround(float xx, float yy, int size, color cur_color) {
  int check_coord;
  int cur_coord_x;
  int cur_coord_y;
  // Check surrounding pixels of current pixel
  for (int xs = -size; xs < size + 1; xs++) {
    for (int ys = -size; ys < size + 1; ys++) {
      cur_coord_x = int(xx) + xs;
      cur_coord_y = int(yy) + ys;
      check_coord = get1dcoord(cur_coord_x, cur_coord_y, w);
      if(int(cur_coord_x) > 0 && int(cur_coord_x) < w) {
        if(int(cur_coord_y) > 0 && int(cur_coord_y) < h) {
          if (cur_shape.pixels[check_coord] == cur_color) { // Dont add already painted pixels
            return true;
          }
        }
      }
    }
  }
  return false;
}

First off, can I ask you to format the unformatted parts of the code.

Second, where exactly does check_surround() get called?