One of Java's most touted features is its support for international scripts via Unicode. Figure 3: A drawBytes() demonstration Unicode support Figure 3 shows what the result would look like if this code where part of a suitable AWT component object. The above code shows the drawBytes() method in use within a component's paint() method. The drawChars() method below takes as parameters a character array containing the text to be drawn, an integer value indicating the offset into the array at which to begin, an integer value indicating the number of characters to draw, and two integer values specifying the coordinates where the text should start.Ĭhar rgc = Void drawChars(char data, int offset, int length, int x, int y) Figure 1 shows what the result would look like if this code were part of a suitable AWT component object. The x and y coordinates specify the location of the lower-left corner of the enclosing text box. The code in this example draws the word "abc" on the component containing this paint() method. The code in the listing above shows the drawString() method in use within a component's paint() method. 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. Void drawString(String str, int x, int y) The Graphics class provides three methods that draw text on a component or an image. Let's begin by examining the Graphics class's methods for drawing text. The Graphics class works together with the Font and FontMetrics classes to provide the tools necessary to draw text within an image or component. These two methods are called as part of the normal drawing cycle initiated within the AWT. 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. Because the Graphics class is an abstract base class, it cannot be instantiated directly. In order to draw, a program requires a valid graphics context (represented by an instance of the Graphics class). All of the graphics-related operations on a component or image occur via one of these methods. 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. More importantly, the graphics context defines the destination for the graphics operations about to be discussed (destinations include components and images). This includes the drawing color, the font, and the location and dimensions of the clipping rectangle (the region in which graphics can be drawn). First, it maintains the graphics context, which consists of all of the information that will affect the outcome of a graphics operation. The Graphics class plays two different but related roles within the abstract windowing toolkit (AWT). Readers looking for thorough coverage should read my October column, available here. This section presents a brief overview of the function and operation of the Graphics class. In order to use the text methods of the Graphics class, an understanding of the role of the Graphics class itself is required. Before I begin, however, a short review of the role of the Graphics class is in order. This column will cover each of these classes in turn and will show you how to use them together. 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. In addition to methods for drawing primitive geometric types like lines and circles, the Graphics class provides methods for drawing text.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |