浏览代码

LEDs nearly selectable :)

All the code that we need to select a LED with the mouse is there…
Unfortunately, we have a "CapabilityNotSetException" and i don't know
where to set that capability… See Led3D.java Line 198!
Thomas Buck 12 年前
父节点
当前提交
e26222c8c4
共有 3 个文件被更改,包括 90 次插入48 次删除
  1. 17
    1
      CubeControl/AFrame.java
  2. 14
    41
      CubeControl/Frame.java
  3. 59
    6
      CubeControl/Led3D.java

+ 17
- 1
CubeControl/AFrame.java 查看文件

@@ -33,12 +33,28 @@ import java.util.Arrays;
33 33
  */
34 34
 
35 35
 public class AFrame {
36
-	private short[] data = new short[64];
36
+	private short[] data = new short[64]; // data[y + (8 * z)] & (1 << x)
37 37
 	private short duration = 1;
38 38
 	private String name = "Frame";
39 39
 	private static int lastIndex = 1;
40 40
 
41 41
 	/**
42
+	 * Toggle a LED in this frame
43
+	 * @param x X Coordinate (0 - 7)
44
+	 * @param y Y Coord. (0 - 7)
45
+	 * @param z Z Coord. (0 - 7)
46
+	 */
47
+	public void toggleLED(int x, int y, int z) {
48
+		if (x < 8) {
49
+			if (y < 8) {
50
+				if (z < 8) {
51
+					data[y + (8 * z)] ^= (1 << x);
52
+				}
53
+			}
54
+		}
55
+	}
56
+
57
+	/**
42 58
 	 * Give it a nice name.
43 59
 	 */
44 60
 	public AFrame() {

+ 14
- 41
CubeControl/Frame.java 查看文件

@@ -159,6 +159,19 @@ public class Frame extends JFrame implements ListSelectionListener {
159 159
 	public static void errorMessageStat(String title, String msg) {
160 160
 		recentFrame.errorMessage(title, msg);
161 161
 	}
162
+
163
+	/**
164
+	 * Toggle a LED in the current selected anim & frame
165
+	 * @param x X Coordinate
166
+	 * @param y Y Coord.
167
+	 * @param z Z Coord.
168
+	 */
169
+	public void toggleLED(int x, int y, int z) {
170
+		if ((animList.getSelectedIndex() != -1) && (frameList.getSelectedIndex() != -1)) {
171
+			worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).toggleLED(x, y, z);
172
+			ledView.setData(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getData());
173
+		}
174
+	}
162 175
 	
163 176
 	/**
164 177
 	 * ListSelectionListener that updates Anim & Frame List
@@ -272,25 +285,8 @@ public class Frame extends JFrame implements ListSelectionListener {
272 285
 		// --------------------
273 286
 		gConfig = SimpleUniverse.getPreferredConfiguration();
274 287
 		cubeCanvas = new Canvas3D(gConfig);
275
-		ledView = new Led3D(cubeCanvas);
276 288
 		cubeCanvas.setBounds(18, 31, 275, 275); // 3d view
277
-		cubeCanvas.addMouseListener(new MouseListener() { // React to clicks in 3d view
278
-			public void mouseClicked(MouseEvent e) {
279
-				Point2d mousePos = convertMousePositionToWorld(e.getX(), e.getY()); 
280
-			}
281
-			public void mouseExited(MouseEvent e) {
282
-
283
-			}
284
-			public void mouseEntered(MouseEvent e) {
285
-
286
-			}
287
-			public void mouseReleased(MouseEvent e) {
288
-
289
-			}
290
-			public void mousePressed(MouseEvent e) {
291
-
292
-			}
293
-		});
289
+		ledView = new Led3D(cubeCanvas, this);
294 290
 		cp.add(cubeCanvas);
295 291
 		// --------------------
296 292
 
@@ -928,27 +924,4 @@ public class Frame extends JFrame implements ListSelectionListener {
928 924
 		}
929 925
 		return s;
930 926
 	}
931
-	
932
-	private Point2d convertMousePositionToWorld (int iX, int iY)
933
-	{
934
-		Point3d eye_pos = new Point3d();
935
-		Point3d mousePosn = new Point3d();
936
-	
937
-		//get the eye point and mouse click point
938
-		this.cubeCanvas.getCenterEyeInImagePlate(eye_pos);
939
-		this.cubeCanvas.getPixelLocationInImagePlate(iX, iY, mousePosn);
940
-	
941
-		//Transform from ImagePlate coordinates to Vworld coordinates
942
-		Transform3D motion = new Transform3D();
943
-		this.cubeCanvas.getImagePlateToVworld(motion);
944
-	
945
-		motion.transform(eye_pos);
946
-		motion.transform(mousePosn);
947
-	
948
-		//calculate the intersection point on Z=0
949
-		double dblX = (-eye_pos.z / (mousePosn.z - eye_pos.z)) * (mousePosn.x - eye_pos.x) + eye_pos.x;
950
-		double dblY = (-eye_pos.z / (mousePosn.z - eye_pos.z)) * (mousePosn.y - eye_pos.y) + eye_pos.y;
951
-		
952
-		return new Point2d (dblX, dblY);
953
-	}
954 927
 }

+ 59
- 6
CubeControl/Led3D.java 查看文件

@@ -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,

正在加载...
取消
保存