Web host 4 life - Chapter 11 Graphics and Java2D 617 The program

Chapter 11 Graphics and Java2D 617 The program of Fig. 11.12 uses the methods of Fig. 11.11 to obtain font metric information for two fonts. 1 // Fig. 11.12: Metrics.java 2 // Demonstrating methods of class FontMetrics and 3 // class Graphics useful for obtaining font metrics. 4 5 // Java core packages 6 import java.awt.*; 7 import java.awt.event.*; 8 9 // Java extension packages 10 import javax.swing.*; 11 12 public class Metrics extends JFrame { 13 14 // set window’s title bar String and dimensions 15 public Metrics() 16 { 17 super( “Demonstrating FontMetrics” ); 18 19 setSize( 510, 210 ); 20 setVisible( true ); 21 } 22 23 // display font metrics 24 public void paint( Graphics g ) 25 { 26 // call superclass’s paint method 27 super.paint( g ); 28 29 g.setFont( new Font( “SansSerif”, Font.BOLD, 12 ) ); 30 FontMetrics metrics = g.getFontMetrics(); 31 g.drawString( “Current font: ” + g.getFont(), 10, 40 ); 32 g.drawString( “Ascent: ” + metrics.getAscent(), 10, 55 ); 33 g.drawString( “Descent: ” + metrics.getDescent(), 10, 70 ); 34 g.drawString( “Height: ” + metrics.getHeight(), 10, 85 ); 35 g.drawString( “Leading: ” + metrics.getLeading(), 10, 100); 36 37 Font font = new Font( “Serif”, Font.ITALIC, 14 ); 38 metrics = g.getFontMetrics( font ); 39 g.setFont( font ); 40 g.drawString( “Current font: ” + font, 10, 130 ); 41 g.drawString( “Ascent: ” + metrics.getAscent(), 10, 145 ); 42 g.drawString( “Descent: ” + metrics.getDescent(), 10, 160); 43 g.drawString( “Height: ” + metrics.getHeight(), 10, 175 ); 44 g.drawString( “Leading: ” + metrics.getLeading(), 10, 190); 45 } 46 47 // execute application 48 public static void main( String args[] ) 49 { 50 Metrics application = new Metrics(); Fig. 11.12 Fig. 11.12 Obtaining font metric information (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply