Java Reference
In-Depth Information
How It Works
We first get a Toolkit object by calling the static method getDefaultToolkit() - this is the
key to the other information. The getScreenResolution() returns the number of pixels per inch as
a value of type int . The getScreenSize() method returns a Dimension object which specifies the
width and height of the screen in pixels.
We use the getAllFonts() method discussed previously to get a String array containing the names
of the fonts which we output to the screen.
Font Metrics
Every component has a method getFontMetrics() that you can use to retrieve font metrics - the
wealth of dimensional data about a font. You pass a Font object as an argument to the method, and it
returns an object of type FontMetrics that you can use to obtain data relating to the particular font.
For example, if aWindow is a Frame object and myFont is a Font object, you could obtain a
FontMetrics object corresponding to the font with the statement:
FontMetrics metrics = aWindow.getFontMetrics(myFont);
You could use the getFont() method for a component to explore the characteristics of the font that
the component contains. For example:
FontMetrics metrics = aWindow.getFontMetrics(aWindow.getFont());
You can now call any of the following FontMetrics methods for the object to get at the basic
dimensions of the font:
Method
Description
int getAscent()
Returns the ascent of the font, which is the distance from the
base line to the top of the majority of the characters in the font.
The base line is the line on which the characters rest.
Depending on the font, some characters can extend beyond the
ascent.
int getMaxAscent()
Returns the maximum ascent for the font. No character will
exceed this ascent.
int getDescent()
Returns the descent of the font, which is the distance from the
base line to the bottom of most of the font characters that
extend below the base line. Depending on the font, some
characters may extend beyond the descent for the font.
int getMaxDescent()
Returns the maximum descent of the characters in the font. No
character will exceed this descent.
int getLeading()
Returns the leading for the font, which is the line spacing for
the font - that is the spacing between the bottom of one line of
text and the top of the next. The term originated when type was
actually made of lead, and there was a strip of lead between one
line of type and the next when a page was typeset.
int getHeight()
Returns the height of the font, which is defined as the sum of
the ascent, the descent, and the leading.
Search WWH ::




Custom Search