import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/***********************************************************************
   FontMenu is a subclass of the Scrib Applet that adds a menu for
   selecting font.
************************************************************************/

public class FontMenu extends Scrib {
  /* Inner class FontEntry.  Represents info we need for one font */
  class FontEntry {
    String name;  // name to be displayed in menu
    Font font;    // font value for menu selection
    
    FontEntry(String s, Font c) {
      name = s;  
      font = c;
    }
  }  

  /* State variables */
  JTextField userInput;  // for input of text
  int textX, textY;    // position to draw next text

  JComboBox<String> fontChoices;
  FontEntry[] fontArray = 
  { new FontEntry("default", new Font("SansSerif", Font.PLAIN, 14)), 
    new FontEntry("bold", new Font("SansSerif", Font.BOLD, 14)), 
    new FontEntry("large", new Font("SansSerif", Font.PLAIN, 24)), 
    new FontEntry("typewriter", new Font("Monospaced", Font.PLAIN, 14)), 
    new FontEntry("serif", new Font("Serif", Font.PLAIN, 14)), 
    new FontEntry("bold italic", new Font("SansSerif", Font.BOLD|Font.ITALIC, 14)) };
  Font currentFont;  // current drawing font

  /* Methods */
  public void init() {
    super.init();
    this.addMouseListener(new MouseClickAdapter());

    textX = textY = 50;  // text draw position before first click
    userInput = new JTextField(11);
    userInput.addActionListener(new InputAdapter());
    this.getContentPane().add(userInput);  // add TextField to this applet's Image

    fontChoices = new JComboBox<String>();
    fontChoices.addItemListener(new FontMenuAdapter());
    fontChoices.setForeground(Color.black);
    fontChoices.setBackground(Color.lightGray);
    for (int i = 0;  i < fontArray.length;  i++)
      fontChoices.addItem(fontArray[i].name);
    this.getContentPane().add(new JLabel(" Font:"));
    this.getContentPane().add(fontChoices);

    currentFont = fontArray[0].font;
  }


  class MouseClickAdapter extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
      textX = e.getX();
      textY = e.getY();
    }
  }


  class InputAdapter implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      Graphics g = getGraphics();  // get a Graphics for Scrib
      g.setColor(getForeground());
      g.setFont(currentFont);
      g.drawString(userInput.getText(), textX, textY);
      userInput.setText("");
    }
  }
  

  class FontMenuAdapter implements ItemListener {
    public void itemStateChanged(ItemEvent e) { 
      currentFont=fontArray[fontChoices.getSelectedIndex()].font;
    }   
  }  
}