0

I'm having a problem where images loaded in as a buffered image contain their alpha channels correctly. But when passed as a parameter the alpha channel is lost.

I load the images in like this:

MousePointer = ImageIO.read(getClass().getResourceAsStream("sprites/Mouse-sheet.png"));

Then I draw like this (the alpha channel remains intact):

g.drawImage(Sprites.MousePointer, mouse.x, mouse.y, 64, 64, null);

But when I draw using my own function, the alpha channel is lost:

mouseAnimation.DrawAbsolute(Sprites.MousePointer, g, mouse.x, mouse.y, 4, 4, deltaTime);

Why is the alpha channel being lost, java variables are pointers and thus pass-by-reference so it doesn't make sense? What can I do to prevent it?

You can see the results here: YouTube video I only show the images in their broken state, at the time I was confusing one method for another which resulted in nothing changing.

Timothy Eckstein
  • 307
  • 1
  • 2
  • 10
  • 1
    It can only come an issue in your mouseAnimation function. As you said, Java only uses pointers, so the image given to your function is the same. – FiReTiTi Nov 20 '16 at 08:48
  • 1
    From the video, it seems you pass `Color.white` to your `DrawAbsolute` method, and this `Color` is passed to one of the `drawImage` methods that takes a `Color` parameter. This method does exactly what you don't want. It replaces the alpha with the color you pass... – Harald K Nov 20 '16 at 17:05
  • @haraldK Thanks for the comment. I always thought that parameter was for colorification, basically white was to keep the original colors. I tried new Color(256, 256, 256, 0) but to no avail. However, I will keep pursuing this path. Changing to Color.cyan did exactly what you predicted. – Timothy Eckstein Nov 20 '16 at 23:10

1 Answers1

0

For anyone else having trouble with this (coming from a language where you are always asked to provide a color when drawing like I did) I was able to fix it with one simple change. I just removed the color parameter.

Like @haraldK indicated, unlike in other languages the color is not for colorization of the image but rather changed all alpha to that color.

Timothy Eckstein
  • 307
  • 1
  • 2
  • 10