Source of MetricsJPanel.java


  1: // Fig. 12.15: MetricsJPanel.java
  2: // FontMetrics and Graphics methods useful for obtaining font metrics.
  3: import java.awt.Font;
  4: import java.awt.FontMetrics;
  5: import java.awt.Graphics;
  6: import javax.swing.JPanel;
  7: 
  8: public class MetricsJPanel extends JPanel 
  9: {
 10:    // display font metrics
 11:    public void paintComponent( Graphics g )
 12:    {
 13:       super.paintComponent( g ); // call superclass's paintComponent
 14: 
 15:       g.setFont( new Font( "SansSerif", Font.BOLD, 12 ) );
 16:       FontMetrics metrics = g.getFontMetrics();
 17:       g.drawString( "Current font: " + g.getFont(), 10, 40 );
 18:       g.drawString( "Ascent: " + metrics.getAscent(), 10, 55 );
 19:       g.drawString( "Descent: " + metrics.getDescent(), 10, 70 );
 20:       g.drawString( "Height: " + metrics.getHeight(), 10, 85 );
 21:       g.drawString( "Leading: " + metrics.getLeading(), 10, 100 );
 22: 
 23:       Font font = new Font( "Serif", Font.ITALIC, 14 );
 24:       metrics = g.getFontMetrics( font );
 25:       g.setFont( font );
 26:       g.drawString( "Current font: " + font, 10, 130 );
 27:       g.drawString( "Ascent: " + metrics.getAscent(), 10, 145 );
 28:       g.drawString( "Descent: " + metrics.getDescent(), 10, 160 );
 29:       g.drawString( "Height: " + metrics.getHeight(), 10, 175 );
 30:       g.drawString( "Leading: " + metrics.getLeading(), 10, 190 );
 31:    } // end method paintComponent
 32: } // end class MetricsJPanel
 33: 
 34: /**************************************************************************
 35:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 36:  * Pearson Education, Inc. All Rights Reserved.                           *
 37:  *                                                                        *
 38:  * DISCLAIMER: The authors and publisher of this book have used their     *
 39:  * best efforts in preparing the book. These efforts include the          *
 40:  * development, research, and testing of the theories and programs        *
 41:  * to determine their effectiveness. The authors and publisher make       *
 42:  * no warranty of any kind, expressed or implied, with regard to these    *
 43:  * programs or to the documentation contained in these books. The authors *
 44:  * and publisher shall not be liable in any event for incidental or       *
 45:  * consequential damages in connection with, or arising out of, the       *
 46:  * furnishing, performance, or use of these programs.                     *
 47:  *************************************************************************/