14.8. Chapter Summary

 
[Page 440 ( continued )]

13.10. Centering a Display Using the FontMetrics Class

You can display a string at any location in a panel. Can you display it centered? 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 descending line, but some may extend below the descending line.

  • Height is the sum of leading, ascent, and descent.


[Page 441]
Figure 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) 

    Returns the font metrics of the specified font.

  •    public   FontMetrics getFontMetrics() 

    Returns the font metrics of the current font.

You can use the following instance methods in the FontMetrics class to obtain the attributes of a font and the width of a string when it is drawn using the font:

   public int   getAscent()  // Return the ascent    public int   getDescent()  // Return the descent    public int   getLeading()  // Return the leading    public int   getHeight()  // Return the height    public int   stringWidth(String str)  // Return the width of the string  

Listing 13.8 gives an example that displays a message in the center of the panel, as shown in Figure 13.17.

Figure 13.17. The program uses the FontMetrics class to measure the string width and height, and displays it at the center of the panel.

Listing 13.8. TestCenterMessage.java
(This item is displayed on pages 441 - 442 in the print version)
 1   import   javax.swing.*; 2   import   java.awt.*; 3 4   public class   TestCenterMessage   extends   JFrame { 5   public   TestCenterMessage() { 6 CenterMessage messagePanel =   new   CenterMessage(); 7 add(messagePanel); 8 messagePanel.setBackground(Color.WHITE); 

[Page 442]
 9 messagePanel.setFont(   new   Font(   "Californian FB"   , Font.BOLD,   30   )); 10 } 11 12  /** Main method */  13   public static void   main(String[] args) { 14 TestCenterMessage frame =   new   TestCenterMessage(); 15 frame.setLocationRelativeTo(   null   );  // Center the frame  16 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 17 frame.setSize(   300   ,   150   ); 18 frame.setVisible(   true   ); 19 } 20 } 21 22   class   CenterMessage   extends   JPanel { 23  /** Paint the message */  24   protected void   paintComponent(Graphics g) { 25   super   .paintComponent(g); 26 27  // Get font metrics for the current font  28  FontMetrics fm = g.getFontMetrics();  29 30  // Find the center location to display  31   int   stringWidth =  fm.stringWidth(   "Welcome to Java"   );  32   int   stringAscent =  fm.getAscent();  33 34  // Get the position of the leftmost character in the baseline  35   int   xCoordinate = getWidth() /   2   - stringWidth /   2   ; 36   int   yCoordinate = getHeight() /   2   + stringAscent /   2   ; 37 38 g.drawString(   "Welcome to Java"   , xCoordinate, yCoordinate); 39 } 40 } 

The methods getWidth() and getHeight() (lines 35 “36), defined in the Component class, return the component's width and height, respectively.

yCoordinate is the height of the baseline for the first character of the string to be displayed. When centered is true , yCoordinate should be getHeight() / 2 + h / 2 , where h is the ascent of the string.

xCoordinate is the width of the baseline for the first character of the string to be displayed. When centered is true , xCoordinate should be getWidth() / 2 - stringWidth / 2 .

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net