pen.drawString( text-to-display, x, y, drawingColor ) pen.clearString( text-to-display, x, y) pen.invertString( text-to-display, x, y)
Text can be placed within an applet's region of the screen by either using the "drawString" method or adding a "Label" component to the applet. The use of a Label component is appropriate when the text is intended as the label of some other GUI component included in the applet. The "drawString" method should be used when the text is part of a drawing produced using other "pen" methods.
Typically, three parameters are provided when "drawString" in invoked. The first is the string of characters to be displayed on the screen. The next two parameters are the x and y coordinates of the point at which the lower left corner of the first character displayed should appear. For example:
might be used to place the text "The Top" near the top of the applet's drawing region (and 40 pixels from the left edge of the applet's drawing region).pen.drawString("The Top",40,15);
The x and y coordinates are optional as a pair. They must either both be included or both be omitted. If they are omitted, the text specified by the first parameter will be displayed centered in the applet's drawing area.
Normally, text drawn using "drawString" appears in the current foreground color. An optional color description can be included as the last parameter to the method to request that the text be drawn in a different color. This parameter can be included whether or not x and y coordinates are provided. As in other "pen" methods, the color can be specified by name or using a color constructor as explained in the discussion of colors above.
As with line drawing, there are actually three primitives for "drawing" text in an applet: "drawString", "clearString" and "invertString". Like "clearLine", "clearString" is equivalent to "drawString" except that the text specified is drawn using the background color. The "invertString" primitive reverses the color of the pixels that would have been filled in by a "drawString". It is useful for placing temporary text on the screen since a second, identical invocation of "invertString" will undo the effect of the first.