19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

13.7 Centering a String Using the FontMetrics Class 493<br />

46 polygon.addPoint((int)(xCenter + radius *<br />

47 Math.cos(4 * 2 * Math.PI / 6)), (int)(yCenter – radius *<br />

48 Math.sin(4 * 2 * Math.PI / 6)));<br />

49 polygon.addPoint((int)(xCenter + radius *<br />

50 Math.cos(5 * 2 * Math.PI / 6)), (int)(yCenter – radius *<br />

51 Math.sin(5 * 2 * Math.PI / 6)));<br />

52<br />

53 // Draw the polygon<br />

54 g.drawPolygon(polygon);<br />

55 }<br />

56 }<br />

13.17 Draw a polygon connecting the following points: (20, 40), (30, 50), (40, 90), (90,<br />

10), (10, 30).<br />

13.18 Create a Polygon object and add points (20, 40), (30, 50), (40, 90), (90, 10), (10,<br />

30) in this order. Fill the polygon with the red color. Draw a polyline with a yellow<br />

color <strong>to</strong> connect all these points.<br />

13.7 Centering a String Using the FontMetrics Class<br />

You can use the FontMetrics class <strong>to</strong> measure the width and height of a string in the<br />

graphics context.<br />

You can display a string at any location in a panel. Can you display it centered? Yes; <strong>to</strong> do so,<br />

you need <strong>to</strong> use the FontMetrics class <strong>to</strong> measure the exact width and height of the string<br />

for a particular font. FontMetrics can measure the following attributes for a given font (see<br />

Figure 13.16):<br />

■ Leading, pronounced ledding, is the amount of space between lines of text.<br />

■ Ascent is the distance from the baseline <strong>to</strong> the ascent line. The <strong>to</strong>p of most characters<br />

in the font will be under the ascent line, but some may extend above the ascent line.<br />

■ Descent is the distance from the baseline <strong>to</strong> the descent line. The bot<strong>to</strong>m of most<br />

descending characters (e.g., j, y, and g) in the font will be above the descent line, but<br />

some may extend below the descent line.<br />

■ Height is the sum of leading, ascent, and descent.<br />

draw polygon<br />

✓Point✓ Check<br />

Key<br />

Point<br />

Ascent line<br />

Height<br />

Baseline<br />

Descent line<br />

By<br />

Leading<br />

Ascent<br />

Descent<br />

FIGURE 13.16 The FontMetrics class can be used <strong>to</strong> determine the font properties of<br />

characters for a given font.<br />

FontMetrics is an abstract class. To get a FontMetrics object for a specific font, use the<br />

following getFontMetrics methods defined in the Graphics class:<br />

■ public FontMetrics getFontMetrics(Font font)<br />

This method returns the font metrics of the specified font.<br />

■ public FontMetrics getFontMetrics()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!