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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
package tesseract;
import java.awt.GraphicsConfiguration;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.media.j3d.BoundingBox;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import tesseract.menuitems.EllipsoidMenuItem;
import tesseract.menuitems.IcosahedronMenuItem;
import tesseract.menuitems.ParticleEmitterMenuItem;
import tesseract.menuitems.ParticleMenuItem;
import tesseract.menuitems.PlanarPolygonMenuItem;
import com.sun.j3d.utils.universe.SimpleUniverse;
/**
* This class is the main UI for the Tesseract Project.
*
* @author Jesse Morgan
*/
public class TesseractUI extends JFrame {
/**
* Generated serialVersionUID.
*/
private static final long serialVersionUID = 4097744746899308736L;
/**
* Update Rate.
*/
private static final int UPDATE_RATE = 30;
/**
* Measure of 1 unite of space in the world.
*/
private static final float UNIT = 1;
/**
* Number of miliseconds in 1 second.
*/
private static final int MILISECONDS_IN_SECOND = 1000;
/**
* A reference to the world.
*/
private World myWorld;
/**
* The Canvas.
*/
private Canvas3D myCanvas;
/**
* Camera TransformGroup.
*/
private TransformGroup cameraTG;
/**
* Camera position information.
*/
private double cameraXRotation, cameraYRotation, cameraDistance;
/**
* Object Menu Items.
*/
private JMenuItem[] myObjectMenuItems;
/**
* World Timer.
*/
private Timer myTimer;
/**
* UI Constructor.
*/
public TesseractUI() {
super("Tesseract Project");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWorld = new World(
new BoundingBox(new Point3d(-UNIT / 2, -UNIT / 2, -UNIT / 2),
new Point3d(UNIT / 2, UNIT / 2, UNIT / 2)));
myObjectMenuItems = new JMenuItem[] {
new ParticleEmitterMenuItem(myWorld),
new ParticleMenuItem(myWorld),
new PlanarPolygonMenuItem(myWorld),
new EllipsoidMenuItem(myWorld),
new IcosahedronMenuItem(myWorld)
};
createMenu();
setupCanvas();
pack();
// Maximize the windows
if (Toolkit.getDefaultToolkit().
isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
// THIS IS WHERE OBJECTS ARE FORCED INTO EXISTANCE
// TODO: REMOVE TEST CODE
//myWorld.addObject(new Particle(new Vector3f(0, 0, 0), null));
//myWorld.addForce(new Gravity());
//myWorld.addObject(new ParticleEmitter(new Vector3f(0, 0.49f, 0), 0.5f, null));
//myWorld.addObject(new PlanarPolygon(new Vector3f(0, 0.49f, 0), 0.25f));
//myWorld.addObject(new Icosahedron(new Vector3f(), 1, 0.00001f));
}
/**
* Create the menu.
*/
private void createMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu simulationMenu = new JMenu("Simulation");
// Added by Steve: Fixes viewing menu problem with Canvas3D on both my windows machines
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
menuBar.add(simulationMenu);
JMenu objectsMenu = new JMenu("Add Object");
for (JMenuItem item : myObjectMenuItems) {
objectsMenu.add(item);
}
menuBar.add(objectsMenu);
/*
JCheckBoxMenuItem cMenuItem = new JCheckBoxMenuItem("Enable Particle Emitters", enableEmitters);
cMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
enableEmitters = !enableEmitters;
}
});
menu.add(cMenuItem);
for (int i = 0; i < forces.length; i++) {
cMenuItem = new JCheckBoxMenuItem(forces[i].toString(), activeForces[i]);
cMenuItem.setActionCommand(i + "");
cMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int index = Integer.parseInt(e.getActionCommand());
activeForces[index] = !activeForces[index];
}
});
menu.add(cMenuItem);
}
*/
// Simulator Start/Stop
JMenuItem runSim = new JCheckBoxMenuItem("Run Simulator", true);
runSim.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
myTimer.start();
} else {
myTimer.stop();
}
}
});
simulationMenu.add(runSim);
// Exit Menu Item
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
// TODO: I feel this is the wrong way of exiting...
System.exit(0);
}
});
simulationMenu.add(exit);
setJMenuBar(menuBar);
}
/**
* Create and show the UI.
*/
private void setupCanvas() {
GraphicsConfiguration config
= SimpleUniverse.getPreferredConfiguration();
myCanvas = new Canvas3D(config);
SimpleUniverse universe = new SimpleUniverse(myCanvas);
universe.getViewer().getView().setSceneAntialiasingEnable(true);
// Set the camera
cameraTG = universe.getViewingPlatform().getViewPlatformTransform();
cameraDistance = 3 * UNIT;
updateCamera();
// Add the scene BG.
universe.addBranchGraph(myWorld.getScene());
// Setup the Mouse Behaviors
myWorld.setupMouseBehaviors(myCanvas);
// Add the canvas to the frame.
add(myCanvas);
// Event listener time
myCanvas.addMouseMotionListener(new MouseMotionAdapter() {
private MouseEvent lastDragEvent = null;
public void mouseDragged(final MouseEvent e) {
if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0
|| (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0) {
return;
}
if (lastDragEvent != null) {
cameraXRotation +=
Math.toRadians(e.getY() - lastDragEvent.getY()) / 3;
if (cameraXRotation > Math.PI / 2) {
cameraXRotation = Math.PI / 2;
} else if (cameraXRotation < -Math.PI / 2) {
cameraXRotation = -Math.PI / 2;
}
cameraYRotation +=
Math.toRadians(e.getX() - lastDragEvent.getX()) / 3;
updateCamera();
}
lastDragEvent = e;
}
public void mouseMoved(final MouseEvent e) {
lastDragEvent = null;
}
});
myCanvas.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(final MouseWheelEvent e) {
if (e.getWheelRotation() > 0) {
cameraDistance *= 1.05;
} else if (e.getWheelRotation() < 0) {
cameraDistance *= 0.95;
}
updateCamera();
}
});
// Setup the timer.
myTimer = new Timer(MILISECONDS_IN_SECOND / UPDATE_RATE,
new ActionListener() {
public void actionPerformed(final ActionEvent e) {
myCanvas.stopRenderer();
myWorld.tick();
myCanvas.startRenderer();
}
});
myTimer.start();
}
/**
* Method to update the camera.
*/
private void updateCamera() {
Transform3D camera3D = new Transform3D();
camera3D.setTranslation(new Vector3f(0f, 0f, (float) -cameraDistance));
Transform3D tmp = new Transform3D();
tmp.rotX(cameraXRotation);
camera3D.mul(tmp);
tmp.rotY(cameraYRotation);
camera3D.mul(tmp);
camera3D.invert();
cameraTG.setTransform(camera3D);
}
/**
* Start up the program.
*
* @param args Unused commandline arguments.
*/
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TesseractUI().setVisible(true);
}
});
}
}
|