I have a BufferedImage and I am trying to fill a rectangle with transparent pixels. The problem is, instead of replacing the original pixels, the transparent pixels just go on top and do nothing. How can I get rid of the original pixel completely? The code works fine for any other opaque colors.
我有一个BufferedImage,我试图用透明像素填充一个矩形。问题是,透明像素不是替换原始像素,而是放在顶部而不做任何事情。如何完全摆脱原始像素?该代码适用于任何其他不透明的颜色。
public static BufferedImage[] slice(BufferedImage img, int slices) {
BufferedImage[] ret = new BufferedImage[slices];
for (int i = 0; i < slices; i++) {
ret[i] = copyImage(img);
Graphics2D g2d = ret[i].createGraphics();
g2d.setColor(new Color(255, 255, 255, 0));
for(int j = i; j < img.getHeight(); j += slices)
g2d.fill(new Rectangle(0, j, img.getWidth(), slices - 1));
g2d.dispose();
}
return ret;
}
public static BufferedImage copyImage(BufferedImage source){
BufferedImage b = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = b.getGraphics();
g.drawImage(source, 0, 0, null);
g.dispose();
return b;
}
1 个解决方案
#1
2
Using AlphaComposite
, you have at least two options:
使用AlphaComposite,您至少有两个选项:
-
Either, use
AlphaComposite.CLEAR
as suggested, and just fill a rectangle in any color, and the result will be a completely transparent rectangle:或者,按照建议使用AlphaComposite.CLEAR,只需填充任何颜色的矩形,结果将是一个完全透明的矩形:
Graphics2D g = ...; g.setComposite(AlphaComposite.Clear); g.fillRect(x, y, w, h);
-
Or, you can use
AlphaComposite.SRC
, and paint in a transparent (or semi-transparent if you like) color. This will replace whatever color/transparency that is at the destination, and the result will be a rectangle with exactly the color specified:或者,您可以使用AlphaComposite.SRC,并以透明(或半透明,如果您喜欢)颜色绘制。这将替换目标处的任何颜色/透明度,结果将是一个具有指定颜色的矩形:
Graphics2D g = ...; g.setComposite(AlphaComposite.Src); g.setColor(new Color(0x00000000, true); g.fillRect(x, y, w, h);
The first approach is probably faster and easier if you want to just erase what is at the destination. The second is more flexible, as it allows replacing areas with semi-transparency or even gradients or other images.
如果您想要删除目的地的内容,第一种方法可能更快更容易。第二种是更灵活,因为它允许替换半透明或甚至渐变或其他图像的区域。
PS: (As Josh says in the linked answer) Don't forget to reset the composite after you're done, to the default AlphaComposite.SrcOver
, if you plan to do more painting using the same Graphics2D
object.
PS :(正如Josh在链接答案中所说的那样)如果您计划使用相同的Graphics2D对象进行更多绘画,请不要忘记在完成后将复合重置为默认的AlphaComposite.SrcOver。
#1
2
Using AlphaComposite
, you have at least two options:
使用AlphaComposite,您至少有两个选项:
-
Either, use
AlphaComposite.CLEAR
as suggested, and just fill a rectangle in any color, and the result will be a completely transparent rectangle:或者,按照建议使用AlphaComposite.CLEAR,只需填充任何颜色的矩形,结果将是一个完全透明的矩形:
Graphics2D g = ...; g.setComposite(AlphaComposite.Clear); g.fillRect(x, y, w, h);
-
Or, you can use
AlphaComposite.SRC
, and paint in a transparent (or semi-transparent if you like) color. This will replace whatever color/transparency that is at the destination, and the result will be a rectangle with exactly the color specified:或者,您可以使用AlphaComposite.SRC,并以透明(或半透明,如果您喜欢)颜色绘制。这将替换目标处的任何颜色/透明度,结果将是一个具有指定颜色的矩形:
Graphics2D g = ...; g.setComposite(AlphaComposite.Src); g.setColor(new Color(0x00000000, true); g.fillRect(x, y, w, h);
The first approach is probably faster and easier if you want to just erase what is at the destination. The second is more flexible, as it allows replacing areas with semi-transparency or even gradients or other images.
如果您想要删除目的地的内容,第一种方法可能更快更容易。第二种是更灵活,因为它允许替换半透明或甚至渐变或其他图像的区域。
PS: (As Josh says in the linked answer) Don't forget to reset the composite after you're done, to the default AlphaComposite.SrcOver
, if you plan to do more painting using the same Graphics2D
object.
PS :(正如Josh在链接答案中所说的那样)如果您计划使用相同的Graphics2D对象进行更多绘画,请不要忘记在完成后将复合重置为默认的AlphaComposite.SrcOver。