Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Led3D.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <<<<<<< HEAD
  2. /*
  3. * Led3D.java
  4. *
  5. *
  6. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  7. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  8. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  9. *
  10. * This file is part of LED-Cube.
  11. *
  12. * LED-Cube is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * LED-Cube is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. import com.sun.j3d.utils.universe.*;
  26. import com.sun.j3d.utils.geometry.*;
  27. import javax.media.j3d.*;
  28. import javax.vecmath.*;
  29. import com.sun.j3d.utils.behaviors.mouse.*;
  30. import com.sun.j3d.utils.image.TextureLoader;
  31. import java.awt.Toolkit;
  32. import com.sun.j3d.utils.picking.*;
  33. import java.awt.event.*;
  34. /**
  35. * This class is responsible for displaying the 3D View of our Cube.
  36. *
  37. * @author Thomas Buck
  38. * @author Max Nuding
  39. * @author Felix Bäder
  40. * @version 1.0
  41. */
  42. public class Led3D extends MouseAdapter {
  43. private Canvas3D canvas = null;
  44. private PickCanvas pickCanvas = null;
  45. private SimpleUniverse universe = null;
  46. private BranchGroup group = null;
  47. private Transform3D trans3D = null;
  48. private TransformGroup transGroup = null;
  49. private Matrix4d mat = null;
  50. private Matrix4d fullScreenMat = null;
  51. private Frame parentFrame = null;
  52. private Sphere[][][] leds = new Sphere[8][8][8];
  53. private static ColoringAttributes redColor = new ColoringAttributes(
  54. new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
  55. private static ColoringAttributes whiteColor = new ColoringAttributes(
  56. new Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
  57. private static Material whiteMat = new Material(new Color3f(0.2f, 0.2f,
  58. 0.2f), new Color3f(0.0f, 0.0f, 0.0f),
  59. new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.2f, 0.2f, 0.2f), 64.0f);
  60. private static Material redMat = new Material(
  61. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f),
  62. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), 64.0f);
  63. private Background background;
  64. /**
  65. * @param canv The Canvas3D we render our cube in
  66. */
  67. public Led3D(Canvas3D canv, Frame f) {
  68. canvas = canv;
  69. parentFrame = f;
  70. group = new BranchGroup();
  71. // Position viewer, so we are looking at object
  72. trans3D = new Transform3D();
  73. mat = new Matrix4d();
  74. mat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
  75. mat.setRow(1, -0.08, -0.995, 0.061, 0.02);
  76. mat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
  77. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  78. fullScreenMat = new Matrix4d();
  79. fullScreenMat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
  80. fullScreenMat.setRow(1, -0.08, -0.995, 0.061, 0.02);
  81. fullScreenMat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
  82. fullScreenMat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  83. trans3D.set(mat);
  84. transGroup = new TransformGroup(trans3D);
  85. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  86. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  87. ViewingPlatform viewingPlatform = new ViewingPlatform();
  88. Viewer viewer = new Viewer(canvas);
  89. universe = new SimpleUniverse(viewingPlatform, viewer);
  90. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0),
  91. new Point3d(13.0, 13.0, 13.0));
  92. // roration with left mouse button
  93. MouseRotate behaviour = new MouseRotate(transGroup);
  94. behaviour.setSchedulingBounds(boundBox);
  95. transGroup.addChild(behaviour);
  96. // zoom with middle mouse button
  97. MouseZoom beh2 = new MouseZoom(transGroup);
  98. beh2.setSchedulingBounds(boundBox);
  99. transGroup.addChild(beh2);
  100. // move with right mouse button
  101. MouseTranslate beh3 = new MouseTranslate(transGroup);
  102. beh3.setSchedulingBounds(boundBox);
  103. transGroup.addChild(beh3);
  104. group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  105. group.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  106. group.setCapability(BranchGroup.ALLOW_DETACH);
  107. background = createBackground();
  108. group.addChild(background);
  109. // Add all our led sphares to the universe
  110. for (int x = 0; x < 8; x++) {
  111. for (int y = 0; y < 8; y++) {
  112. for (int z = 0; z < 8; z++) {
  113. Appearance a = new Appearance();
  114. a.setMaterial(whiteMat);
  115. a.setColoringAttributes(whiteColor);
  116. leds[x][y][z] = new Sphere(0.08f,
  117. Sphere.ENABLE_APPEARANCE_MODIFY, a);
  118. TransformGroup tg = new TransformGroup();
  119. Transform3D transform = new Transform3D();
  120. Vector3f vector = new Vector3f(x - 3.5f, y -3.5f, z-3.5f);
  121. transform.setTranslation(vector);
  122. tg.setTransform(transform);
  123. tg.addChild(leds[x][y][z]);
  124. transGroup.addChild(tg);
  125. drawLedFeetVertical((double) x - 3.5, y - 3.5, (double) z-3.5, 0.9f, 0.01f);
  126. if (x < 7)
  127. drawLedFeetHorizontal(x - 3, (double) y - 3.5, (double) z - 3.5, 0.9f, 0.01f, 0);
  128. }
  129. }
  130. // 8 times, use x as y
  131. for(int i = 0; i > -8; i--){
  132. drawLedFeetHorizontal(i+3.5, (double) x, 0, 7.0f, 0.02f, 90);
  133. }
  134. }
  135. // Add an ambient light
  136. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  137. AmbientLight light2 = new AmbientLight(light2Color);
  138. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
  139. 100.0);
  140. light2.setInfluencingBounds(bounds);
  141. light2.setEnable(true);
  142. transGroup.addChild(light2);
  143. group.addChild(transGroup);
  144. universe.addBranchGraph(group); // Add group to universe
  145. // Mouse-Selectable objects
  146. pickCanvas = new PickCanvas(canvas, group);
  147. pickCanvas.setMode(PickCanvas.BOUNDS);
  148. canvas.addMouseListener(this);
  149. }
  150. /**
  151. * Listen for mouse events so the user can click on LEDs.
  152. * @param e MouseEvent generated by the user
  153. */
  154. public void mouseClicked(MouseEvent e) {
  155. pickCanvas.setShapeLocation(e);
  156. PickResult result = pickCanvas.pickClosest();
  157. if (result != null) {
  158. // User clicked near something
  159. Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
  160. if (p != null) {
  161. // p is now a Primitive that the user clicked
  162. if (p.getClass().getName().equals("com.sun.j3d.utils.geometry.Sphere")) {
  163. // p is a Cylinder. Our LEDs are Spheres, so p.equals(led[x][y][z]) does not find anything...
  164. for (int x = 0; x < 8; x++) {
  165. for (int y = 0; y < 8; y++) {
  166. for (int z = 0; z < 8; z++) {
  167. if (p.equals(leds[x][y][z])) {
  168. // Clicked led found!
  169. System.out.println("Clicked LED found: " + x + " " + y + " " + z);
  170. parentFrame.toggleLED(x, y, z);
  171. x = 8;
  172. y = 8;
  173. z = 8;
  174. }
  175. }
  176. }
  177. }
  178. } else {
  179. System.out.println("Clicked, but not a sphere. Clicked object: " + p.getClass().getName());
  180. if(p.getClass().getName().equals("com.sun.j3d.utils.geometry.Cylinder")){
  181. }
  182. }
  183. }
  184. } else {
  185. System.out.println("Clicked, but hit nothing");
  186. }
  187. }
  188. private void drawLedFeetVertical(double x, double y, double z,
  189. float length, float rad) {
  190. // draw Feet going down
  191. Appearance feetApp = new Appearance();
  192. feetApp.setMaterial(whiteMat);
  193. feetApp.setColoringAttributes(whiteColor);
  194. Cylinder c = new Cylinder(rad, length, feetApp);
  195. TransformGroup tg = new TransformGroup();
  196. Transform3D transform = new Transform3D();
  197. Vector3d vector = new Vector3d(x, y, z);
  198. transform.setTranslation(vector);
  199. tg.setTransform(transform);
  200. tg.addChild(c);
  201. transGroup.addChild(tg);
  202. }
  203. private void drawLedFeetHorizontal(double x, double y, double z,
  204. float length, float rad, int deg) {
  205. // draw Feet going down
  206. Appearance feetApp = new Appearance();
  207. feetApp.setMaterial(whiteMat);
  208. feetApp.setColoringAttributes(whiteColor);
  209. Cylinder c = new Cylinder(rad, length, feetApp);
  210. TransformGroup tg = new TransformGroup();
  211. Transform3D transform = new Transform3D();
  212. Vector3d vector = new Vector3d(x, y, z);
  213. transform.rotZ(Math.toRadians(90));
  214. if (deg != 0)
  215. transform.rotX(Math.toRadians(deg));
  216. transform.setTranslation(vector);
  217. tg.setTransform(transform);
  218. tg.addChild(c);
  219. transGroup.addChild(tg);
  220. }
  221. /**
  222. * Rotate the cube back to its initial position.
  223. */
  224. public void resetView() {
  225. Matrix4d mat = new Matrix4d();
  226. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  227. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  228. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  229. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  230. trans3D.set(mat);
  231. transGroup.setTransform(trans3D);
  232. }
  233. /**
  234. * Prints the translation matrix that is changed by moving/rotating the 3D
  235. * Cube with your mouse.
  236. */
  237. public void printTranslationData() {
  238. Matrix4d mat = new Matrix4d();
  239. Transform3D t = new Transform3D();
  240. transGroup.getTransform(t);
  241. t.get(mat);
  242. String s = mat.toString();
  243. System.out.println(s.replaceAll(", ", "\t"));
  244. }
  245. /**
  246. * Sets the data that is displayed by the LEDs
  247. *
  248. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  249. */
  250. public void setData(short[] data) {
  251. for (int y = 0; y < 8; y++) {
  252. for (int z = 0; z < 8; z++) {
  253. for (int x = 0; x < 8; x++) {
  254. Appearance a = new Appearance();
  255. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  256. // Activate led
  257. a.setColoringAttributes(redColor);
  258. a.setMaterial(redMat);
  259. } else {
  260. // Deactivate led
  261. a.setColoringAttributes(whiteColor);
  262. a.setMaterial(whiteMat);
  263. }
  264. leds[x][y][z].setAppearance(a);
  265. }
  266. }
  267. }
  268. }
  269. // create nice background
  270. private Background createBackground() {
  271. Background bg = new Background(0.0f, 0.0f, 1.0f);
  272. int radius = canvas.getWidth();
  273. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), radius));
  274. return bg;
  275. }
  276. /**
  277. * Create new background to reflect changed Canvas size.
  278. */
  279. private void toggleFullscreen() {
  280. group.detach();
  281. if(group.indexOfChild(background) != -1){
  282. group.removeChild(background);
  283. }
  284. background = createBackground();
  285. group.addChild(background);
  286. universe.addBranchGraph(group);
  287. }
  288. /**
  289. * Create new background and adjust view.
  290. */
  291. public void enterFullscreen() {
  292. toggleFullscreen();
  293. trans3D.set(fullScreenMat);
  294. transGroup.setTransform(trans3D);
  295. }
  296. /**
  297. * Create new background and adjust view.
  298. */
  299. public void leaveFullscreen() {
  300. toggleFullscreen();
  301. trans3D.set(mat);
  302. transGroup.setTransform(trans3D);
  303. }
  304. }
  305. =======
  306. /*
  307. * Led3D.java
  308. *
  309. *
  310. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  311. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  312. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  313. *
  314. * This file is part of LED-Cube.
  315. *
  316. * LED-Cube is free software: you can redistribute it and/or modify
  317. * it under the terms of the GNU General Public License as published by
  318. * the Free Software Foundation, either version 3 of the License, or
  319. * (at your option) any later version.
  320. *
  321. * LED-Cube is distributed in the hope that it will be useful,
  322. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  323. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  324. * GNU General Public License for more details.
  325. *
  326. * You should have received a copy of the GNU General Public License
  327. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  328. */
  329. import com.sun.j3d.utils.universe.*;
  330. import com.sun.j3d.utils.geometry.*;
  331. import javax.media.j3d.*;
  332. import javax.vecmath.*;
  333. import com.sun.j3d.utils.behaviors.mouse.*;
  334. import com.sun.j3d.utils.image.TextureLoader;
  335. import java.awt.Toolkit;
  336. import com.sun.j3d.utils.picking.*;
  337. import java.awt.event.*;
  338. /**
  339. * This class is responsible for displaying the 3D View of our Cube.
  340. *
  341. * @author Thomas Buck
  342. * @author Max Nuding
  343. * @author Felix Bäder
  344. * @version 1.0
  345. */
  346. public class Led3D extends MouseAdapter {
  347. private Canvas3D canvas = null;
  348. private PickCanvas pickCanvas = null;
  349. private SimpleUniverse universe = null;
  350. private BranchGroup group = null;
  351. private Transform3D trans3D = null;
  352. private TransformGroup transGroup = null;
  353. private Matrix4d mat = null;
  354. private Matrix4d fullScreenMat = null;
  355. private Frame parentFrame = null;
  356. private Sphere[][][] leds = new Sphere[8][8][8];
  357. private static ColoringAttributes redColor = new ColoringAttributes(
  358. new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
  359. private static ColoringAttributes whiteColor = new ColoringAttributes(
  360. new Color3f(0.2f, 0.2f, 0.2f), ColoringAttributes.FASTEST);
  361. private static Material whiteMat = new Material(new Color3f(0.2f, 0.2f,
  362. 0.2f), new Color3f(0.0f, 0.0f, 0.0f),
  363. new Color3f(0.2f, 0.2f, 0.2f), new Color3f(0.2f, 0.2f, 0.2f), 64.0f);
  364. private static Material redMat = new Material(
  365. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f),
  366. new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), 64.0f);
  367. private Background background;
  368. /**
  369. * @param canv The Canvas3D we render our cube in
  370. */
  371. public Led3D(Canvas3D canv, Frame f) {
  372. canvas = canv;
  373. parentFrame = f;
  374. group = new BranchGroup();
  375. // Position viewer, so we are looking at object
  376. trans3D = new Transform3D();
  377. mat = new Matrix4d();
  378. mat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
  379. mat.setRow(1, -0.08, -0.995, 0.061, 0.02);
  380. mat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
  381. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  382. fullScreenMat = new Matrix4d();
  383. fullScreenMat.setRow(0, 0.7597, -0.0204, 0.64926, 0.56);
  384. fullScreenMat.setRow(1, -0.08, -0.995, 0.061, 0.02);
  385. fullScreenMat.setRow(2, 0.64473, -0.09786, -0.758, -14.68);
  386. fullScreenMat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  387. trans3D.set(mat);
  388. transGroup = new TransformGroup(trans3D);
  389. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  390. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  391. ViewingPlatform viewingPlatform = new ViewingPlatform();
  392. Viewer viewer = new Viewer(canvas);
  393. universe = new SimpleUniverse(viewingPlatform, viewer);
  394. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0),
  395. new Point3d(13.0, 13.0, 13.0));
  396. // roration with left mouse button
  397. MouseRotate behaviour = new MouseRotate(transGroup);
  398. behaviour.setSchedulingBounds(boundBox);
  399. transGroup.addChild(behaviour);
  400. // zoom with middle mouse button
  401. MouseZoom beh2 = new MouseZoom(transGroup);
  402. beh2.setSchedulingBounds(boundBox);
  403. transGroup.addChild(beh2);
  404. // move with right mouse button
  405. MouseTranslate beh3 = new MouseTranslate(transGroup);
  406. beh3.setSchedulingBounds(boundBox);
  407. transGroup.addChild(beh3);
  408. group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  409. group.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  410. group.setCapability(BranchGroup.ALLOW_DETACH);
  411. background = createBackground();
  412. group.addChild(background);
  413. // Add all our led sphares to the universe
  414. for (int x = 0; x < 8; x++) {
  415. for (int y = 0; y < 8; y++) {
  416. for (int z = 0; z < 8; z++) {
  417. Appearance a = new Appearance();
  418. a.setMaterial(whiteMat);
  419. a.setColoringAttributes(whiteColor);
  420. leds[x][y][z] = new Sphere(0.08f,
  421. Sphere.ENABLE_APPEARANCE_MODIFY, a);
  422. TransformGroup tg = new TransformGroup();
  423. Transform3D transform = new Transform3D();
  424. Vector3f vector = new Vector3f(x - 3.5f, y -3.5f, z-3.5f);
  425. transform.setTranslation(vector);
  426. tg.setTransform(transform);
  427. tg.addChild(leds[x][y][z]);
  428. transGroup.addChild(tg);
  429. drawLedFeetVertical((double) x - 3.5, y - 3, (double) z-3.5, 0.9f, 0.01f);
  430. if (x < 7)
  431. drawLedFeetHorizontal(x - 3, (double) y - 3.5, (double) z - 3.5, 0.9f, 0.01f, 0);
  432. }
  433. }
  434. // 8 times, use x as y
  435. for(int i = 0; i > -8; i--){
  436. drawLedFeetHorizontal(i+3.5, (double) x-3.5, 0, 7.0f, 0.02f, 90);
  437. }
  438. }
  439. drawLedFeetVertical(0, 0, 0, 10, 0.01f); //
  440. drawLedFeetHorizontal(0, 0, 0, 10, 0.01f, 0); // x, y, and z axis
  441. Appearance c = new Appearance();
  442. c.setMaterial(redMat);
  443. c.setColoringAttributes(redColor);
  444. Sphere center = new Sphere(0.04f, Sphere.ENABLE_APPEARANCE_MODIFY, c);
  445. TransformGroup tg = new TransformGroup();
  446. Transform3D transform = new Transform3D();
  447. Vector3f vector = new Vector3f(0, 0, 0);
  448. transform.setTranslation(vector);
  449. tg.setTransform(transform);
  450. tg.addChild(center);
  451. transGroup.addChild(tg);
  452. // Add an ambient light
  453. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  454. AmbientLight light2 = new AmbientLight(light2Color);
  455. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
  456. 100.0);
  457. light2.setInfluencingBounds(bounds);
  458. light2.setEnable(true);
  459. transGroup.addChild(light2);
  460. group.addChild(transGroup);
  461. universe.addBranchGraph(group); // Add group to universe
  462. // Mouse-Selectable objects
  463. pickCanvas = new PickCanvas(canvas, group);
  464. pickCanvas.setMode(PickCanvas.BOUNDS);
  465. canvas.addMouseListener(this);
  466. }
  467. /**
  468. * Listen for mouse events so the user can click on LEDs.
  469. * @param e MouseEvent generated by the user
  470. */
  471. public void mouseClicked(MouseEvent e) {
  472. pickCanvas.setShapeLocation(e);
  473. PickResult result = pickCanvas.pickClosest();
  474. if (result != null) {
  475. // User clicked near something
  476. Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
  477. if (p != null) {
  478. // p is now a Primitive that the user clicked
  479. if (p.getClass().getName().equals("com.sun.j3d.utils.geometry.Sphere")) {
  480. // p is a Cylinder. Our LEDs are Spheres, so p.equals(led[x][y][z]) does not find anything...
  481. for (int x = 0; x < 8; x++) {
  482. for (int y = 0; y < 8; y++) {
  483. for (int z = 0; z < 8; z++) {
  484. if (p.equals(leds[x][y][z])) {
  485. // Clicked led found!
  486. System.out.println("Clicked LED found: " + x + " " + y + " " + z);
  487. parentFrame.toggleLED(x, y, z);
  488. x = 8;
  489. y = 8;
  490. z = 8;
  491. }
  492. }
  493. }
  494. }
  495. } else {
  496. System.out.println("Clicked, but not a sphere. Clicked object: " + p.getClass().getName());
  497. if(p.getClass().getName().equals("com.sun.j3d.utils.geometry.Cylinder")){
  498. }
  499. }
  500. }
  501. } else {
  502. System.out.println("Clicked, but hit nothing");
  503. }
  504. }
  505. private void drawLedFeetVertical(double x, double y, double z,
  506. float length, float rad) {
  507. // draw Feet going down
  508. Appearance feetApp = new Appearance();
  509. feetApp.setMaterial(whiteMat);
  510. feetApp.setColoringAttributes(whiteColor);
  511. Cylinder c = new Cylinder(rad, length, feetApp);
  512. TransformGroup tg = new TransformGroup();
  513. Transform3D transform = new Transform3D();
  514. Vector3d vector = new Vector3d(x, y, z);
  515. transform.setTranslation(vector);
  516. tg.setTransform(transform);
  517. tg.addChild(c);
  518. transGroup.addChild(tg);
  519. }
  520. private void drawLedFeetHorizontal(double x, double y, double z,
  521. float length, float rad, int deg) {
  522. // draw Feet going down
  523. Appearance feetApp = new Appearance();
  524. feetApp.setMaterial(whiteMat);
  525. feetApp.setColoringAttributes(whiteColor);
  526. Cylinder c = new Cylinder(rad, length, feetApp);
  527. TransformGroup tg = new TransformGroup();
  528. Transform3D transform = new Transform3D();
  529. Vector3d vector = new Vector3d(x, y, z);
  530. transform.rotZ(Math.toRadians(90));
  531. if (deg != 0)
  532. transform.rotX(Math.toRadians(deg));
  533. transform.setTranslation(vector);
  534. tg.setTransform(transform);
  535. tg.addChild(c);
  536. transGroup.addChild(tg);
  537. }
  538. /**
  539. * Rotate the cube back to its initial position.
  540. */
  541. public void resetView() {
  542. Matrix4d mat = new Matrix4d();
  543. mat.setRow(0, 0.744, 0.0237, -0.66756, -0.34);
  544. mat.setRow(1, 0.136, -0.9837, 0.117, 3.24);
  545. mat.setRow(2, -0.6536, -0.1785, -0.735, -8.32);
  546. mat.setRow(3, 0.0, 0.0, 0.0, 1.0);
  547. trans3D.set(mat);
  548. transGroup.setTransform(trans3D);
  549. }
  550. /**
  551. * Prints the translation matrix that is changed by moving/rotating the 3D
  552. * Cube with your mouse.
  553. */
  554. public void printTranslationData() {
  555. Matrix4d mat = new Matrix4d();
  556. Transform3D t = new Transform3D();
  557. transGroup.getTransform(t);
  558. t.get(mat);
  559. String s = mat.toString();
  560. System.out.println(s.replaceAll(", ", "\t"));
  561. }
  562. /**
  563. * Sets the data that is displayed by the LEDs
  564. *
  565. * @param data 64 byte array with the data (8 bits/LEDs per byte)
  566. */
  567. public void setData(short[] data) {
  568. for (int y = 0; y < 8; y++) {
  569. for (int z = 0; z < 8; z++) {
  570. for (int x = 0; x < 8; x++) {
  571. Appearance a = new Appearance();
  572. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  573. // Activate led
  574. a.setColoringAttributes(redColor);
  575. a.setMaterial(redMat);
  576. } else {
  577. // Deactivate led
  578. a.setColoringAttributes(whiteColor);
  579. a.setMaterial(whiteMat);
  580. }
  581. leds[x][y][z].setAppearance(a);
  582. }
  583. }
  584. }
  585. }
  586. // create nice background
  587. private Background createBackground() {
  588. Background bg = new Background(0.0f, 0.0f, 1.0f);
  589. int radius = canvas.getWidth();
  590. bg.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), radius));
  591. return bg;
  592. }
  593. /**
  594. * Create new background to reflect changed Canvas size.
  595. */
  596. private void toggleFullscreen() {
  597. group.detach();
  598. if(group.indexOfChild(background) != -1){
  599. group.removeChild(background);
  600. }
  601. background = createBackground();
  602. group.addChild(background);
  603. universe.addBranchGraph(group);
  604. }
  605. /**
  606. * Create new background and adjust view.
  607. */
  608. public void enterFullscreen() {
  609. toggleFullscreen();
  610. trans3D.set(fullScreenMat);
  611. transGroup.setTransform(trans3D);
  612. }
  613. /**
  614. * Create new background and adjust view.
  615. */
  616. public void leaveFullscreen() {
  617. toggleFullscreen();
  618. trans3D.set(mat);
  619. transGroup.setTransform(trans3D);
  620. }
  621. }
  622. >>>>>>> Fixed levs, added axis and center point