[java] Transparent Images in awt

Started by
13 comments, last by tombr 18 years, 11 months ago
I load the image somthing like that:

Image img=Toolkit.getDefaultToolkit().createImage(fileName);
mdtr.addImage(img,0);try
{
   mdtr.waitForID(0);
}
catch (InterruptedException e)
{
   System.out.println(e);
}

mdtr is a mediaTracker fileName is a name of a gif file I print the Image on the screen like that:

target.drawImage(frames[currentFrame],xPosition,yPosition,bgColor,null);

target is declared "Graphics target", but it actualt of the type Graphics2D. target is my buffer frames[currentFrame] is the image I loaded before You can guess what xPosition and yPosition are bgColor is the background color Now, I made some pictures with white backgrounds, and tried to print the with

bgColor=new Color(255,255,255);

But the pictures background is still not transparent. What have I been doing wrong?
-----------------------------------------Everyboddy need someboddy!
Advertisement
Do you really have to use an AWT image? If you use BufferedImage you could do like this:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();GraphicsDevice gd = ge.getDefaultScreenDevice();GraphicsConfiguration gc = gd.getDefaultConfiguration();Offscreen = gc.createCompatibleImage(300,300, Transparency.TRANSLUCENT);
Well, I don't realy use Graphics and Image Objects. Those classes are abstract. The actual Objects are of the types Graphics2D and BufferedImage.

gcsaba2, thanks for replying, but your code didn't solved the problem. It only made the entire background black.

I tried to use the setXORMode(java.awt.Color) function of the Graphics class with the parameter java.awt.Color.white. It solved that problem, but it made other probelm with images lying on each other.

Any other ideas?
-----------------------------------------Everyboddy need someboddy!
Quote:Original post by someboddy
gcsaba2, thanks for replying, but your code didn't solved the problem. It only made the entire background black.

Yes I know. There is a way to fix this though I haven't looked it up (too lazy). I solved the problem by using only GIF images, and you can draw them to have a transparent background. I downloaded IrfanView which is very useful for conversion between image types, and when you save an image as a GIF it shows you your image zoomed and you can select a color, and then that color will be shown as transparent if you load it from Java.

Quote:java.awt.image.BufferedImage#getAlphaRaster()
getAlphaRaster

public WritableRaster getAlphaRaster()

Returns a WritableRaster representing the alpha channel for BufferedImage objects with ColorModel objects that support a separate spatial alpha channel, such as ComponentColorModel and DirectColorModel. Returns null if there is no alpha channel associated with the ColorModel in this image. This method assumes that for all ColorModel objects other than IndexColorModel, if the ColorModel supports alpha, there is a separate alpha channel which is stored as the last band of image data. If the image uses an IndexColorModel that has alpha in the lookup table, this method returns null since there is no spatially discrete alpha channel. This method creates a new WritableRaster, but shares the data array.

Returns:
a WritableRaster or null if this BufferedImage has no alpha channel associated with its ColorModel.

java.awt.image.BufferedImage@getTransparency()
getTransparency

public int getTransparency()

Returns the transparency. Returns either OPAQUE, BITMASK, or TRANSLUCENT.

Specified by:
getTransparency in interface Transparency

Returns:
the transparency of this BufferedImage.
Since:
1.5
See Also:
Transparency.OPAQUE, Transparency.BITMASK, Transparency.TRANSLUCENT


[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Well, IrfanView didn't solved my problem.

capn_midnight, can you explain or link to a toturial which explains how to use Rasters and Alfa?
-----------------------------------------Everyboddy need someboddy!
Will it slow my game seriuesly if I will paint the images manualy, using getRGB and setRGB?
-----------------------------------------Everyboddy need someboddy!
One idea to use transparency is to draw a loaded image to a BufferedImage and then filter out a color that you want to be transparent.

    //Filters a color out of an image (replaces with transparency)    public static BufferedImage filterImage(Image i, Color c)    {    	final int WIDTH = i.getWidth(null);    	final int HEIGHT = i.getHeight(null);    	final int COLOR = c.getRGB();    	final Color CLEAR = new Color(0,0,0,0);    	    	GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();    	GraphicsConfiguration gc = gd.getDefaultConfiguration();    	    	//The new image with the color filtered out    	BufferedImage temp;    	temp = gc.createCompatibleImage(WIDTH,HEIGHT,Transparency.BITMASK);    	    	//Gets the graphics of the temporary image		Graphics2D g = temp.createGraphics();				//Copies the old image to this one		g.drawImage(i,0,0,null);				//Gets rid of extra resources used on the graphics obj.		g.dispose();						//Find all COLOR pixels and make them transparent		for(int x=0; x<WIDTH; x++){			for(int y=0; y<HEIGHT; y++)				if(temp.getRGB(x,y) == COLOR)					temp.setRGB(x,y,CLEAR.getRGB());		}    	    	return temp;    }


Now you have the image just display it with
target.drawImage(img,positionX,positionY,null);


Also, another way to load images (from Brackeen's "Developing Games in Java") is to use
Image img = (new ImageIcon(file)).getImage();
It handles waiting for the image to finish loading and supports (as far as i can tell) .png and .gif.
(ImageIcon is in javax.swing.*)
Thank you mako_5. I will stay in your debt forever. Now my game looks better.
-----------------------------------------Everyboddy need someboddy!
That's great mako_5, somebody should put that code in the FAQ

This topic is closed to new replies.

Advertisement