Java Reference
In-Depth Information
Figure 7-8. A font's ascent and descent are relative to its baseline.
Everyfontisassociatedwithvariousmeasurements.The ascent isthatportionofthe
font'scharactersabovethebaseline;the descent isthatportionofthesecharactersbelow
thebaseline.Extraspaceaddedbetweenlinesoftextisknownas leading .Whenadded
together,ascent,descent,andleadingformthefont's height .Lastly,the advance roughly
specifies the baseline location where the next character should appear.
AWT's java.awt.FontMetrics class encapsulates this measurement inform-
ation. You can obtain an instance of this class by calling the Graphics class's
FontMetrics getFontMetrics() method, which returns the font metrics for
the current font. Among its various methods, you will find the int
stringWidth(String str) method (which returns the total advance width for
showing str 's characters in the current font) useful for centering a string horizontally.
component classandoverriding paint() ,youshouldtrytoavoiddoingso,toavoid
vantage of AWT's Canvas class, which is intended for this purpose.
Touse Canvas ,youmustextendthisclassandoverride paint() .Youalsoneedto
task by overriding getPreferredSize() to return a Dimension object contain-
ingthecanvas'sextents,orbyinvoking setPreferredSize() witha Dimension
object containing the preferred size (as demonstrated in fixGUI() ).
I'vecreateda Geometria applicationthatdemonstrates Canvas .(Although Geo-
metria is just a skeleton that presents a Canvas -based splash-screen component, it
couldbeturnedintoafull-blownapplication forteaching basicgeometry.) Listing7-3
excerpts this application's SplashCanvas class.
Listing 7-3 . Creating a splash screen
Search WWH ::

Custom Search