我创建了一个 ARGB BufferedImage。现在我想用透明背景重新初始化它。我尝试了以下代码:
(...)
if( this.offscreen==null ||
this.offscreen.getWidth()!= dim.width ||
this.offscreen.getHeight()!= dim.height )
{
this.offscreen=new BufferedImage(
dim.width,
dim.height,
BufferedImage.TYPE_INT_ARGB);
}
Graphics2D g=this.offscreen.createGraphics();
g.setColor(new Color(255,255,255,0));
g.clearRect(0, 0, dim.width, dim.height);
(...)
但是没用。
关于如何执行此操作的任何想法?
谢谢!
最佳答案
g.clearRect(..)
用 Graphics2D
对象的背景颜色填充选定的矩形。您最好使用 g.fillRect(..)
来为您的代码提供预期的结果,或者预先设置 Graphics2D
对象的背景颜色(g.setBackground(..)
).
此外,您可能必须在填充之前执行 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
以便正确设置缓冲区(忽略目标缓冲区数据,仅使用源数据——在本例中为填充操作)。不确定此值的默认值是多少,但您之后应将其设置回该值以确保正常运行。
关于java - 使用 Graphics2D 填充透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4565909/