[java] Transparent Images in awt

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

Recommended Posts

I load the image somthing like that:
Image img=Toolkit.getDefaultToolkit().createImage(fileName);
{
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?

Share on other sites
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);

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

Share on other sites
Quote:
 Original post by someboddygcsaba2, 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.

Share on other sites
Quote:
 java.awt.image.BufferedImage#getAlphaRaster()getAlphaRasterpublic 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()getTransparencypublic 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

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

Share on other sites
Will it slow my game seriuesly if I will paint the images manualy, using getRGB and setRGB?

Share on other sites
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.*)

Share on other sites
Thank you mako_5. I will stay in your debt forever. Now my game looks better.

Share on other sites
That's great mako_5, somebody should put that code in the FAQ

1. 1
2. 2
JoeJ
14
3. 3
4. 4
5. 5
frob
11

• 13
• 16
• 13
• 20
• 12
• Forum Statistics

• Total Topics
632178
• Total Posts
3004604

×