Figure 12.9 HSB and RGB tabs of the J C o l o r C h o o s e r dialog [View full size image] [Page 606 (continued)] 12.4 Font Control This section introduces methods and constants for font control Most font methods and font constants are part of class Font Some methods of class Font and class Graphics are summarized in Fig 12.10 [Page 607] Figure 12.10 F o n t -related methods and constants Method or constant Description Font constants, constructors and methods p u b li c f i n a l s t a t i c i n t P L A I N A constant representing a plain font style p u b li c f i n a l s t a t i c i n t B O L D A constant representing a bold font style p u b li c f i n a l s t a t i c i n t I T A L I C A constant representing an italic font style p u b li c Font( String name , int style , int size ) Creates a Font object with the specified font name, style and size p u b li c i n t getStyle() Returns an integer value indicating the current font style p u b li c i n t getSize() Returns an integer value indicating the current font size p u b li c String getName() Returns the current font name as a string p u b li c String getFamily() Returns the font's family name as a string p u b li c b o o l e a n isPlain() Returns TRue if the font is plain, else false p u b li c b o o l e a n isBold() Returns true if the font is bold, else false p u b li c b o o l e a n isItalic() Returns TRue if the font is italic, else false Graphics methods for manipulating Fonts p u b li c Font getFont() Returns a Font object reference representing the current font p u b li c v o i d setFont( Font f ) Sets the current font to the font, style and size specified by the Font object reference f Class F on t 's constructor takes three argumentsthe font name, font style and font size The font name is any font currently supported by the system on which the program is running, such as standard Java fonts Monospaced , SansSerif and Serif The font style is F o n tt P LL A II N , FF o nn tt II T A LL I C or FF oo n tt BB OO LL DD (each is a static field of class Font ) Font styles can be used in combination (e.g., Font.ITALIC + Font.BOLD ) The font size is measured in points A point is 1/72 of an inch Graphics method ss ee tt FF oo nn t sets the current drawing fontthe font in which text will be displayedto its Font argument [Page 608] Portability Tip 12.2 The number of fonts varies greatly across systems Java provides five logical font names Serif, Monospaced , SansSerif , Dialog and DialogInput that can be used on all Java platforms The Java runtime environment (JRE) on each platform maps these logical font names to actual fonts installed on the platform The actual fonts used may vary by platform The application of Fig 12.11Fig 12.12 displays text in four different fonts, with each font in a different size Figure 12.11 uses the Font constructor to initialize Font objects (in lines 16, 20, 24 and 29) that are each passed to Graphics method setFont to change the drawing font Each call to the Font constructor passes a font name ( Serif , Monospaced or SansSerif ) as a string, a font style ( Font.PLAIN , Font.ITALIC or Font.BOLD ) and a font size Once Graphics method setFont is invoked, all text displayed following the call will appear in the new font until the font is changed Each font's information is displayed in lines 17, 21, 25 and 3031 using method drawString Note that the coordinate passed to drawString corresponds to the lower-left corner of the baseline of the font Line 28 changes the drawing color to red, so the next string displayed appears in red Lines 3031 display information about the final Font object Method g ee tt F oo nn tt of class Graphics returns a Font object representing the current font Method gg ee t NN aa mm ee returns the current font name as a string Method g e tt S ii z e returns the font size in points [Page 609] Figure 12.11 G r a p h i c s method s e t F o n t changes the drawing font (This item is displayed on page 608 in the print version) 10 11 12 13 14 15 // Fi g 1 : F o n t J P a n e l ja va // Di s p l a y s t r i n g s i n d i f f er ent fonts and color s im por t j a v a a w t F o n t ; im por t j a v a a w t C o l o r ; im por t j a v a a w t G r a p h i c s ; im por t j a v a x s w i n g J P a n e l ; pu bli c c l a s s F o n t J P a n e l e x te nds JPane l { // d i s p l a y S t r i n g s i n d if ferent fo nts and co lors pu b l i c v o i d p a i n t C o m p o n en t( Graphi cs g ) { s u p e r p a i n t C o m p o n e n t ( g ); // c all superc lass's paint Conpon ent / / s e t f o n t t o S e r i f ( Times), b old, 12pt and dr aw a s tring 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 g s e t F o n t ( n e w F o n t ( " Serif", F ont BOLD, 12 ) ) ; g d r a w S t r i n g ( " S e r i f point b old ", 20, 50 ); / / s e t f o n t t o M o n o s pa ced (Cour ier) , ital ic, 24 pt and draw a str ing g s e t F o n t ( n e w F o n t ( " Monospace d", Font.I TALIC, 24 ) ); g d r a w S t r i n g ( " M o n o s pa ced 24 po int italic ", 20 , 70 ) ; / / s e t f o n t t o S a n s S er if (Helve tica ), pla in, 14 pt and draw a str ing g s e t F o n t ( n e w F o n t ( " SansSerif ", F ont.PL AIN, ) ); g d r a w S t r i n g ( " S a n s S er if 14 poi nt p lain." , 20, 90 ); / / s e t f o n t t o S e r i f ( Times), b old/ italic , 18pt and d raw a strin g g s e t C o l o r ( C o l o r R E D ); g s e t F o n t ( n e w F o n t ( " Serif", F ont BOLD + Font ITALIC , 18 ) ); g d r a w S t r i n g ( g g e t F on t().getNa me() + " " + g.g etFont ().get Size( ) + " p o i n t b o l d i t a l ic ", 20, 10 ) ; } / / e n d m e t h o d p a i n t C o mp onent } // e n d c l a s s F o n t J P a n e l Figure 12.12 Creating J F r a m e to display fonts 10 11 12 13 14 15 16 17 18 19 // Fi g : F o n t s j a v a // Us i n g f o n t s im por t j a v a x s w i n g J F r a m e ; pu bli c c l a s s F o n t s { // e x e c u t e a p p l i c a t i o n pu b l i c s t a t i c v o i d m a i n ( String ar gs[] ) { / / c r e a t e f r a m e f o r Fo ntJPanel J F r a m e f r a m e = n e w J Fr ame( "Usi ng f onts" ); f r a m e s e t D e f a u l t C l o s eO peration( JFr ame.EX IT_ON_ CLOSE ); F o n t J P a n e l f o n t J P a n e l = new Fon tJPa nel(); // cr eate F ontJPa nel f r a m e a d d ( f o n t J P a n e l ); // add fon tJPane l to f rame f r a m e s e t S i z e ( , 17 ); // s et f rame s ize f r a m e s e t V i s i b l e ( t r ue ); // di spla y fram e } // end main } // e n d c l a s s F o n t s Figure 12.12 contains method main , which creates a JFrame We add a FontJPanel object to this JFrame (line 15), which displays the graphics created in Fig 12.11 Software Engineering Observation 12.2 To change the font, you must create a new Font object Font objects are immutableclass Font has no set methods to change the characteristics of the current font Font Metrics Sometimes it is necessary to get information about the current drawing font, such as its name, style and size Several Font methods used to get font information are summarized in Fig 12.10 Method gg ee t SS tt yy ll ee returns an integer value representing the current style The integer value returned is either Font.PLAIN , Font.ITALIC , Font.BOLD or the combination of Font.ITALIC and Font.BOLD Method gg ee t FF aa mm ii ll yy returns the name of the font family to which the current font belongs The name of the font family is platform specific Font methods are also available to test the style of the current font, and these too are summarized in Fig 12.10 Methods ii ss P l a ii nn, ii s B oo l dd and ii s I tt a ll ii c return TRue if the current font style is plain, bold or italic, respectively [Page 610] Sometimes precise information about a font's metrics must be knownsuch as height, descent (the amount a character dips below the baseline), ascent (the amount a character rises above the baseline) and leading (the difference between the descent of one line of text and the ascent of the line of text below itthat is, the interline spacing) Figure 12.13 illustrates some of the common font metrics Figure 12.13 Font metrics [View full size image] Class F oo nn tt MM e tt rr i cc ss declares several methods for obtaining font metrics These methods and Graphics method gg ee tt FF oo nn t MM ee t rr ii cc ss are summarized in Fig 12.14 The application of Fig 12.15Fig 12.16 uses the methods of Fig 12.14 to obtain font metric information for two fonts Figure 12.14 F o n t M e t r i c s and G r a p h i c s methods for obtaining font metrics Method Description FontMetrics methods p u b li c i n t getAscent() Returns the ascent of a font in points Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] y -coordinate 2nd 3rd y axis 2nd Y _ AX I S constant of Box Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] zero-based counting zeroth element zooming ... ic ", 20, 10 ) ; } / / e n d m e t h o d p a i n t C o mp onent } // e n d c l a s s F o n t J P a n e l Figure 12 .12 Creating J F r a m e to display fonts 10 11 12 13 14 15 16 17 18 19 // Fi... [Page 609] Figure 12 .11 G r a p h i c s method s e t F o n t changes the drawing font (This item is displayed on page 608 in the print version) 10 11 12 13 14 15 // Fi g 1 : F o n t J P a n... ii cc ss are summarized in Fig 12 .14 The application of Fig 12 .15 Fig 12 .16 uses the methods of Fig 12 .14 to obtain font metric information for two fonts Figure 12 .14 F o n t M e t r i c s and G