Java 图像编辑填充区域不起作用
Java Image Editing Fill Area not working
我的图像编辑程序有一部分可以填充图像的任何区域,其中 (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
是透明的。为什么会这样,我该如何解决这个问题?
编辑: 它在 this.color
透明时起作用。只要 original
不是 100% 不透明(例如 alpha
值为 < 255
),它似乎就会失败
Color(int rgb)
constructor you are using "creates an opaque sRGB color ..." The 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
方法,您可能也需要更改它们。
我的图像编辑程序有一部分可以填充图像的任何区域,其中 (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
是透明的。为什么会这样,我该如何解决这个问题?
编辑: 它在 this.color
透明时起作用。只要 original
不是 100% 不透明(例如 alpha
值为 < 255
),它似乎就会失败
Color(int rgb)
constructor you are using "creates an opaque sRGB color ..." The 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
方法,您可能也需要更改它们。