Transparent Pixels

In my "radar screen" project, I have now created a background console image, and radar screen background image, and saved these to one buffered image.

However, the radar screen, being round, should have transparent pixels in the area between the circle of the radar screen, and the square boundaries of its image.

When I create this image, is there a method to set all pixels to transparent initially, and then draw the screen?

MTIA, Max

[462 byte] By [maxhugena] at [2007-9-27 23:40:11]
# 1

If someone doesn't come up with a cleaner way, you can do this:

public static Image makeTransparent(Image bi) {

int[] pgPixels = new int[Tile.TILESIZE * Tile.TILESIZE];

PixelGrabber pg = new PixelGrabber(bi, 0, 0, Tile.TILESIZE, Tile.TILESIZE, pgPixels, 0, Tile.TILESIZE);

try {

pg.grabPixels();

} catch (InterruptedException e) {

e.printStackTrace();

}

for (int y = 0; y < Tile.TILESIZE; y++) {

for (int x = 0; x < Tile.TILESIZE; x++) {

int i = y * Tile.TILESIZE + x;

int a = (pgPixels[i] & 0xff000000) >> 24;

int r = (pgPixels[i] & 0x00ff0000) >> 16;

int g = (pgPixels[i] & 0x0000ff00) >> 8;

int b = pgPixels[i] & 0x000000ff;

if (r >= 210 && g >= 210 && b >= 210) {

a = 0;

pgPixels[i] = a | (r << 16) | (g << 8) | b;

}

}

}

Image _i = new JLabel().createImage(new MemoryImageSource(Tile.TILESIZE, Tile.TILESIZE,

pgPixels, 0, Tile.TILESIZE));

return _i;

}

This is something I adapted from Just Java by Peter van der Linden. It takes an image, and returns a version of the image where a particular range of colors has been made transparent. The real meat of the method is the 'if (r ... g ... b)' part. r, g, and b will be numbers (0-255) that represent the red, green, and blue components of the colors you are trying to see through. In my case, the method removes white. If you want it to remove black, for example, screen out anything where r, g, and b are all less than ~10, and so forth. The 'a = 0' line is what actually sets the Alpha value of the pixel to transparent. Good luck,

Bret

PS - you'll need to import the Image and Swing libraries, and maybe others, to make it work.

bh94704a at 2007-7-7 16:14:53 > top of Java-index,Other Topics,Java Game Development...
# 2
Thanks BretI'll give it a whirl!Cheers, Max
maxhugena at 2007-7-7 16:14:53 > top of Java-index,Other Topics,Java Game Development...
# 3

You are very aggressive with this game huh? Hoping to see some demo soon...

I would simply create a MemoryImageSource with alpha bits as full transparent of the applet's background color as something like this:

int width = getSize().width;

int height = getSize().height;

int size = width*height;

int pix = new int[size];

int trans = getBackground().getRGB() | (0 << 24);

for (int bit=0; bit<size; bit++)

pix[bit] = trans;

Image image = createImage(new MemoryImageSource(width, height, pix, 0, width));

>

ibosana at 2007-7-7 16:14:53 > top of Java-index,Other Topics,Java Game Development...