Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Java and transparent images

Started by dxFoo Aug 30, 2005 at 6:56 PM 23 replies 1.8k views
Original Post
dxFoo
dxFoo
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
Blew
Blew
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
OrangyTang
OrangyTang
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.
dxFoo
dxFoo
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?
Blew
Blew
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
dxFoo
dxFoo
Why am I still getting a pink background, though?
Blew
Blew
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
Blew
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
dxFoo
dxFoo
I'm pretty sure. Would you mind taking a look at the image, just for a second opinion?
Blew
Blew
Try filling a black rectangle for the background before drawing anything else.
blew
DRPhil
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.
Blew
Blew
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? ;)
blew
dxFoo
dxFoo
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)	{	} }
Blew
Blew
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.
blew
dxFoo
dxFoo
Edit: I found Gimp. I'll give it a try.
Blew
Blew
Yeap.
blew
dxFoo
dxFoo
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]
Blew
Blew
So how's it going? I stoped replying 'cause I had to sleep. :D
blew
dxFoo
dxFoo
I'm always getting a green tint when removing the pink background. I'm not sure why it's doing this.
Blew
Blew
So you did Layer\Transparency\Color to Alpha\RGB(100,0,100)?
It should be alright then... :-/
blew

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.