Jump to content
  • Advertisement
Sign in to follow this  
someboddy

[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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!