Java Reference
In-Depth Information
This method expects the red, green, and blue values in a packed integer, as shown in
Figure 13-3.
Figure 13-3. Packing a color into an integer
An alternate convenience method accepts red, green, and blue values as integers in the
range from 0 to 255 inclusive:
public void setColor(int red, int green, int blue)
You can retrieve the current drawing color (as a packed integer) with getColor() . Alternatively,
you can retrieve each component separately using getRedComponent() , getGreenComponent() , and
getBlueComponent() .
Of course, different devices will have different levels of color support, from black and
white (affectionately known as “1-bit color”) through full 24-bit color. As we mentioned in
Chapter 5, the isColor() and numColors() methods in Display return useful information about
the capabilities of the device.
For grayscale devices, Graphics provides setGrayScale() as a convenience method. You
pass it a number from 0 (black) to 255 (white). You can find out the current grayscale value by
calling getGrayScale() . If the current color of this Graphics is not a grayscale color (that is, if the
red, green, and blue values of the current color are not the same), then this method returns its
best guess as to the brightness of the current color.
Another handy method in the Graphics class is getDisplayColor() . This method can tell
you at runtime exactly how a requested color will be displayed on the device. You feed it a color
int , and it returns the color int that will actually be displayed on the device. For example, on
the WTK's DefaultGrayPhone emulator, pure green ( 0x00ff00 ) maps to the gray level 0x959595 .
Line Styles
Graphics also maintains a current line style, called a stroke style, that is used for drawing shape
outlines and lines. There are two choices for line style, represented by constants in the
Graphics class:
SOLID is the default.
DOTTED lines may also be drawn.
Search WWH ::




Custom Search