import java.awt.*;
import java.awt.event.*;

/** Illustrates the insertion of menu entries in Frame
 *  menu bars.
 *
 *  Taken from Core Web Programming from 
 *  Prentice Hall and Sun Microsystems Press,
 *  http://www.corewebprogramming.com/.
 *  &copy; 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted. 
 */

public class ColorMenu extends CloseableFrame 
                       implements ActionListener {
                        
  private String[] colorNames =
    { "Black", "White", "Light Gray", "Medium Gray", 
      "Dark Gray" };
  private Color[] colorValues =
    { Color.black, Color.white, Color.lightGray, 
      Color.gray, Color.darkGray };
   
  public ColorMenu() {
    super("ColorMenu");
    MenuBar bar = new MenuBar();
    Menu colorMenu = new Menu("Colors");
    for(int i=0; i<2; i++) {
      colorMenu.add(colorNames[i]);
    }
    Menu grayMenu = new Menu("Gray");
    for(int i=2; i<colorNames.length; i++) {
      grayMenu.add(colorNames[i]);
    }
    colorMenu.add(grayMenu);
    bar.add(colorMenu);
    setMenuBar(bar);
    colorMenu.addActionListener(this);
    grayMenu.addActionListener(this);
    
    setBackground(Color.lightGray);
    setSize(400, 200);
    setVisible(true);
  }

  /** Catch menu events in the containing Frame. */
  
  public void actionPerformed(ActionEvent event) {
      setBackground(colorNamed(event.getActionCommand()));
      repaint();
  }

  private Color colorNamed(String colorName) {
    for(int i=0; i<colorNames.length; i++) {
      if(colorNames[i].equals(colorName)) {
        return(colorValues[i]);
      }
    }
    return(Color.white);
  }  
  
  public static void main(String[] args) {
    new ColorMenu();
  }
    
}