java midp game programming

heres my situation. i am programing a small game in java. now i use .png files for my sprites. to hide the frame which surrounds the sprites i made their background black along with the bits that i don't want to see in my sprites, but when my two sprites(a and b) colide, i see the black border of a over the b's image, what is the best soloution for this.

can i set a transperancy color, so that every time this color is detected in a sprite it is never painted or something like that?

[501 byte] By [mariziona] at [2007-10-2 7:28:56]
# 1
Make your sprites transparent when drawing them in whatever drawing application you use. It's much easier this way.Hasman.
hasman001a at 2007-7-16 21:07:19 > top of Java-index,Other Topics,Java Game Development...
# 2
Make the backgrounds of the sprites transparents, the png format support this feature!
jow_caraa at 2007-7-16 21:07:19 > top of Java-index,Other Topics,Java Game Development...
# 3

I find it useful sometimes to manually specify a certain color that I want to be transparent in an image. This way I can use ms paint/photoshop/whatever to paint the areas that I want to be transparent with something like magenta (255,0,255). This is handy when a) you dont have access to a graphics program that will deal with transparency the way you want it to, or with the format you need, and b) you need to specify a certain specific color (since, unlike magenta, black might actually show up in other places in your image, which can make for some interesting effects if you accidentally made it transparent.

// Get all the pixels

int width = bufferedImage.getWidth(null);

int height = bufferedImage.getHeight(null);

int[] rgbs = new int[width*height];

bufferedImage.getRGB(0, 0, width, height, rgbs, 0, width);

// magenta, use any color, pass one in, whatever

Color transparencyColor = new Color(255,0,255);

int transR = transparencyColor.getRed();

int transG = transparencyColor.getGreen();

int transB = transparencyColor.getBlue();

for (int x=0; x < width; x++) {

for (int y=0; y < height; y++) {

int px = rgbs[ y*width + x ];

int alpha= (px & 0xff000000) >> 32;

int red= (px & 0x00ff0000) >> 16;

int green= (px & 0x0000ff00) >> 8;

int blue= (px & 0x000000ff);

// check pixel against configured transparency color

if (red == transR && green == transG && blue == transB) {

// change pixels to transparent

px = 0x00000000;

bufferedImage.setRGB( x, y, px );

}

}

}

Classes are in java.awt and java.awt.image. Also, since using images with 'nice looking' actual alpha blending will usually have a severe impact on performance in java, you may want to make sure that when you make your BufferedImage object, you make it with a simpler mode of transparency (where every pixels is either 100% transparent or 100% opaque):

public static BufferedImage createCompatibleBufferedImage( int width, int height ) {

BufferedImage bimage = null;

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

GraphicsDevice gs = ge.getDefaultScreenDevice();

GraphicsConfiguration gc = gs.getDefaultConfiguration();

// make 1-bit transparent image buffer

bimage = gc.createCompatibleImage( width, height, Transparency.BITMASK);

return bimage;

}

I certainly won't claim to be an expert on transparency/hardware/performance, but this has just been my experience.

In practice, I use this code in an animation class that reads either a single image file, or a text file describing an animation including lists of images (or directories of images) and an optional transparency color to erase out of that image. This way I can do nice convenient things like, render a 3d model with the options to make the background explicitly magenta (and dont anti-alias the model against the background, or there will be some pixels at the edge that are partly magenta and partly 'model' colored, which ruins the effect entirely). Then, the rendered image of the model is immedeatly useable in my game since the animation class knows to remove magenta pixels, and I dont have to do any further processing on the image in photoshop/etc.

Sorry this explanation was so verbose. Hope it was useful. ;)

steelporkchopa at 2007-7-16 21:07:19 > top of Java-index,Other Topics,Java Game Development...