Java Reference
In-Depth Information
46 polygon.addPoint(( int )(xCenter + radius *
47 Math.cos( 4 * 2 * Math.PI / 6 )), ( int )(yCenter - radius *
48 Math.sin( 4 * 2 * Math.PI / 6 )));
49 polygon.addPoint(( int )(xCenter + radius *
50 Math.cos( 5 * 2 * Math.PI / 6 )), ( int )(yCenter - radius *
51 Math.sin( 5 * 2 * Math.PI / 6 )));
52
53
// Draw the polygon
54
55 }
56 }
g.drawPolygon(polygon);
draw polygon
13.17
Draw a polygon connecting the following points: ( 20 , 40 ), ( 30 , 50 ), ( 40 , 90 ), ( 90 ,
10 ), ( 10 , 30 ).
Check
Point
13.18
Create a Polygon object and add points ( 20 , 40 ), ( 30 , 50 ), ( 40 , 90 ), ( 90 , 10 ), ( 10 ,
30 ) in this order. Fill the polygon with the red color. Draw a polyline with a yellow
color to connect all these points.
13.7 Centering a String Using the FontMetrics Class
You can use the FontMetrics class to measure the width and height of a string in the
graphics context.
Key
Point
You can display a string at any location in a panel. Can you display it centered? Yes; to do so,
you need to use the FontMetrics class to measure the exact width and height of the string
for a particular font. FontMetrics can measure the following attributes for a given font (see
Figure 13.16):
Leading, pronounced ledding , is the amount of space between lines of text.
Ascent is the distance from the baseline to the ascent line. The top of most characters
in the font will be under the ascent line, but some may extend above the ascent line.
Descent is the distance from the baseline to the descent line. The bottom of most
descending characters (e.g., j , y , and g ) in the font will be above the descent line, but
some may extend below the descent line.
Height is the sum of leading, ascent, and descent.
Leading
Ascent line
By
Height
Ascent
Baseline
Descent
Descent line
F IGURE 13.16 The FontMetrics class can be used to determine the font properties of
characters for a given font.
FontMetrics is an abstract class. To get a FontMetrics object for a specific font, use the
following getFontMetrics methods defined in the Graphics class:
public FontMetrics getFontMetrics(Font font)
This method returns the font metrics of the specified font.
public FontMetrics getFontMetrics()
 
 
 
Search WWH ::




Custom Search