Archived

This topic is now archived and is closed to further replies.

felisandria

[java] Blitting in Java

Recommended Posts

I''ve just learned Java (I know enough to be dangerous *wry smile*) and I''m trying to solidify that by writing a little game. I have a tile engine working and I''m putting in a character, but I can''t for the life of me find any information on transparent blitting/colorkeying in Java. All I need is a method name or a link. I''ve found a lot of source but everything either makes the sprite background the color of the game background, or makes the sprites pixel by pixel in a sprite painting method, neither of which I want to do. -fel

Share this post


Link to post
Share on other sites
If you are using GIFs, I bet you do, because there aren''t better ways to do it without tricks, use GIFs natural transparency OR

If I recall right it works with natural(bluest) blue gif files. If it doesn''t work I can give you the gif file, too. =)


import java.awt.image.*;
import java.awt.*;
import java.net.*;

public class app extends java.applet.Applet {
Image GifOriginalWithWithBlueBackground;
Image GifModifiedWithTransparentBackground;

public void init() {
setBackground(new Color(0).white);

MediaTracker media = new MediaTracker(this);
// image of our friend, Gumby with a blue background
GifOriginalWithWithBlueBackground = getImage(getDocumentBase(),"man.gif");
media.addImage(GifOriginalWithWithBlueBackground,0);
try {
media.waitForID(0);
GifModifiedWithTransparentBackground =
Transparency.makeColorTransparent
(GifOriginalWithWithBlueBackground, new Color(0).blue);
}
catch(InterruptedException e) {}
}

public void paint(Graphics g) {
g.drawImage(GifOriginalWithWithBlueBackground, 10,10,this);
g.drawImage(GifModifiedWithTransparentBackground,10, 80,this);
}
}


import java.awt.*;
import java.awt.image.*;


public class Transparency {
public static Image makeColorTransparent
(Image im, final Color color) {
ImageFilter filter = new RGBImageFilter() {
// the color we are looking for... Alpha bits are set to opaque
public int markerRGB = color.getRGB() / 0xFF000000;

public final int filterRGB(int x, int y, int rgb) {
if ( ( rgb / 0xFF000000 ) == markerRGB ) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
}
else {
// nothing to do
return rgb;
}
}
};

ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
}

[/Code]




Time comes, time goes and I only am.

Share this post


Link to post
Share on other sites
If you are using Java2D support in Java2, you can call getAlphaRaster() on your BufferedImage instances and use the returned WriteableRaster to set the alpha component for any given pixel of the image. The key is that your BufferedImage should support alpha components up front, or you will have to copy the data into one that does.


Image image = getImage("Blah.jpg");
int iw = image.getWidth(this);
int ih = image.getHeight(this);
BufferedImage buf = new BufferedImage(iw, ih,BufferedImage.TYPE_INT_ARGB); //ARGB is important for alpha
buf.createGraphics().drawImage(image,0,0,null);//draw image to buffer
//All that stuff just wrote the image into a buffer
WriteableRaster rast = buf.getAlphaRaster();
for (int i=0;i for (int j=0;j rast.setSample(i,j,0,Math.abs(255-i)); //alpha gradient along x axis. 255 is opaque, 0 is transparent
}
}
// later, in your painting routine
Graphics2D gcontext = (Graphics2D)passed_graphics;
gcontext.drawImage(buf,0,0,null); //write out to screen given your passed graphics object


ManaSink
mmmrrrfffmm....stupid less than. HTML markup bites.

Edited by - ManaSink on June 15, 2000 1:16:14 PM

Share this post


Link to post
Share on other sites