MainMenu.java
6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import javax.swing.KeyStroke;
import java.util.Map;
import java.util.HashMap;
/**
* The MainMenu class will serve as the menu bar for JPaint.
* It will contain all basic functions, i.e. new file, etc. There are keyboard
* shorts cuts to speed up the process.
**/
public class MainMenu extends JMenuBar
{
private Map JMenuItemsMap;
private JMenu menuFile, menuEdit, menuView, menuHelp;
private JMenuItem fileNew, fileOpen, fileClose, fileSave, fileSaveAs,
filePrint, fileExit, editCut, editCopy, editPaste, editDelete, viewToolBox,
viewColorBox, imageClear, imageRotate, helpAbout;
/**
* Creates a default constructor for the MainMenu. Instantiates the menus
* and the menu items and the keys. Creates a maps to make accessors easier.
**/
public MainMenu()
{
// instantiations for menus, menuitems and hotkeys
initJMenus();
initJMenuItems();
initHotKeys();
// creates a hashmap and then adds the items to the hashmap
JMenuItemsMap = new HashMap(19);
addJMenuItemsToMap();
// adds everything onto the screen
addAll();
}
/** Adds the menubar onto the screen. **/
public void addAll()
{
add(menuFile);
menuFile.add(fileNew);
menuFile.add(fileOpen);
menuFile.add(fileClose);
menuFile.addSeparator();
menuFile.add(fileSave);
menuFile.add(fileSaveAs);
menuFile.addSeparator();
menuFile.add(filePrint);
menuFile.addSeparator();
menuFile.add(fileExit);
add(menuEdit);
menuEdit.add(editCut);
menuEdit.add(editCopy);
menuEdit.add(editPaste);
menuEdit.addSeparator();
menuEdit.add(editDelete);
menuEdit.addSeparator();
menuEdit.add(imageRotate);
menuEdit.addSeparator();
menuEdit.add(imageClear);
add(menuView);
menuView.add(viewToolBox);
menuView.add(viewColorBox);
add(menuHelp);
menuHelp.add(helpAbout);
}
/** Adds actionlisteners to each of the menu items. **/
public void addActionListener(ActionListener listener)
{
fileNew.addActionListener(listener);
fileOpen.addActionListener(listener);
fileClose.addActionListener(listener);
fileSave.addActionListener(listener);
fileSaveAs.addActionListener(listener);
filePrint.addActionListener(listener);
fileExit.addActionListener(listener);
editCut.addActionListener(listener);
editCopy.addActionListener(listener);
editPaste.addActionListener(listener);
editDelete.addActionListener(listener);
viewToolBox.addActionListener(listener);
viewColorBox.addActionListener(listener);
imageRotate.addActionListener(listener);
imageClear.addActionListener(listener);
helpAbout.addActionListener(listener);
}
/** This method instantiates all the menus. **/
public void initJMenus()
{
menuFile = new JMenu("File");
menuEdit = new JMenu("Edit");
menuView = new JMenu("View");
menuHelp = new JMenu("About");
}
/** This method instantiates all the jmenu items. **/
public void initJMenuItems()
{
fileNew = new JMenuItem("New");
fileOpen = new JMenuItem("Open");
fileClose = new JMenuItem("Close");
fileSave = new JMenuItem("Save");
fileSaveAs = new JMenuItem("Save As");
filePrint = new JMenuItem("Print");
fileExit = new JMenuItem("Exit");
editCut = new JMenuItem("Cut");
editCopy = new JMenuItem("Copy");
editPaste = new JMenuItem("Paste");
editDelete = new JMenuItem("Delete");
viewToolBox = new JMenuItem("ToolBox");
viewColorBox = new JMenuItem("ColorBox");
imageClear = new JMenuItem("Clear Image");
imageRotate = new JMenuItem("Rotate");
helpAbout = new JMenuItem("About JPaint");
}
/** This method sets all the hot keys for the menu items. **/
public void initHotKeys()
{
fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
fileSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F12, ActionEvent.CTRL_MASK));
filePrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK));
fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
viewToolBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
viewColorBox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
editCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
editCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
editPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
editDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));
imageRotate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
imageClear.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK));
helpAbout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK));
}
/** This method adds the menu items to the map. **/
public void addJMenuItemsToMap()
{
JMenuItemsMap.put("fileNew", fileNew);
JMenuItemsMap.put("fileOpen", fileOpen);
JMenuItemsMap.put("fileClose", fileClose);
JMenuItemsMap.put("fileSave", fileSave);
JMenuItemsMap.put("fileSaveAs", fileSaveAs);
JMenuItemsMap.put("filePrint", filePrint);
JMenuItemsMap.put("fileExit", fileExit);
JMenuItemsMap.put("editCut", editCut);
JMenuItemsMap.put("editCopy", editCopy);
JMenuItemsMap.put("editPaste", editPaste);
JMenuItemsMap.put("editDelete", editDelete);
JMenuItemsMap.put("viewToolBox", viewToolBox);
JMenuItemsMap.put("viewColorBox", viewColorBox);
JMenuItemsMap.put("imageRotate", imageRotate);
JMenuItemsMap.put("imageClear", imageClear);
JMenuItemsMap.put("helpAbout", helpAbout);
}
/** Returns the JMenuItem. **/
public JMenuItem getJMenuItem(String str)
{
return (JMenuItem)JMenuItemsMap.get(str);
}
}