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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
package tesseract.newmenu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.vecmath.Vector3f;
import tesseract.World;
/**
* NewIcosahedronMenutItem
*
* Defines a menu item to add an Icosahedron to the world.
* Code recycled from TesseractMenuItem by Steve Bradshaw and Jessie Morgan
*
* @author Phillip Cardon
*/
public abstract class MenuItem extends JMenuItem implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 4191575308469669044L;
/**
* default position.
*/
protected static final Vector3f DEFAULT_POSITION = new Vector3f(0, 0, 0);
/**
*
*/
protected static final float DEFAULT_MASS = 2f;
/**
* Jframe to hold panel.
*/
private JFrame myFrame;
/**
* Jpanel for variable text boxes.
*/
private JPanel myPanel;
/**
* Variable name/type mapping.
*/
protected Map <String, Object> myParameters;
/**
* Variable/Textfield mapping.
*/
protected Map <String, JTextField> myReadData;
/**
* World to add objects to.
*/
protected World myWorld;
/**
* Color to make object.
*/
protected Color myColor;
/**
* A Parameter setting JFrame.
*/
private JFrame myParamFrame;
/**
* The button that get all inputs for shape.
*/
private JButton myEnterButton;
/**
* The button that get all inputs for shape.
*/
private JButton myColorButton;
/**
* The default button.
*/
private JButton myDefaultButton;
private boolean useColorButton;
/**
* Default constructor.
*/
public MenuItem() { }
/**
* Constructor.
* @param theWorld world parameter.
* @param theLabel for menu item.
*/
public MenuItem(final World theWorld, final String theLabel) {
super(theLabel);
useColorButton = true;
myFrame = new JFrame();
myParameters = new HashMap<String, Object>();
myPanel = new JPanel();
myReadData = new HashMap <String, JTextField>();
myWorld = theWorld;
myParameters.put("Position", DEFAULT_POSITION);
myParameters.put("Mass", new Float(DEFAULT_MASS));
addActionListener(this);
myParamFrame = new JFrame("Parameters");
myParamFrame.setBackground(Color.GRAY);
myParamFrame.setLayout(new BorderLayout());
myColorButton = new JButton("Color");
myColor = Color.RED;
}
/**
* Constructor.
* @param theWorld world parameter.
* @param theLabel for menu item.
* @param theColor use color button.
*/
public MenuItem(final World theWorld, final String theLabel, boolean theColor) {
this(theWorld, theLabel);
useColorButton = theColor;
}
/**
* Create Panel.
*/
protected void makePanel() {
Set<String> varNames = myParameters.keySet();
myPanel.setLayout(new GridLayout(myParameters.size(), 2));
for (String s : varNames) {
myPanel.add(new JLabel(s));
myReadData.put(s, new JTextField());
myPanel.add(myReadData.get(s));
}
myReadData.get("Position").setText(
MenuItem.DEFAULT_POSITION.toString().substring(1,
MenuItem.DEFAULT_POSITION.toString().length() - 1));
myReadData.get("Mass").setText(((Float)
MenuItem.DEFAULT_MASS).toString());
}
/**
*
*/
protected void createParameterMenu() {
myColorButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
myColor = JColorChooser.showDialog(null, "Particle Color",
Color.RED);
}});
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
myParamFrame.setSize(screenWidth / 4, screenHeight / 4);
myParamFrame.setLocation(screenWidth / 4, screenHeight / 4);
/*
my_position_input = new JTextField(10);
my_position_input.setText("0,0,0");
my_radius_input = new JTextField(10);
my_radius_input.setText(".1");
my_mass_input = new JTextField(10);
my_mass_input.setText("1");
*/
//JLabel blank = new JLabel("");
//JLabel position_label = new JLabel("Enter Position: ");
//JLabel radius_label = new JLabel("Enter Radius: ");
//JLabel mass_label = new JLabel("Enter Mass: ");
myEnterButton = new JButton("ENTER");
myDefaultButton = new JButton("Default Shape");
if (useColorButton) {
JPanel temp = new JPanel();
temp.setLayout(new GridLayout(1, 2));
temp.add(myColorButton);
temp.add(myEnterButton);
myParamFrame.add(temp, BorderLayout.SOUTH);
} else {
myParamFrame.add(myEnterButton, BorderLayout.SOUTH);
}
myParamFrame.add(myDefaultButton, BorderLayout.NORTH);
myParamFrame.add(myPanel, BorderLayout.CENTER);
myParamFrame.setAlwaysOnTop(true);
myParamFrame.pack();
myParamFrame.setVisible(isVisible());
}
/**
*
* @return Default Button
*/
public JButton getDefaultButton() {
return myDefaultButton;
}
/**
*
* @return enter button
*/
public JButton getEnterButton() {
return myEnterButton;
}
/**
*
* @return parameter frame
*/
public JFrame getParamFrame() {
return myParamFrame;
}
/**
* Utility method to parse a string formatted as x,y,z into a vector3f.
*
* @param input A string to parse.
* @return A vector3f.
* @author Jesse Morgan, Steve Bradshaw
*/
protected Vector3f parseVector(final String input) {
String[] split = input.split(",");
float x = Float.parseFloat(split[0]);
float y = Float.parseFloat(split[1]);
float z = Float.parseFloat(split[2]);
return new Vector3f(x, y, z);
}
/**
*
* @return position from vector.
*/
protected Vector3f getPosition() {
return (Vector3f) myParameters.get("Position");
}
/**
*
* @return mass.
*/
protected float getMass() {
return ((Float) myParameters.get("Mass")).floatValue();
}
}
|