Jump to content
  • Advertisement
Sign in to follow this  
dxFoo

[java] Java and transparent images

This topic is 4771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I usually use the drawImage() function that doesn't take that background color parameter. It uses the image's alpha channel instead. Works perfectly.

Share this post


Link to post
Share on other sites
Quote:
Original post by dxFoo
Why am I still getting a pink background, though?


Are you sure it's RGB(255,0,255)?

Share this post


Link to post
Share on other sites
I'm pretty sure. Would you mind taking a look at the image, just for a second opinion?

Share this post


Link to post
Share on other sites
Try filling a black rectangle for the background before drawing anything else.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!