🔒 Locked
[java] Java and transparent images
Advertisement
Original Post
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
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.
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.
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?
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.
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.
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)?
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) { } }
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.
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.
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]
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]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement