previous | start | next

File MenuFrame.java

1 import java.awt.BorderLayout;
2 import java.awt.event.ActionEvent;
3 import java.awt.event.ActionListener;
4 import java.util.Random;
5 import javax.swing.JFrame;
6 import javax.swing.JMenu;
7 import javax.swing.JMenuBar;
8 import javax.swing.JMenuItem;
9
10 /**
11     This frame has a menu with commands to set the position of
12     a rectangle.
13 */
14 class MenuFrame extends JFrame
15
16    /**
17        Constructs the frame.
18     */
19    public MenuFrame()
20    {  
21       generator = new Random();
22
23       // add drawing panel to content pane
24       
25       panel = new RectanglePanel();
26       getContentPane().add(panel, BorderLayout.CENTER);   
27       pack();
28       
29       // construct menu
30       
31       JMenuBar menuBar = new JMenuBar();     
32       setJMenuBar(menuBar);
33
34       menuBar.add(createFileMenu());
35       menuBar.add(createEditMenu());
36    }
37
38    /**
39        Creates the File menu.
40       @return the menu
41     */
42    public JMenu createFileMenu()
43    {
44       JMenu menu = new JMenu("File");
45       menu.add(createFileNewItem());
46       menu.add(createFileExitItem());
47       return menu;
48    }
49
50    /**
51        Creates the Edit menu.
52       @return the menu
53     */
54    public JMenu createEditMenu()
55    {
56       JMenu menu = new JMenu("Edit");
57       menu.add(createMoveMenu());
58       menu.add(createEditRandomizeItem());
59       return menu;
60    }
61
62    /**
63        Creates the Move submenu.
64       @return the menu
65     */
66    public JMenu createMoveMenu()
67    {
68       JMenu menu = new JMenu("Move");
69       menu.add(createMoveItem("Up", 0, -1));
70       menu.add(createMoveItem("Down", 0, 1));
71       menu.add(createMoveItem("Left", -1, 0));
72       menu.add(createMoveItem("Right", 1, 0));
73       return menu;
74    }  
75
76    /**
77        Creates the File->New menu item and sets its action listener.
78       @return the menu item
79     */
80    public JMenuItem createFileNewItem()
81    {
82       JMenuItem item = new JMenuItem("New");      
83       class MenuItemListener implements ActionListener
84       {
85          public void actionPerformed(ActionEvent event)
86          {
87             panel.reset();
88          }
89       }      
90       ActionListener listener = new MenuItemListener();
91       item.addActionListener(listener);
92       return item;
93    }
94    
95    /**
96        Creates the File->Exit menu item and sets its action listener.
97       @return the menu item
98     */
99    public JMenuItem createFileExitItem()
100    {
101       JMenuItem item = new JMenuItem("Exit");      
102       class MenuItemListener implements ActionListener
103       {
104          public void actionPerformed(ActionEvent event)
105          {
106             System.exit(0);
107          }
108       }      
109       ActionListener listener = new MenuItemListener();
110       item.addActionListener(listener);
111       return item;
112    }
113
114    /**
115        Creates a menu item to move the rectangle and sets its 
116        action listener.
117       @param label the menu label
118       @param dx the amount by which to move the rectangle in x-direction
119       @param dy the amount by which to move the rectangle in y-direction
120       @return the menu item
121     */
122    public JMenuItem createMoveItem(String label,
123       final int dx, final int dy)
124    {
125       JMenuItem item = new JMenuItem(label);      
126       class MenuItemListener implements ActionListener
127       {
128          public void actionPerformed(ActionEvent event)
129          {
130             panel.moveRectangle(dx, dy);
131          }
132       }      
133       ActionListener listener = new MenuItemListener();
134       item.addActionListener(listener);
135       return item;
136    }
137
138    /**
139        Creates the Edit->Randomize menu item and sets its action listener.
140       @return the menu item
141     */
142    public JMenuItem createEditRandomizeItem()
143    {
144       JMenuItem item = new JMenuItem("Randomize");      
145       class MenuItemListener implements ActionListener
146       {
147          public void actionPerformed(ActionEvent event)
148          {
149             int width = panel.getWidth();
150             int height = panel.getHeight();
151             int dx = -1 + generator.nextInt(2);
152             int dy = -1 + generator.nextInt(2);
153             panel.moveRectangle(dx, dy);
154          }
155       }      
156       ActionListener listener = new MenuItemListener();
157       item.addActionListener(listener);
158       return item;
159    }
160
161    private RectanglePanel panel;
162    private Random generator;
163 }
164
165
166
167


previous | start | next