0

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?

Mertcan Ekiz
  • 691
  • 1
  • 9
  • 22

3 Answers3

2

have look at

  1. JLayer (Java7) based on JXLayer (Java6)

  2. Translucency

  3. Set transparency by using AlphaComposite

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mKorbel
  • 109,525
  • 20
  • 134
  • 319
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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045