Note that Java does provide much more extensive support for color modeling than we have the space to discuss in this book, so if you want to know more, look in the classes of the java.awt.color package.
When you want to define an image to be transparent to some degree, so that when it is displayed on top of a background image the underlying image shows through, you can specify the degree of transparency by something called an alpha component for each pixel. An image format that has an alpha component for each pixel is said to have an alpha channel. Note that not all image formats support an alpha channel, but PNG files do, and GIF images can have a single transparent color. The alpha component value is used when an image is being overlaid by another image. The alpha value is multiplied by each of the color components to modify the contribution of each color component to the visual appearance of the pixel. The alpha value for a pixel in an image can vary from a minimum of 0.0, meaning completely transparent and therefore invisible since all the color components will be 0, to a maximum of 1.0, meaning completely opaque. In an RGB image with an alpha channel, each pixel is defined by four components, the three color components, red, green and blue, plus the alpha component. This allows the transparency to vary over the image, so some parts of the image could be opaque - with an alpha component of 1.0, and other parts may be more or less transparent, with alpha components for the pixels less than 1.0. It's worth noting that both the source image, the image that you are drawing, and the destination image, the background in other words, can have an alpha channel. If an image has no alpha channel, then the alpha component is assumed to be 1.0.
When you draw a source image over a destination image, there are basically two steps to the process. The color components for each pixel in the source image and the corresponding pixels in the destination image will be multiplied by their alpha component - often this will be done once and for all ahead of time for an image to avoid all those multiplications each time you draw an image. The source image is then rendered over the destination image according to the alpha compositing rule that is in effect. There are several alpha compositing rules as we shall see, but they each determine the fraction of the source image components and the fraction of the destination image components that contribute to the components of the result. In general, the components of the source and destination pixels are combined as follows:
ColorR = ColorS*AlphaS*FractionS + ColorD*AlphaD*FractionD
AlphaR = AlphaS*FractionS + AlphaD*FractionD
The first equation applies to each of the three color components. The subscripts R, S, and D refer to the resultant pixel, the source pixel and the destination pixel, respectively. Thus, ColorR refers to the color of the resultant pixel produced by combining the source and the destination, AlphaS refers to the alpha component for the source pixel and FractionS represents the fraction of the source pixel determined by the compositing rule in effect.
This sounds a lot more complicated than it really is, so don't be put off by these equations. The alpha compositing rules that you can use in Java are implemented by the AlphaComposite class that is defined in the java.awt package. The Graphics2D class defines the method setComposite() that takes an AlphaComposite argument in order to set the alpha compositing rule to be used when you draw in the graphics context. Let's take a look at the AlphaComposite class in more detail.
The AlphaComposite Class
There is no constructor for the AlphaComposite class, so you cannot create objects directly. There is a static class member, getInstance(), that will return a reference to an AlphaComposite object with the compositing rule specified by the argument, which is a value of type int. There is also an overloaded version of this method where you can specify an alpha value as a second argument of type float, which is multiplied by the alpha for the source image. This is particularly useful when the source image has no alpha channel. Since an image with no alpha channel has an alpha component that is assumed to be 1.0, the alpha value that you specify in the call to getInstance() becomes the alpha value for all the pixels in the source. Most of the time, your images will not have an alpha channel so this is a way for you to specify the transparency of the source image directly in the graphics context.
There are eight possible alpha compositing rules, determined by constants of type int that are defined in the AlphaComposite class. In reviewing these, we will assume that, if the source or destination image has an alpha channel, the color components, ColorS and ColorD, for each pixel have already been pre-multiplied by the alpha component, AlphaS or AlphaD respectively. In the illustration of the effect of each rule described below, the source image is the light gray circle, and this is rendered over the darker gray rectangle (the destination image). The source image has its alpha component set to 0.5f.

SRC_OVER
This is the default rule that applies in a graphics context and is the rule that you are most likely to be using. The fraction of the source that contributes to the result is 1, and the fraction of the destination contributing to the result is 1-AlphaS.Therefore from our general equations, the source pixels are combined with the corresponding destination pixels using the following operations:
ColorR = ColorS + (1-AlphaS)*ColorD
AlphaR = AlphaS + (1-AlphaS)*AlphaD
The calculation of the resultant color is applied to each of the red, green and blue components of each pixel. You can see from the equations above that if the alpha component for the source, AlphaS, is 1, then the fraction of the destination will be zero so the result is just the original source pixel - in other words the source is opaque. If the alpha component for the source is 0, then the result is just the destination pixel so the source is completely transparent and would be invisible. The illustration shows the source with an alpha of 0.5f so the destination shows through.

SRC
With this rule, the source pixels replace the destination pixels, so the operations determining the resultant color and alpha components for each pixel are:
ColorR = ColorS
AlphaR = AlphaS

SRC_IN
With this rule, the fraction of the source in the result is the alpha for the destination, AlphaD, and the fraction of the destination in the result is zero. Thus only the source pixels that fall within the area destination image are rendered. All other pixels rendered from the source will have zero color components. As you can see, the outline of the destination image acts like a pastry cutter on the source image. The operations for the rule are:
ColorR = ColorS*AlphaD
AlphaR = AlphaS*AlphaD

Continued...