14.3. Listeners, Registrations, and Handling Events

 
[Page 431 ( continued )]

13.6. Drawing Strings, Lines, Rectangles, and Ovals

The drawString(String s, int x, int y) method draws a string starting at the point (x, y) , as shown in Figure 13.6(a).

Figure 13.6. (a) The drawString(s, x, y) method draws a string starting at (x, y) . (b) The drawLine(x1, y1, x2, y2) method draws a line between two specified points.

The drawLine(int x1, int y1, int x2, int y2) method draws a straight line from point (x1, y1) to point (x2, y2) , as shown in Figure 13.6(b).

Java provides six methods for drawing rectangles in outline or filled with color . You can draw plain rectangles, round-cornered rectangles, or three-dimensional rectangles.

The drawRect(int x, int y, int w, int h) method draws a plain rectangle, and the fillRect(int x, int y, int w, int h) method draws a filled rectangle. The parameters x and y represent the upper-left corner of the rectangle, and w and h are its width and height (see Figure 13.7).

Figure 13.7. (a) The drawRect(x, y, w, h) method draws a rectangle. (b) The fillRect(x, y, w, h) method draws a filled rectangle.
(This item is displayed on page 432 in the print version)

The drawRoundRect(int x, int y, int w, int h, int aw, int ah) method draws a round-cornered rectangle, and the fillRoundRect(int x, int y, int w, int h, int aw, int ah) method draws a filled round-cornered rectangle. Parameters x , y , w , and h are the same as in the drawRect method, parameter aw is the horizontal diameter of the arcs at the corner, and ah is the vertical diameter of the arcs at the corner (see Figure 13.8(a)). In other words, aw and ah are the width and the height of the oval that produces a quarter-circle at each corner.


[Page 432]
Figure 13.8. (a) The drawRoundRect(x, y, w, h, aw, ah) method draws a round-cornered rectangle. (b) The drawOval(x, y, w, h) method draws an oval based on its bounding rectangle.


The draw3DRect(int x, int y, int w, int h, boolean raised) method draws a 3D rectangle and the fill3DRect(int x, int y, int w, int h, boolean raised) method draws a filled 3D rectangle. The parameters x , y , w , and h are the same as in the drawRect method. The last parameter, a Boolean value, indicates whether the rectangle is raised above the surface or sunk into the surface.

Depending on whether you wish to draw an oval in outline or filled solid, you can use either the drawOval(int x, int y, int w, int h) method or the fillOval(int x, int y, int w, int h) method. An oval is drawn based on its bounding rectangle. Parameters x and y indicate the top-left corner of the bounding rectangle, and w and h indicate the width and height, respectively, of the bounding rectangle, as shown in Figure 13.8(b).

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net