Beginning Java 2- JDK 1.3 Version Images and Animation
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
|
SRC_OUT
With this rule, only the source pixels outside the area of the destination
will be rendered. The outline of the destination image also acts like a
pastry cutter here, but the source image inside the outline is discarded,
and only the source image lying outside of the destination image is kept.
The area of the source that lies inside the boundary of the destination
results in pixels with color components that are zero. The calculation of
the components of the result is defined as:
|
ColorR = ColorS*(1 - AlphaD)
AlphaR = AlphaS*(1 - AlphaD)
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.
|