home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch03 / Fonts.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  1.6 KB  |  87 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3.  
  4. /**
  5.  * Class that determines which fonts are available
  6.  */
  7. public class Fonts extends Applet {
  8.  
  9. /*
  10.  * Maximum number of fonts to display
  11.  */
  12. final int MaxFonts = 10;
  13.  
  14. /*
  15.  * Width and height of bounding panel
  16.  */
  17. int width, height;
  18.  
  19. /*
  20.  * Array of font names
  21.  */
  22. String fontName[];
  23.  
  24. /*
  25.  * Array of fonts
  26.  * Holds plain, italic, and bold for each font
  27.  */
  28. Font theFonts[] = new Font[3 * MaxFonts];
  29.  
  30. /*
  31.  * The number of fonts found
  32.  */
  33. int nfonts = 0;
  34.  
  35. /*
  36.  * Applet entry point
  37.  */
  38. public void init () {
  39.  
  40.     int i;
  41.     Dimension d = getSize ();
  42.  
  43.     width = d.width;
  44.     height = d.height;
  45.  
  46.         theFonts[0] = new Font ("Courier", Font.PLAIN, 12);
  47.         theFonts[1] = new Font ("System", Font.BOLD, 16);
  48.         theFonts[2] = new Font ("Helvetica", Font.BOLD, 18); 
  49.     }
  50.  
  51. /*
  52.  * Draw the font names.
  53.  * @param g - destination graphics object
  54.  */
  55. public void paint (Graphics g) {
  56.  
  57.     int i;
  58.  
  59.  
  60.         g.setFont (theFonts[0]);
  61.         g.drawString ("Courier", 10, 30);
  62.         g.setFont (theFonts[1]);
  63.         g.drawString ("System", 70, 70);
  64.         g.setFont (theFonts[2]);
  65.         g.drawString ("Helvetica", 150, 90);
  66.  
  67. }
  68.  
  69. /*
  70.  * Application entry point
  71.  * Creates a window frame and adds the applet inside
  72.  * @param args[] - command-line arguments
  73.  */
  74. public static void main (String args[]) {
  75.  
  76.     Frame f = new Frame ("Fonts");
  77.     Fonts fonts = new Fonts ();
  78.  
  79.     f.setSize (250, 200);
  80.     f.add ("Center", fonts);
  81.     f.show ();
  82.     fonts.init ();
  83. }
  84. }
  85.  
  86.  
  87.