I'm trying to use a sprite sheet in my game and I want the background to be transparent, how can I implement this using swing and/or awt?
Asked
Active
Viewed 96 times
0
-
1Is the sprite sheet BG transparent now? – Andrew Thompson Sep 12 '12 at 08:55
3 Answers
1
Your best option is to create your sheets using a format that already supports transparency for background, like PNG 32bits.
Translucency and transparency options can only change the whole transparent channel of your image, not act on individual bits based on their colors.

Aurelien Ribon
- 7,548
- 3
- 43
- 54
-
1`LookupOp`, mentioned [here](http://stackoverflow.com/a/4383798/230513) may be used to alter an existing background color. – trashgod Sep 12 '12 at 16:53
-
It makes a useful complement to `AlphaComposite`, suggested [here](http://stackoverflow.com/a/12384425/230513) and mentioned [here](http://stackoverflow.com/a/12392808/230513). – trashgod Sep 13 '12 at 00:00
1
Expanding on mKorbel's suggestion to use AlphaComposite
, you cannot paint with AlphaComposite.Clear
mode, but you can clear the background of a BufferedImage
. For example,
Graphics2D dstG = dstImage.createGraphics();
dstG.setComposite(AlphaComposite.Clear);
dstG.fillRect(0, 0, WIDTH, HEIGHT);
Subsequent painting in a different mode will leave unpainted pixels transparent.
More details may be found in this answer.