0

I am making a paint application and the flood fill tool works, but it takes about two minutes for it to fill a 400x180. What can I do to speed up this process? Here is the code I am currently using for this.

public void gradientSize(int x, int y, int origRGB, int index){
    queue = new ArrayList<String>(); //queue is an ArrayList<String> that holds the points
    time = System.currentTimeMillis(); // time is a long so I can calculate the time it takes to finish a flood fill
    if(new Color(origRGB).equals(foreground)){ //foreground is the color the flood fill is using to fill in. origRGB is the RGB of the color I clicked
        return;
    }
    if(!testFill(x, y, origRGB)){
        return;
    }
    queue.add(pixel(x,y));
    while(!queue.isEmpty()){
        String pixel = queue.get(0);
        int x2 = Integer.parseInt(pixel.substring(0, pixel.indexOf(","))); 
        int y2 = Integer.parseInt(pixel.substring(pixel.indexOf(",")+1,pixel.length()));
        queue.remove(0);
        if(testFill(x2, y2, origRGB)){
            queue.add(pixel(x2+1, y2));
            queue.add(pixel(x2-1,y2));
            queue.add(pixel(x2,y2+1));
            queue.add(pixel(x2,y2-1));
            gradientPoints.add(pixel(x2, y2)); //gradientPoints is an ArrayList<String> that contains all the points for the fill
            processed[y*image.getWidth()+x] = true; //processed[] is a boolean array that has a true or false value for each pixel to determine if it has been looked at yet.
        }
    }
}

public boolean testFill(int x, int y,int origRGB){ //testFill tests if the current pixel is okay to be filled or not
    if(x>=0&&x<image.getWidth()&&y>=0&&y<image.getHeight()){
        int testRGB = image.getRGB(x, y);
        Color orig = new Color(origRGB,true);
        Color test = new Color(testRGB,true);
        if ((Math.abs(orig.getRed() - test.getRed()) <= difference) && (Math.abs(orig.getGreen() - test.getGreen()) <= difference)&& (Math.abs(orig.getBlue() - test.getBlue()) <= difference)&&(Math.abs(orig.getAlpha() - test.getAlpha()) <= difference)) {
            if (!gradientPoints.contains(pixel(x,y))) {
                if (!queue.contains(pixel(x,y))) {
                    if (processed[y*image.getWidth()+x]==false) {
                        return true;
                    }
                }
            }
        }
    }
    return false;

}

public String pixel(int x, int y){//this returns the String value of a pixel's x and y coordinates.
    return String.valueOf(x)+","+String.valueOf(y);
}
public void gradientFillSolid(){ //This gets all the points from gradientPoints and fills each pixel from there.
    for(String s:gradientPoints){
        int x = Integer.parseInt(s.substring(0, s.indexOf(',')));
        int y = Integer.parseInt(s.substring(s.indexOf(',')+1,s.length()));
        image.setRGB(x, y, foreground.getRGB());
    }
    System.out.println(System.currentTimeMillis()-time);
    repaint();
}

The output for a 400x180 rectangle was 148566 milliseconds. Is there a way for me to speed up this process at all? Any help is appreciated.

Jaboyc
  • 567
  • 1
  • 8
  • 16

2 Answers2

2

Here's your problem:

queue.add(pixel(x2+1, y2));
queue.add(pixel(x2-1,y2));
queue.add(pixel(x2,y2+1));
queue.add(pixel(x2,y2-1));

You're adding every pixel multiple times (once here, and once for every block around that particular pixel) and rechecking it every time it's added again. If you had a 4x4 block, or something, you really wouldn't notice a slowdown, but when we're talking about 400x180 (72,000) pixels being added and checked 3 or 4 times per pixel, it gets to be huge.

My suggestion is very simple: Check before you add. Or even better, make a small little "MyPixel" class that has a boolean value that is flipped to true after you've already checked it. That way, you can skip doing any math on it and you can just do something like this:

if(my_pixel.has_been_checked == false)
   queue.add(my_pixel);
Lee Presswood
  • 210
  • 2
  • 15
0

You are converting the pixel coordinates to a String, then parsing them back out. I have found in my experience that string concatenation is an expensive action. Instead, just store pixels as java.awt.Point objects and read the coordinates from those.

Adam Evans
  • 2,072
  • 1
  • 20
  • 29