Java Reference
In-Depth Information
coordinates and dimensions,
Graphics2D
works with floating-point values in a
user space that is transformed to the device space of a screen display or printer.
The
rendering
system does the work of transforming the floating-point values in
the drawing method arguments to integer pixel or dot numbers for drawing on a
device.
Forlow-resolution devices, such as monitor screens, the conversion from user
space units goes as the screen resolution, e.g. 96.0 units give 1 inch on a 96
pixels/inch screen, 2 inches for 48 pixels/inch, etc. On a high resolution device
such as a printer, 72 user units always gives 1 inch regardless of the dots-per-
inch setting [3]. You can modify the conversions with the
scale()
method in
Graphics2D
.
6.6.3 Color
The class
java.awt.Color
defines the properties of a color in Java. The default
color space in Java is the sRGB (standard RGB), which offers “a simple and robust
device independent color definition” [4]. In this space a color is defined by its
RGB (Red-Green-Blue) color component values. It spans a subset of the standard
CIEXYZ color space that includes all visible colors [5].
A color model, based on a particular color space, specifies exactly how a color
is represented. In Java the default ARGB model uses eight bits for each of the
RGB components plus another eight bits for an alpha transparency factor that
specifies what happens when a color is drawn over another. Other color models,
such as a gray scale model, can also be obtained.
For convenience, there are several
Color
constructors. Some constructors
take
int
values between 0 and 255 for each color component. Some take
float
values between
0.0f
and
1.0f
. There are four-parameter construc-
tors that let you specify the R, G, B, and alpha values and three-parameter ver-
sions that default to opaque alpha. In integer format, alpha is 0 for completely
transparent and 255 for opaque and in floating-point these are
0.0f/1.0f,
respectively.
An example of a three-parameter
int
constructor is shown here:
Color red = new Color (0xFF, 0, 0); // R, G, B,
// default alpha = 255
Note that it is common to use hexadecimal values to specify color compo-
nents and here we used
0xFF
instead of the decimal 255 for the red compo-
nent. The three color components and alpha, each represented by a byte value,
can be packed into an
int
. One constructor has a single
int
argument for
packed RGB (alpha defaults to
0xFF
) and another constructor has an
int
plus a
boolean
that is
true
if the integer value includes an alpha component, as shown
here:
Search WWH ::
Custom Search