/*
 *  ====================================================================
 *  DemoFont.java : This applet retrieves and displays platform fonts.
 *  The fonts are displayed in the pull-down list of a combo box to 
 *  allow selection of the font. The combo box is attached to the
 *  control panel. After a user has selected a new parameter, the 
 *  program creates new font faces using these parameters. The font
 *  face is subsequently applied to the text shown in the display panel.
 *
 *  Adapted from : Pantham S., Pure JFC Swing, 1999.
 *  Modified by : Mark Austin                                March, 2001
 *  ====================================================================
 */

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class DemoFont extends JApplet {
    DemoFontDisplayPanel displayPanel;
    JComboBox fontsBox, fontStylesBox, fontSizesBox;

    String[] fontStyleLabels = {"Plain", "Bold", "Italic", "Bold&Italic"};
    int BOLDITALIC = Font.BOLD|Font.ITALIC;
    int[] fontStyles = {Font.PLAIN, Font.BOLD, Font.ITALIC, BOLDITALIC};
    String[] fontSizeLabels = { "8", "9", 
                               "10", "11",
                               "12", "14",
                               "18", "25",
                               "36", "72"};

    public void init() {

        // 1. Get the content pane and assign layout

        Container container = getContentPane();

        // 2. Create a display panel and add it to the applet ....

        displayPanel = new DemoFontDisplayPanel();
        container.add(displayPanel);

        // 3. Create a control panel with titled border

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(1,3));
        TitledBorder border = new TitledBorder(
            "Select a Font, Font Style, and Font Size...");
        controlPanel.setBorder(border);

        // 4. Create combo boxes for font names, styles, and sizes

        fontsBox = new JComboBox(displayPanel.fontFamilyNames);
        fontsBox.setSelectedItem("Arial"); // default selection
        fontsBox.addActionListener(new ComboBoxListener());

        fontStylesBox = new JComboBox(fontStyleLabels);
        fontStylesBox.addActionListener(new ComboBoxListener());

        fontSizesBox = new JComboBox(fontSizeLabels);
        fontSizesBox.setSelectedItem("36");
        fontSizesBox.addActionListener(new ComboBoxListener());

        // 5. Add the combo boxes to the control panel and add the
        //    panel to the main frame.

        controlPanel.add(fontsBox);
        controlPanel.add(fontStylesBox);
        controlPanel.add(fontSizesBox);
        container.add(BorderLayout.SOUTH, controlPanel);
    }

    // 6. Combo box listener to handle font name, style and size
    //    selections in the respective combo boxes.

    class ComboBoxListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JComboBox tempBox = (JComboBox) e.getSource();

            if (tempBox.equals(fontsBox)) {
                displayPanel.fontFamilyName =
                             (String) tempBox.getSelectedItem();
                displayPanel.repaint();
            }
            else if (tempBox.equals(fontStylesBox)) {
                displayPanel.fontStyle =
                             fontStyles[tempBox.getSelectedIndex()];
                displayPanel.repaint();
            }
            else if (tempBox.equals(fontSizesBox)) {
                displayPanel.fontSize =
                             Integer.parseInt((String) tempBox.getSelectedItem());
                displayPanel.repaint();
            }
        }
    }

    // 7. Definition of display panel

    class DemoFontDisplayPanel extends JPanel {
        String displayText;
        Font currentFont;
        String fontFamilyName;
        int fontStyle;
        int fontSize;

        GraphicsEnvironment ge;
        String[] fontFamilyNames;
        
        // 8. Constructor for DemoFont display panel....

        public DemoFontDisplayPanel() {

            // Logo to be displayed on the panel

            displayText = "Java 2D Fonts";
            fontFamilyName = "Arial";
            fontStyle = Font.PLAIN;
            fontSize = 36;
            
            // Retrieve all the font family names from the platform

            System.out.println("Loading Fonts... Please Wait...");
            ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            fontFamilyNames = ge.getAvailableFontFamilyNames();

            setBackground(Color.white);  // For canvas background color
        }

        // 9. The update method...

        public void update(Graphics g) {
            g.clearRect(0, 0, getWidth(), getHeight());
            paintComponent(g);
        }

        // 10. The painting method...

        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            // 11. Create the graphics context object

            Graphics2D g2D = (Graphics2D) g;

            // 12. Prepare the current font and apply it to
            //     the display text.

            currentFont = new Font(fontFamilyName,
                                   fontStyle,
                                   fontSize);
            g2D.setFont(currentFont);
            g2D.drawString(displayText, 25, 100);
        }
    }
}
