[java] Java and transparent images

Started by
23 comments, last by dxFoo 18 years, 7 months ago
My character sprite (.png file) has a pink background which will be used for transparency. The question is how to do that with Java? In C#, it took as little as 5 lines to accomplish this task using the standard graphics namespace. What's the most common way in doing this with Java 1.5? Thanks, Phil
Advertisement
Make a PNG with alpha channels. Java will handle the transparency and do all the blending.

EDIT: Check the drawImage() function on the Graphics class.
- blew
Easiest would be to actually create the image with an alpha channel in the first place. Png files can have a full 8bit alpha channel which is easily used as transparency, whereas gif images use a single transparent colour in the pallette.

How you actually create this depends on your image program you're using. If you can live with the 256 colour cap per sprite then gif pallette transparency is usually dead easy to set.

Once your images are created correctly, regular ImageIO will load them as normal, and Graphics2D operations use the alpha channel by default IIRC.
I'm glad there are ways in doing this. I was getting a little worried as I browsed Google. Can you provide a tutorial link please?

Edit:
According to Sun, it reads...

The image is drawn inside the specified rectangle of this graphics context's coordinate space, and is scaled if necessary. Transparent pixels are drawn in the specified background color. This operation is equivalent to filling a rectangle of the width and height of the specified image with the given color and then drawing the image on top of it, but possibly more efficient.

public abstract boolean drawImage(Image img,                                  int x,                                  int y,                                  int width,                                  int height,                                  Color bgcolor,                                  ImageObserver observer)


	public void paint(Graphics g)	{		g.drawImage(imgPC, x, y, size, size, new Color(255, 0, 255), this);		} 


The pink color is RGB (255,0,255). When I load the applet, the pink background is still there. Am I on the right track anyway?
Really, the Java API Documentation is all you need.

Check these classes:
- ImageIO (to read images from files)
- Image (an image instance)
- Graphics (to draw anything: lines, images, text, ...)

EDIT: Yes, as OrangyTang said, one of the functions to draw images (provided by the Graphics class) handles all the alpha blending automatically.
- blew
Why am I still getting a pink background, though?
I usually use the drawImage() function that doesn't take that background color parameter. It uses the image's alpha channel instead. Works perfectly.
- blew
Quote:Original post by dxFoo
Why am I still getting a pink background, though?


Are you sure it's RGB(255,0,255)?
- blew
I'm pretty sure. Would you mind taking a look at the image, just for a second opinion?
Try filling a black rectangle for the background before drawing anything else.
- blew

This topic is closed to new replies.

Advertisement