1

I'm having a problem when it comes to drawing BufferedImages. I'm working on a 2D tile-based map editor and when I draw a tile, it first draws the lower layer followed by the top layer. like so:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g.drawImage(tileLayer, 0, 0, null);
    g.drawImage(objectLayer, 0, 0, null);
}

Note that this method is in a class which extends JLabel. It's actually redrawing the ImageIcon which was set. Now, to understand the problem you must realize that before the objectLayer BufferedImage is created, each pixel is checked for a certain color. If the pixel is that color, then the pixel is set to all white with an alpha value of 0 (so that it will be transparent). Example:

    int transparentRed = transparentColor.getRed();
    int transparentGreen = transparentColor.getGreen();
    int transparentBlue = transparentColor.getBlue();


for (int x = 0; x < image.getWidth(); x++)
{
    for (int y = 0; y < image.getHeight(); y++)
    {
        int color = i.getRGB(x, y);

        int red = (color & 0x00FF0000) >> 16;
        int green = (color & 0x0000FF00) >> 8;
        int blue = color & 0x000000FF;

        // If the pixel matches the specified transparent color
        // Then set it to an absolute white with alpha at 0
        if (red == transparentRed && green == transparentGreen && blue == transparentBlue)
            i.setRGB(x, y, 0x00FFFFFF);
    }
}

    return i; 

The point is to draw the top layer over the lower layer without affecting any of the previously placed lower layer pixels. The white pixels of the top layer should just not appear.

The problem is that this works on some images and on others it doesn't. On certain images, when I go to draw the top layer, it just draws the white in anyways (as if the alpha value isn't set to 0) and on other images it works like a charm and the white pixels aren't drawn in.

I have been using only .png images so I know that it doesn't have to do with the formatting. I've tried quite a few different things and I'm stuck if anyone can help out.

Kyle
  • 21
  • 3
  • For better help sooner, post an [SSCCE](http://sscce.org/). We also need links to 1. a small (bytes) image that works. 2. a small image that fails. – Andrew Thompson Apr 13 '12 at 17:41
  • `g.drawImage(tileLayer, 0, 0, null);` Swap `null` for `this` (in both of them). – Andrew Thompson Apr 13 '12 at 17:44
  • Tried that, but it didn't appear to help. – Kyle Apr 13 '12 at 17:47
  • *"Tried that, but it didn't appear to help."* An SSCCE would, `null`/`this` was merely best practice, it had nothing to do with detecting the 'white'. So which were you referring to? Pretend I'm *not* psychic. – Andrew Thompson Apr 13 '12 at 17:51
  • 1
    Sorry. Tried to replace the null/this. I'll try to post an SSCCE and two example images – Kyle Apr 13 '12 at 18:53
  • Here are the two example images: http://i39.tinypic.com/16897xc.png and http://imageshack.us/photo/my-images/107/smworldtilesetcn1.png/sr=1 The first link works while the second one does not. The way that the code is set up doesn't really allow for a SSCCE – Kyle Apr 13 '12 at 19:15
  • I don't see any pure white in the 2nd image. Where was it supposed to be hiding? *"The way that the code is set up doesn't really allow for a SSCCE "* Well, don't worry about it. Good luck with it. – Andrew Thompson Apr 13 '12 at 19:23
  • The bright pink color which represents transparency gets converted to white. The same process happens for both images, but the second one doesn't work, as I mentioned. – Kyle Apr 13 '12 at 19:25

1 Answers1

1

I believe that by default the BufferedImage doesn't support an alpha channel. When constructing the BufferedImage, passing in BufferedImage.TYPE_INT_ARGB fixed the problem.

Kyle
  • 21
  • 3