1  /*
  2   *  File:  FontList.java
  3   *
  4   *  Create a scrolling list of fonts
  5   *
  6   *  Copyright:  Northeast Parallel Architectures Center
  7   *  
  8   */
  9  
 10  import java.awt.*;
 11  import java.awt.event.*;
 12  
 13  public class FontList extends java.applet.Applet
 14                        implements ItemListener, ActionListener {
 15  
 16    // Allow five visible items in the list, but 
 17    // disallow multiple selections:
 18    private List fontList = new List( 5, false );
 19    
 20    private Checkbox boldCheckbox, italicCheckbox;
 21    
 22    private String fontString1, fontString2;
 23    private Label fontLabel1, fontLabel2;
 24    private Font font = new Font( "Monospaced", Font.PLAIN, 18 );
 25  
 26    public void init() {
 27      setBackground( Color.white );
 28  
 29      // Color the scrolling list:
 30      Color lightBlue = new Color( 0xB0, 0xE0, 0xE6 );
 31      Color  navyBlue = new Color( 0x19, 0x19, 0x70 );
 32      fontList.setBackground( lightBlue );
 33      fontList.setForeground(  navyBlue );
 34      fontList.setFont( new Font( "SansSerif", Font.BOLD, 18 ) );
 35      
 36      // Get list of system fonts:
 37      Toolkit toolkit = Toolkit.getDefaultToolkit();
 38      String fonts[] = toolkit.getFontList();
 39        
 40      // Add fonts to the scrolling list:
 41      for ( int i = 0; i < fonts.length; i++ ) {
 42        fontList.addItem( fonts[i] );
 43      }
 44      
 45      // Construct a text label:
 46      fontString1 = "How razorback-jumping frogs";
 47      fontString2 = "can level six piqued gymnasts!";
 48      fontLabel1 = new Label( fontString1, Label.CENTER );
 49      fontLabel2 = new Label( fontString2, Label.CENTER );
 50      fontLabel1.setFont( font );
 51      fontLabel2.setFont( font );
 52      
 53      // Define checkboxes:
 54      boldCheckbox = new Checkbox( "Bold" );
 55      italicCheckbox = new Checkbox( "Italic" );
 56      
 57      // Add the components to separate panels:
 58      Panel listPanel = new Panel();
 59      listPanel.add( fontList );
 60      Panel labelPanel = new Panel();
 61      labelPanel.setLayout( new GridLayout( 2, 1, 5, 5 ) );
 62      labelPanel.add( fontLabel1 );
 63      labelPanel.add( fontLabel2 );
 64      Panel boxPanel = new Panel();
 65      boxPanel.add( boldCheckbox );
 66      boxPanel.add( italicCheckbox );
 67      
 68      // Set layout to BorderLayout and add the panels:
 69      setLayout( new BorderLayout( 5, 5 ) );
 70      add( boxPanel, BorderLayout.NORTH );
 71      add( listPanel, BorderLayout.CENTER );
 72      add( labelPanel, BorderLayout.SOUTH );
 73      
 74      // Determine default font:
 75      String defaultFontName = font.getName();
 76      
 77      // Select default font in scrolling list:
 78      for ( int i = 0; i < fonts.length; i++ ) {
 79        if ( defaultFontName.equals( fonts[i] ) ) {
 80          fontList.select(i); break;
 81        }
 82      }
 83      
 84      // Register the applet to listen for events:
 85      fontList.addItemListener( this );
 86      fontList.addActionListener( this );
 87      boldCheckbox.addItemListener( this );
 88      italicCheckbox.addItemListener( this );
 89    }
 90    
 91    public void itemStateChanged( ItemEvent event ) {
 92      String fontName = fontList.getSelectedItem();
 93      int style = Font.PLAIN;
 94      if ( boldCheckbox.getState() ) style += Font.BOLD;
 95      if ( italicCheckbox.getState() ) style += Font.ITALIC;
 96      int size = font.getSize();
 97      fontLabel1.setFont( new Font( fontName, style, size ) );
 98      fontLabel2.setFont( new Font( fontName, style, size ) );
 99      fontLabel1.setText( fontString1 );
100      fontLabel2.setText( fontString2 );
101      showStatus( "" );
102    }
103    
104    public void actionPerformed( ActionEvent event ) {
105      String fontName = fontList.getSelectedItem();
106      showStatus( "You chose " + fontName + "!" );
107    }
108    
109  }