|
@@ -28,6 +28,8 @@ import javax.vecmath.*;
|
28
|
28
|
import com.sun.j3d.utils.behaviors.mouse.*;
|
29
|
29
|
import com.sun.j3d.utils.image.TextureLoader;
|
30
|
30
|
import java.awt.Toolkit;
|
|
31
|
+import com.sun.j3d.utils.picking.*;
|
|
32
|
+import java.awt.event.*;
|
31
|
33
|
|
32
|
34
|
/**
|
33
|
35
|
* This class is responsible for displaying the 3D View of our Cube.
|
|
@@ -38,14 +40,16 @@ import java.awt.Toolkit;
|
38
|
40
|
* @version 1.0
|
39
|
41
|
*/
|
40
|
42
|
|
41
|
|
-public class Led3D {
|
|
43
|
+public class Led3D extends MouseAdapter {
|
42
|
44
|
private Canvas3D canvas = null;
|
|
45
|
+ private PickCanvas pickCanvas = null;
|
43
|
46
|
private SimpleUniverse universe = null;
|
44
|
47
|
private BranchGroup group = null;
|
45
|
48
|
private Transform3D trans3D = null;
|
46
|
49
|
private TransformGroup transGroup = null;
|
47
|
50
|
private Matrix4d mat = null;
|
48
|
51
|
private Matrix4d fullScreenMat = null;
|
|
52
|
+ private Frame parentFrame = null;
|
49
|
53
|
|
50
|
54
|
private Sphere[][][] leds = new Sphere[8][8][8];
|
51
|
55
|
private static ColoringAttributes redColor = new ColoringAttributes(
|
|
@@ -63,9 +67,10 @@ public class Led3D {
|
63
|
67
|
/**
|
64
|
68
|
* @param canv The Canvas3D we render our cube in
|
65
|
69
|
*/
|
66
|
|
- public Led3D(Canvas3D canv) {
|
|
70
|
+ public Led3D(Canvas3D canv, Frame f) {
|
67
|
71
|
|
68
|
72
|
canvas = canv;
|
|
73
|
+ parentFrame = f;
|
69
|
74
|
group = new BranchGroup();
|
70
|
75
|
// Position viewer, so we are looking at object
|
71
|
76
|
trans3D = new Transform3D();
|
|
@@ -87,6 +92,8 @@ public class Led3D {
|
87
|
92
|
transGroup = new TransformGroup(trans3D);
|
88
|
93
|
transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
|
89
|
94
|
transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
|
|
95
|
+ transGroup.setCapability(Node.ALLOW_BOUNDS_READ);
|
|
96
|
+ transGroup.setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
|
90
|
97
|
ViewingPlatform viewingPlatform = new ViewingPlatform();
|
91
|
98
|
Viewer viewer = new Viewer(canvas);
|
92
|
99
|
universe = new SimpleUniverse(viewingPlatform, viewer);
|
|
@@ -112,6 +119,8 @@ public class Led3D {
|
112
|
119
|
group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
|
113
|
120
|
group.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
|
114
|
121
|
group.setCapability(BranchGroup.ALLOW_DETACH);
|
|
122
|
+ group.setCapability(Node.ALLOW_BOUNDS_READ);
|
|
123
|
+ group.setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
|
115
|
124
|
background = createBackground();
|
116
|
125
|
group.addChild(background);
|
117
|
126
|
|
|
@@ -131,14 +140,14 @@ public class Led3D {
|
131
|
140
|
Vector3f vector = new Vector3f(x, y, z);
|
132
|
141
|
transform.setTranslation(vector);
|
133
|
142
|
tg.setTransform(transform);
|
|
143
|
+ tg.setCapability(Node.ALLOW_BOUNDS_READ);
|
|
144
|
+ tg.setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
|
134
|
145
|
tg.addChild(leds[x][y][z]);
|
135
|
146
|
transGroup.addChild(tg);
|
136
|
147
|
|
137
|
|
- drawLedFeetVertical((double) x, y + 0.5, (double) z, 0.9f,
|
138
|
|
- 0.01f);
|
|
148
|
+ drawLedFeetVertical((double) x, y + 0.5, (double) z, 0.9f, 0.01f);
|
139
|
149
|
if (x < 7)
|
140
|
|
- drawLedFeetHorizontal(x + 0.5, (double) y, (double) z,
|
141
|
|
- 0.9f, 0.01f, 0);
|
|
150
|
+ drawLedFeetHorizontal(x + 0.5, (double) y, (double) z, 0.9f, 0.01f, 0);
|
142
|
151
|
}
|
143
|
152
|
}
|
144
|
153
|
// 8 times, use x as y
|
|
@@ -157,6 +166,50 @@ public class Led3D {
|
157
|
166
|
transGroup.addChild(light2);
|
158
|
167
|
group.addChild(transGroup);
|
159
|
168
|
universe.addBranchGraph(group); // Add group to universe
|
|
169
|
+
|
|
170
|
+ // Mouse-Selectable objects
|
|
171
|
+ pickCanvas = new PickCanvas(canvas, group);
|
|
172
|
+ pickCanvas.setMode(PickCanvas.BOUNDS);
|
|
173
|
+ canvas.addMouseListener(this);
|
|
174
|
+ }
|
|
175
|
+
|
|
176
|
+ /**
|
|
177
|
+ * Listen for mouse events so the user can click on LEDs.
|
|
178
|
+ * @param e MouseEvent generated by the user
|
|
179
|
+ */
|
|
180
|
+ public void mouseClicked(MouseEvent e) {
|
|
181
|
+ pickCanvas.setShapeLocation(e);
|
|
182
|
+ PickResult result = pickCanvas.pickClosest();
|
|
183
|
+ if (result != null) {
|
|
184
|
+ // User clicked near something
|
|
185
|
+ Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
|
|
186
|
+ Shape3D s = (Shape3D)result.getNode(PickResult.SHAPE3D);
|
|
187
|
+ Cylinder c = null;
|
|
188
|
+ if (p != null) {
|
|
189
|
+ if (p.getClass().getName().equals("com.sun.j3d.utils.geometry.Cylinder")) {
|
|
190
|
+ c = (Cylinder)p;
|
|
191
|
+ }
|
|
192
|
+ } else if (s != null) {
|
|
193
|
+ if (s.getClass().getName().equals("com.sun.j3d.utils.geometry.Cylinder")) {
|
|
194
|
+ c = (Cylinder)p;
|
|
195
|
+ }
|
|
196
|
+ }
|
|
197
|
+ if (c != null) {
|
|
198
|
+/* ---> */ Bounds b = c.getBounds();
|
|
199
|
+ // CapabilityNotSetException: no capability to read user bounds
|
|
200
|
+
|
|
201
|
+ for (int x = 0; x < 8; x++) {
|
|
202
|
+ for (int y = 0; y < 8; y++) {
|
|
203
|
+ for (int z = 0; z < 8; z++) {
|
|
204
|
+ if (b.equals(leds[x][y][z].getBounds())) {
|
|
205
|
+ // Clicked led found!
|
|
206
|
+ parentFrame.toggleLED(x, y, z);
|
|
207
|
+ }
|
|
208
|
+ }
|
|
209
|
+ }
|
|
210
|
+ }
|
|
211
|
+ }
|
|
212
|
+ }
|
160
|
213
|
}
|
161
|
214
|
|
162
|
215
|
private void drawLedFeetVertical(double x, double y, double z,
|