Sign in to follow this  

[java] Java and transparent images

This topic is 4486 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
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 DRPhil
I just draw a bmp in paint, load it up in photoshop, make what I need transparent, and then save it as a Compuserve gif. Works like a charm.

Why not draw it in photoshop in the first place? ;)

Share this post


Link to post
Share on other sites
I tried a second paint program called Pixia. I checked the pink color and it was a value of RGB(100,0,100). MS Paint was a horrible way to tell what the value was. Somehow I got 255,0,255. With that being done, I compiled and ran the program. The pink background is still there. I tried the black rectangle behind the PC - nothing happened. With a thought of this just being my code causing the problem, I'll post it here for you to view.

Edit: Also, like in C#, can Java determine what the pixel color is at each image positioned at (0,0)?


import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Applet_Practice extends Applet implements KeyListener
{
Image imgPC;
int x, y;
int size = 50;
Color transColor = new Color(100, 0, 100);

public void init()
{
addKeyListener(this);
imgPC = getImage(getDocumentBase(), "PC.png");
}

public void start()
{
}

public void stop()
{
}

public void destroy()
{
}

public void paint(Graphics g)
{

g.drawImage(imgPC, x, y, size, size, transColor, this);
}

public void keyReleased(KeyEvent ke)
{
}

public void keyPressed(KeyEvent ke)
{
int key = ke.getKeyCode();

switch (key)
{
case KeyEvent.VK_W:
y -= 50;
break;
case KeyEvent.VK_S:
y += 50;
break;
case KeyEvent.VK_D:
x += 50;
break;
case KeyEvent.VK_A:
x -= 50;
break;
}

repaint();
}

public void keyTyped(KeyEvent ke)
{
}
}


Share this post


Link to post
Share on other sites
I think we're misunderstanding that background color parameter. I think the function will paint all transparent pixels of your image with that background color, and not the other way around. I see no purpose in this, though.

Go to Photoshop or Gimp and replace that pink background from your image with transparent pixels. Then use the other drawImage() function without the background color parameter. It will work for sure.

Then, just for the kick of it, use the previous drawImage() function as you had just to see if the pink background magically reappears.

Share this post


Link to post
Share on other sites
I went to the site and had some problems as to what link to click on.

My first click downloaded GTK runtime environment. While executing, it wanted to know what language to use, and all users' language. My second click downloaded the source code (I believe). I just want the exe! Can you please tell me which link to use for WinXP?

Edit: Nevermind, I found it.

Because I don't feel like spending all day to figure Gimp out, can someone tell me what to do to make the image transparent? I tried Layer -> Transparency -> Set it to RGB(100,0,100), and the background disapears. However, the character has a tint of green. Why is that?

[Edited by - dxFoo on August 30, 2005 8:08:59 PM]

Share this post


Link to post
Share on other sites
Just so everyone knows, Blew figured out why it was giving the green tint, and we got it working fine now. But my thanks goes out to everyone who gave their insight. It helped! :)

Share this post


Link to post
Share on other sites

This topic is 4486 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this