UNB/ CS/ David Bremner/ teaching/ java/ MenuTest.java
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class MenuTest
{  public static void main(String[] args)
   {  MenuFrame frame = new MenuFrame();
      frame.setTitle("MenuTest");
      frame.show();      
   }
}

class MenuFrame extends JFrame
{  public MenuFrame()
   {  final int DEFAULT_FRAME_WIDTH = 300;
      final int DEFAULT_FRAME_HEIGHT = 300;
      setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);

      addWindowListener(new WindowCloser());
      
      // add drawing panel to content pane
      
      panel = new RectanglePanel();
      Container contentPane = getContentPane();
      contentPane.add(panel, "Center");   
      
      // construct menu
      
      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);
      
      JMenu fileMenu = new JMenu("File");
      menuBar.add(fileMenu);

      MenuListener listener = new MenuListener();
      
      newMenuItem = new JMenuItem("New");      
      fileMenu.add(newMenuItem);
      newMenuItem.addActionListener(listener);

      exitMenuItem = new JMenuItem("Exit");      
      fileMenu.add(exitMenuItem);
      exitMenuItem.addActionListener(listener);
      
      JMenu editMenu = new JMenu("Edit");
      menuBar.add(editMenu);
      
      JMenuItem moveMenu = new JMenu("Move");      
      editMenu.add(moveMenu);

      upMenuItem = new JMenuItem("Up");      
      moveMenu.add(upMenuItem);
      upMenuItem.addActionListener(listener);

      downMenuItem = new JMenuItem("Down");      
      moveMenu.add(downMenuItem);
      downMenuItem.addActionListener(listener);

      leftMenuItem = new JMenuItem("Left");      
      moveMenu.add(leftMenuItem);
      leftMenuItem.addActionListener(listener);

      rightMenuItem = new JMenuItem("Right");      
      moveMenu.add(rightMenuItem);
      rightMenuItem.addActionListener(listener);

      randomizeMenuItem = new JMenuItem("Randomize");      
      editMenu.add(randomizeMenuItem);
      randomizeMenuItem.addActionListener(listener);
   }
   
   private JMenuItem exitMenuItem;
   private JMenuItem newMenuItem;
   private JMenuItem upMenuItem;
   private JMenuItem downMenuItem;
   private JMenuItem leftMenuItem;
   private JMenuItem rightMenuItem;
   private JMenuItem randomizeMenuItem;
   private RectanglePanel panel;

   private class MenuListener implements ActionListener
   {  public void actionPerformed(ActionEvent event)
      {  // find the menu that was selected
      
         Object source = event.getSource();
         
         if (source == exitMenuItem) 
            System.exit(0);
         else if (source == newMenuItem) 
            panel.reset();
         else if (source == upMenuItem) 
            panel.moveRectangle(0, -1);
         else if (source == downMenuItem) 
            panel.moveRectangle(0, 1);
         else if (source == leftMenuItem) 
            panel.moveRectangle(-1, 0);
         else if (source == rightMenuItem) 
            panel.moveRectangle(1, 0);
         else if (source == randomizeMenuItem) 
            panel.randomize();
      }
   }

   private class WindowCloser extends WindowAdapter
   {  public void windowClosing(WindowEvent event)
      {  System.exit(0);
      }
   }
}