I created an ARGB BufferedImage. Now I’d like to reinitialize it with a transparent background. I tried the following code:
(...)
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);
(...)
but it didn’t work.
Any idea on how to do this please ?
Thanks !
g.clearRect(..)fills the selected rectangle with the background colour of theGraphics2Dobject. You’re better off doingg.fillRect(..)which would give the intended result with your code, or set the background colour of theGraphics2Dobject beforehand (g.setBackground(..)).Also, you may have to do
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));before the fill so that it sets the buffer properly (ignore destination buffer data, only use source data — in this case, the fill operation). Not sure what the default is for this value, but you should set it back to that afterwards to ensure proper operation.