Beginning Java 2- JDK 1.3 Version Images and Animation
Transforming Images
Alpha compositing is all about the transparency of an image, and we
are just going to look at the basic ideas here. The subject of color
representation is itself a major topic with a range of different ways of
representing color, but we will assume we are dealing with the RGB model
throughout and ignore other color models. 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.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|