Transparent Image Graphics for Java

It's not widely advertised, but java does support full 8-bit transparency (not just the 1-bit transparency in GIF images), and always has. GIF images are limited to 256 colors, and transparency is strictly on-off, which produces jagged edges.   Java also supports transparency in PNG format images, but few tools know how to read and write PNG.  PNG allows full color images, but  full color PNGs are a lot larger than equivalent JPG images.

Inexplicably, there is no predefined way to actually load or create a transparent JPG image.  I imagine this is another manifestation of Java's graphics philosophy starting with AWT, which seems to have been to include just those things needed for the next demo.

So here, is a snippet of hard won knowledge, with full source code.

My solution to the transparency problem is to use jpeg images (which do not support transparency) but to load pairs of images and composite them as soon as they  have been loaded, to produce an image with transparency for use in the applet graphics. 

Image #1 at the left is the raw jpeg image. Image #2 is photoshopped copy of image number #1, converted to gray scale and made White for  transparency.  It's quite easy to make such images using photoshop or other image manipulation tools.
Image #3 is the resulting composited image.

If you found this tidbit useful, please help spread the knowledge by linking to this page.

comments/suggestions to:
my home page