Java Reference
In-Depth Information
It's up to the implementation to decide exactly how dotted lines are implemented, so
dotted lines on one device may look dashed on another. You can set or retrieve the current style
using setStrokeStyle() and getStrokeStyle() . For example, the following code draws a
square with a solid outline (the default) and another square with a dotted outline:
public void paint(Graphics g) {
g.drawRect(20, 10, 35, 35);
g.setStrokeStyle(Graphics.DOTTED);
g.drawRect(20, 60, 35, 35);
}
Drawing Text
The Graphics class makes it easy to draw text anywhere on the screen. Text drawing is based
around the idea of an anchor point. The anchor point determines exactly where the text will be
drawn. Anchor points are described with a horizontal and vertical component. The Graphics
class defines the horizontal and vertical anchor points as constants. Figure 13-4 illustrates the
various anchor points for a string of text. Each anchor point is described as a combination of a
horizontal and vertical anchor point.
Figure 13-4. Text anchor points
To draw text, you just need to specify the text itself and the location and type of anchor
point. You could, for example, place some text in the upper-left corner of the screen by using a
TOP | LEFT anchor point located at 0, 0.
Text is specified as a String or an array of chars , which means you can draw text in many
languages, provided that the fonts you're using have the corresponding glyphs.
Graphics provides four different methods for drawing text. You can draw characters or
String s, depending on what you have available:
 
Search WWH ::




Custom Search