Java Floodfill真的很慢

时间:2015-07-28 17:47:46

标签: java flood-fill

我正在制作一个绘画应用程序并且洪水填充工具可以工作,但它需要大约两分钟来填充400x180。我该怎么做才能加快这个过程?以下是我目前使用的代码。

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();
}

400x180矩形的输出为148566毫秒。我有办法加速这个过程吗?任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

这是你的问题:

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

您需要多次添加每个像素(此处一次,对于该特定像素周围的每个块一次),并在每次重新添加时重新检查它。如果你有一个4x4块或其他东西,你真的不会注意到减速,但当我们谈论添加400x180(72,000)像素并检查每个像素3或4次时,它会变成巨大。

我的建议非常简单:在添加之前检查。或者甚至更好,做一个小小的&#34; MyPixel&#34;具有布尔值的类,在您已经检查它之后将其翻转为true。这样,你可以跳过任何数学运算,你可以做这样的事情:

if(my_pixel.has_been_checked == false)
   queue.add(my_pixel);

答案 1 :(得分:0)

您正在将像素坐标转换为String,然后将其解析出来。根据我的经验,我发现字符串连接是一种昂贵的操作。相反,只需将像素存储为java.awt.Point个对象,然后从中读取坐标。