Java Reference
In-Depth Information
Drawing Style and Color
In the DrawingDemoCanvas implementation, you can find two calls to setGrayScale() . The
setGrayScale() method sets the gray scale value for the following drawing operations. Valid
grayscale values range from 0 to 255, where 0 means black and 255 means white. Not all possible
values may actually render to different gray values on the screen. If the device provides fewer than 256
shades of gray, the best fitting value supported by the device is chosen. In the example, the value is first
set to white, and the screen is cleared by the following call to drawRect() . Then, the color is set to
black for the subsequent drawing operations.
The setGrayScale() method is not the only way to influence the color of subsequent drawing.
MIDP also provides a setColor() method. The setColor() method has three parameters
holding the red, green, and blue components of the desired color. Again, the values range from 0 to 255,
where 255 means brightest and 0 means darkest. If all three parameters are set to the same value, the
call is equivalent to a corresponding call of setGrayScale() . If the device is not able to display the
desired color, it chooses the best fitting color or grayscale supported by the device automatically. Some
examples are listed in Table 3.7 .
Table 3.7. Example Color Param e ter Settings
Parameter Settings
Resulting Color
setColor (255, 0, 0)
Red
Green
setColor (0, 255, 0)
setColor (0, 0, 255)
Blue
setColor (128, 0, 0)
Dark red
Yellow
setColor (255, 255, 0)
setColor (0, 0, 0)
Black
White
setColor (255, 255, 255)
setColor (128, 128, 128)
5 0% gray
The only other method that influences the current style of drawing is the setStrokeStyle()
method. The setStrokeStyle() command sets the drawing style of lines to dotted or solid. You
determine the style by setting the parameter to one of the constants DOTTED or SOLID , defined in the
Graphics class.
When the paint() method is entered, the initial drawing color is always set to black and the line
style is SOLID .
Simple Drawing Methods
In the example, you have already seen fillRect() and drawLine() . Table 3.8 shows all drawing
primitives contained in the Graphics class. All operations where the method names begin with draw ,
except drawString() and drawImage() , are influenced by the current color and line style. They
draw the outline of a figure, whereas the fill methods fill the corresponding area with the current
color and do not depend on the line style.
Ta b le 3.8. Drawing Methods of the Graphics Class
Method
Purpose
drawImage (Image
image,
int x, int y,
int align)
Draws an Image. Explained in detail in the " Images " section.
drawString
(String text,
Draws a text string at the given position in the current color; see " Text
and Fonts . "
 
 
Search WWH ::




Custom Search