Graphics class provides methods for drawing text. When combined with the Font and FontMetrics classes, the result is a set of tools that makes the job of drawing appealing text much easier than it otherwise might be.
This column will cover each of these classes in turn and will show you how to use them together. Before I begin, however,
a short review of the role of the Graphics class is in order.In order to use the text methods of the Graphics class, an understanding of the role of the Graphics class itself is required. This section presents a brief overview of the function and operation of the Graphics class. Readers looking for thorough coverage should read my October column, available here.
The Graphics class plays two different but related roles within the abstract windowing toolkit (AWT). First, it maintains the graphics
context, which consists of all of the information that will affect the outcome of a graphics operation. This includes the
drawing color, the font, and the location and dimensions of the clipping rectangle (the region in which graphics can be drawn).
More importantly, the graphics context defines the destination for the graphics operations about to be discussed (destinations
include components and images).
In addition to its role as the graphics context, the Graphics class provides methods for drawing simple geometric shapes, text, and images to the graphics destination. All of the graphics-related operations on a component or image occur via one of these methods.
In order to draw, a program requires a valid graphics context (represented by an instance of the Graphics class). Because the Graphics class is an abstract base class, it cannot be instantiated directly. An instance typically is created by a component, and
then handed to the program as an argument to a component's update() and paint() methods. These two methods are called as part of the normal drawing cycle initiated within the AWT.
The Graphics class works together with the Font and FontMetrics classes to provide the tools necessary to draw text within an image or component. Let's begin by examining the Graphics class's methods for drawing text.
The Graphics class provides three methods that draw text on a component or an image.
void drawString(String str, int x, int y)
The drawString() method, shown below, takes as parameters an instance of the String class containing the text to be drawn, and two integer values specifying the coordinates where the text should start.
public void paint(Graphics g)
{
g.drawString("abc", 25, 25);
}
The code in the listing above shows the drawString() method in use within a component's paint() method. The code in this example draws the word "abc" on the component containing this paint() method. The x and y coordinates specify the location of the lower-left corner of the enclosing text box. Figure 1 shows what the result would look like if this code were part of a suitable AWT
component object.
Graphics API:Font API:FontMetrics API:Graphics class: