Chapter 11 Graphics and Java2D 615 (X web hosting) 33 //
Chapter 11 Graphics and Java2D 615 33 // set current font to Monospaced (Courier), 34 // italic, 24pt and draw a string 35 g.setFont( new Font( “Monospaced”, Font.ITALIC, 24 ) ); 36 g.drawString( “Monospaced 24 point italic.”, 20, 70 ); 37 38 // set current font to SansSerif (Helvetica), 39 // plain, 14pt and draw a string 40 g.setFont( new Font( “SansSerif”, Font.PLAIN, 14 ) ); 41 g.drawString( “SansSerif 14 point plain.”, 20, 90 ); 42 43 // set current font to Serif (times), bold/italic, 44 // 18pt and draw a string 45 g.setColor( Color.red ); 46 g.setFont( 47 new Font( “Serif”, Font.BOLD + Font.ITALIC, 18 ) ); 48 g.drawString( g.getFont().getName() + ” ” + 49 g.getFont().getSize() + 50 ” point bold italic.”, 20, 110 ); 51 } 52 53 // execute application 54 public static void main( String args[] ) 55 { 56 Fonts application = new Fonts(); 57 58 application.setDefaultCloseOperation( 59 JFrame.EXIT_ON_CLOSE ); 60 } 61 62 } // end class Fonts Fig. 11.9 Using Graphicsmethod setFontto change Fonts (part 2 of 2). Software Engineering Observation 11.3 To change the font, you must create a new Font object; there are no set methods in class Fontto change the characteristics of the current font. Often, it is necessary to get information about the current font, such as the font name, the font style and the font size. Several Fontmethods used to get font information are summarized in Fig. 11.8. Method getStylereturns an integer value representing the current style. The integer value returned is either Font.PLAIN, Font.ITALIC, Font.BOLD or any combination of Font.PLAIN, Font.ITALICand Font.BOLD. Method getSize returns the font size in points. Method getName returns the current font name as a String. Method getFamilyreturns the name of the font family to which the current font belongs. The name of the font family is platform specific. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01