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.
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.
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); 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 .