Hi,
Re: MemoryImageSource
Basically, you can represent an image by an array of ints, and so instead of drawing rects over and over (which could be slow, and prone to garbage creation) you can directly modify the integer in the array to set the appropriate color (either black or blue). When you are done going through the array, you signal the image that the image has been changed, and then you can draw it to the screen. Check out the API docs on MemoryImage source but here’s a little code snipit that gets an image of a car and makes it semi-transparent by changing the alpha bit of the int of the pixel and then creating an image out of that array.
image contains the opaque image of the car.
// load up transparent image for the car
int[] pixels = new int[image.getWidth() * image.getHeight()];
PixelGrabber pg = new PixelGrabber(image, 0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
try
{
pg.grabPixels();
}
catch(InterruptedException e)
{}
for(int ctr = 0; ctr < pixels.length; ctr++)
{
int alpha = (pixels[ctr] >> 24) & 0xff;
if (alpha != 0)
{
// set this pixels transparancy to 25%
pixels[ctr] = 0x4F000000 | (pixels[ctr] & 0x00FFFFFF);
}
}
carTransImages[i] = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(image.getWidth(),image.getHeight(),pixels,0,image.getWidth()));
}
Now, don’t quote me, but I think that if you create an image from a MemoryImageSource, the array of integers is the actual data of the image, so modifying the array will actually modify the image. This, obviosuly is a very non-OO way to manipulate images, but the whole theme behind the water applet seems to fit very nicely into this model.
-Chris