Java Reference
In-Depth Information
Figure 3G.9
Output of DrawColorGradient
Another drawing command worth mentioning can be used to include text in your
drawings. The
drawString
method of the
Graphics
object draws the given
String
with its lower-left corner at coordinates (
x
,
y
):
g.drawString(<message>, <x>, <y>);
This is a slightly different convention than we used for
drawRect
. With
drawRect
, we specified the coordinates of the upper-left corner. Here we specify the
coordinates of the lower-left corner. By default, text is drawn approximately 10 pixels
high. Here is a sample program that uses a loop to draw a particular
String
10 dif-
ferent times, each time indenting it 5 pixels to the right and moving it down 10 pixels
from the top:
1 // Draws a message several times.
2
3
import
java.awt.*;
4
5
public class
DrawStringMessage1 {
6
public static void
main(String[] args) {
7
DrawingPanel panel =
new
DrawingPanel(200, 100);
Search WWH ::
Custom Search