0

I've come across a rather troubling point in my video game that I am building in Java. Before you go and set this as spam, please note that I have taken a look at both of these links and they have not helped me in any way:

  1. Drawing a transparent BufferedImage over a non-transparent BufferedImage
  2. Making a Certain Color on a BufferedImage Become Transparent

What I need to do is get the color 0xFF00FF from my sprites and set it to transparent so that the charecter shows up, not just the background behind him. Here's what I have:

(Sorry about not having enough "reputation", but here's a link to the image.) https://plus.google.com/photos/yourphotos?enfplm&hl=en&utm_source=lmnavbr&utm_medium=embd&utm_campaign=lrnmre&rtsl=1&partnerid=selm0&pid=5940308896707611042&oid=114903427794927596233

I have a main BufferedImage and a pixels[] array for the data in that image, and I render the level to the screen, and then render the player afterwards. Here's my method for updating the image before rendering it:

private void tickImage() {

    final int centerY = (HEIGHT / 2) - (48 / 2);
    final int centerX = (WIDTH / 2) - (48 / 2);

    display.clear();
    display.renderBlock(StoneBlock.block, xMove, yMove);

    display.renderPlayer(knight, centerX, centerY);

    for (int a = 0; a < pixels.length; a++) {
        pixels[a] = display.pixels[a];
    }
}

If you would like to look at the rest of the code, please, feel free to here:

https://github.com/NikolaAndMichael/WarDungeon/tree/master/WarDungeon/src/net/naprav/wardungeon

Thank you in advance.

Community
  • 1
  • 1

1 Answers1

0

Like this:

BufferedImage img = ...;
for (int x = 0; x < img.getWidth(); ++x)
for (int y = 0; y < img.getHeight(); ++y)
{
   if ((img.getRGB(x, y) & 0x00FFFFFF) == 0xFF00FF) 
   {
       img.setRGB(x, y, 0);
   }
}

What this does is iterating over each pixel and testing the red, green and blue component to match 0xFF00FF. If it matched, set the color to transparent black (0x00000000). In order to test only the RGB channels and not the alpha channel, use a bitwise operator to filter the alpha channel data away.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • Hahahaha. I lolled. I answered the linked question as well, apparently. – Martijn Courteaux Oct 29 '13 at 22:44
  • Hey man, I hate to say that it didn't work. I tried it, and I'm using a "TYPE_INT_RGB" BufferedImage, and bitwise operators aren't exactly my forte, if you will. A bit of help, please? – Payneztastic Oct 29 '13 at 22:51
  • This piece of code modifies the buffered image. If you are 100% sure that the buffered image really contains those 0xFF00FF colors, then this should work. Maybe you are using JPG sprites, causing the colors to be inaccurate? However, why don't you simply use transparency in the image file itself, instead of using the deprecated ugly-pink trick? – Martijn Courteaux Oct 29 '13 at 23:02