Java图像编辑填充区域不工作



我有我的图像编辑程序的一部分,填充图像的任何区域,其中(a)与所选坐标的颜色相同,并且(b)与可用替换的点相邻,具有属性this.color。本质上,MS Paint使用的填充方法。方法如下:

public void branchFrom(int x, int y) {
        int color = this.color.getRGB(); // Color to fill in the area
        Color original = new Color(this.image.getRGB(x, y)); // Color of the selected coordinate
        this.replace(x, y, color); // First replaces the selected spot with the indicated color
        // Next, it checks all spots joined to itself by an edge. If it is invalid for filling, it continues to the next adjacency. Else, it fills the area and continues branching from there, until all spots are filled
        this.replaceSecondaryOnCondition(x + 1, y, clr -> {
            if (clr.equals(original)) {
                this.branchFromSecondary(x + 1, y);
                return true;
            } else {
                return false;
            }
        });
        this.replaceSecondaryOnCondition(x - 1, y, clr -> {
            if (clr.equals(original)) {
                this.branchFromSecondary(x - 1, y);
                return true;
            } else {
                return false;
            }
        });
        this.replaceSecondaryOnCondition(x, y + 1, clr -> {
            if (clr.equals(original)) {
                this.branchFromSecondary(x, y + 1);
                return true;
            } else {
                return false;
            }
        });
        this.replaceSecondaryOnCondition(x, y - 1, clr -> {
            if (clr.equals(original)) {
                this.branchFromSecondary(x, y - 1);
                return true;
            } else {
                return false;
            }
        });
    }
}

它工作得很好,除了original是透明的。为什么会这样,我该如何解决这个问题?

EDIT:它工作时,this.color是透明的。每当original不是100%不透明时(例如alpha值为< 255),它似乎就会失败

您正在使用的Color(int rgb)构造函数"创建不透明的sRGB颜色…"Color(int rgba, boolean hasalpha)构造函数将创建一个具有透明度的新Color。试着修改这一行:

Color original = new Color(this.image.getRGB(x, y));

:

Color original = new Color(this.image.getRGB(x, y), true);
注意:如果您在其他相关代码中使用Color(int rgb)构造函数(例如您的replace方法),您可能也需要更改它们。

最新更新