Java 爪哇填海问题

Java 爪哇填海问题,java,image-processing,libgdx,flood-fill,Java,Image Processing,Libgdx,Flood Fill,我需要写一个泛光填充算法来给黑色边框内的图像像素着色。我根据这里的一些帖子写了以下内容: private Queue<Point> queue = new LinkedList<Point>(); private int pickedColorInt = 0; private void floodFill(Pixmap pixmap, int x, int y){ //set to true for fields that have been checked

我需要写一个泛光填充算法来给黑色边框内的图像像素着色。我根据这里的一些帖子写了以下内容:

private Queue<Point> queue = new LinkedList<Point>();
private int pickedColorInt = 0;

private void floodFill(Pixmap pixmap, int x, int y){
    //set to true for fields that have been checked
    boolean[][] painted  = new boolean[pixmap.getWidth()][pixmap.getHeight()];

    //skip black pixels when coloring
    int blackColor = Color.rgba8888(Color.BLACK);

    queue.clear();
    queue.add(new Point(x, y));

    while(!queue.isEmpty()){
        Point temp = queue.remove();
        int temp_x = temp.getX();
        int temp_y = temp.getY();

        //only do stuff if point is within pixmap's bounds
        if(temp_x >= 0 && temp_x < pixmap.getWidth() && temp_y >= 0 && temp_y < pixmap.getHeight()) {
            //color of current point
            int pixel = pixmap.getPixel(temp_x, temp_y);
            if (!painted[temp_x][temp_y] && pixel != blackColor) {
                painted[temp_x][temp_y] = true;
                pixmap.drawPixel(temp_x, temp_y, pickedColorInt);

                queue.add(new Point(temp_x + 1, temp_y));
                queue.add(new Point(temp_x - 1, temp_y));
                queue.add(new Point(temp_x, temp_y + 1));
                queue.add(new Point(temp_x, temp_y - 1));

            }
        }
    }
}
private Queue=new LinkedList();
私有int pickedColorInt=0;
专用空隙漫灌(Pixmap Pixmap、int x、int y){
//对于已检查的字段,设置为true
boolean[][]painted=new boolean[pixmap.getWidth()][pixmap.getHeight()];
//着色时跳过黑色像素
int blackColor=Color.rgba8888(Color.BLACK);
queue.clear();
添加(新点(x,y));
而(!queue.isEmpty()){
Point temp=queue.remove();
int temp_x=temp.getX();
int temp_y=temp.getY();
//仅当点在pixmap的边界内时才执行操作
如果(temp\u x>=0&&temp\u x=0&&temp\u y
这不符合预期。例如,在以下测试图像上:


随机矩形将根据我单击的位置重新着色。例如,单击紫色矩形下方的任意位置将重新显示紫色矩形。单击紫色矩形内部可重新选择绿色矩形。我已经检查了它,并且正在将正确的参数传递给方法,所以问题可能在我的循环中的某个地方。

您的算法是正确的,只有您的输入参数是错误的

随机矩形将根据我单击的位置重新着色。例如,单击紫色矩形下方的任意位置将重新显示紫色矩形。单击紫色矩形内部可重新选择绿色矩形

如果你看这张图片,彩色矩形并不是随机的。真正的问题是Y坐标不正确。特别是你的Y坐标是颠倒的

这是因为大多数时候LibGDX使用的是左下角的y-up坐标系,但是在
Pixmap
的情况下,它是左上角的y-down坐标系


一个简单的解决方法是通过执行
Y=pixmap.getHeight()-Y

来反转Y值,我认为您没有传递正确的参数。听起来你的Y坐标是错误的,因为你使用的坐标系是Y向上的,而另一个是Y向下的。试着在你的方法开始的时候用
y=pixmap.getHeight()。这是一个libGDX项目,例如场景坐标、精灵、矩形和类似对象的左下角有一个(0,0)坐标。但我猜Pixmap使用左上角表示(0,0)。谢谢这解决了你的问题吗?如果有,我会把它写下来作为答案,这样它就不会显得没有答案。是的,去做吧,伙计。谢谢你的帮助。@noone看起来你可以用aswerThank来写它@noone我在我的库中使用了这个解决方案: