Source of FontJPanel.java


  1: // Fig. 12.11: FontJPanel.java
  2: // Display strings in different fonts and colors.
  3: import java.awt.Font;
  4: import java.awt.Color;
  5: import java.awt.Graphics;
  6: import javax.swing.JPanel;
  7: 
  8: public class FontJPanel extends JPanel
  9: {
 10:    // display Strings in different fonts and colors
 11:    public void paintComponent( Graphics g )
 12:    {
 13:       super.paintComponent( g ); // call superclass's paintConponent
 14: 
 15:       // set font to Serif (Times), bold, 12pt and draw a string 
 16:       g.setFont( new Font( "Serif", Font.BOLD, 12 ) );
 17:       g.drawString( "Serif 12 point bold.", 20, 50 );
 18: 
 19:       // set font to Monospaced (Courier), italic, 24pt and draw a string 
 20:       g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) );
 21:       g.drawString( "Monospaced 24 point italic.", 20, 70 );
 22: 
 23:       // set font to SansSerif (Helvetica), plain, 14pt and draw a string 
 24:       g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) );
 25:       g.drawString( "SansSerif 14 point plain.", 20, 90 );
 26: 
 27:       // set font to Serif (Times), bold/italic, 18pt and draw a string
 28:       g.setColor( Color.RED );
 29:       g.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) );
 30:       g.drawString( g.getFont().getName() + " " + g.getFont().getSize() +
 31:          " point bold italic.", 20, 110 );
 32:    } // end method paintComponent
 33: } // end class FontJPanel
 34: 
 35: /**************************************************************************
 36:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 37:  * Pearson Education, Inc. All Rights Reserved.                           *
 38:  *                                                                        *
 39:  * DISCLAIMER: The authors and publisher of this book have used their     *
 40:  * best efforts in preparing the book. These efforts include the          *
 41:  * development, research, and testing of the theories and programs        *
 42:  * to determine their effectiveness. The authors and publisher make       *
 43:  * no warranty of any kind, expressed or implied, with regard to these    *
 44:  * programs or to the documentation contained in these books. The authors *
 45:  * and publisher shall not be liable in any event for incidental or       *
 46:  * consequential damages in connection with, or arising out of, the       *
 47:  * furnishing, performance, or use of these programs.                     *
 48:  *************************************************************************/