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.

Frame.java 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * Frame.java
  3. *
  4. *
  5. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  6. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  7. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  8. *
  9. * This file is part of LED-Cube.
  10. *
  11. * LED-Cube is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * LED-Cube is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. import com.sun.j3d.utils.universe.*;
  25. import javax.media.j3d.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import javax.swing.*;
  29. import javax.swing.event.*;
  30. import java.io.File;
  31. import javax.vecmath.Point2d;
  32. import javax.vecmath.Point3d;
  33. public class Frame extends JFrame implements ListSelectionListener {
  34. private static final long serialVersionUID = 23421337L;
  35. // Anfang Variablen
  36. private GraphicsConfiguration gConfig = SimpleUniverse
  37. .getPreferredConfiguration();
  38. private Canvas3D cubeCanvas = new Canvas3D(gConfig);
  39. public Led3D ledView = new Led3D(cubeCanvas);
  40. // Anfang Attribute
  41. private JButton editA = new JButton();
  42. private JButton editB = new JButton();
  43. private JButton editC = new JButton();
  44. private JButton editD = new JButton();
  45. private JButton editE = new JButton();
  46. private JButton editF = new JButton();
  47. private JButton editG = new JButton();
  48. private JButton editH = new JButton();
  49. private DefaultListModel frameListModel = new DefaultListModel();
  50. public JList frameList = new JList();
  51. private JScrollPane frameListScrollPane = new JScrollPane(frameList);
  52. private JButton frameUp = new JButton();
  53. private JButton frameDown = new JButton();
  54. private JButton frameAdd = new JButton();
  55. private JButton frameRemove = new JButton();
  56. private JButton frameRename = new JButton();
  57. private JList animList = new JList();
  58. private DefaultListModel animModel = new DefaultListModel();
  59. private JScrollPane animScrollPane = new JScrollPane(animList);
  60. private JButton animUp = new JButton();
  61. private JButton animDown = new JButton();
  62. private JButton animAdd = new JButton();
  63. private JButton animRemove = new JButton();
  64. private JButton animRename = new JButton();
  65. private JTextField animPath = new JTextField();
  66. private JButton load = new JButton();
  67. private JButton save = new JButton();
  68. private JButton saveAs = new JButton();
  69. public JComboBox jComboBox1 = new JComboBox();
  70. private JButton upload = new JButton();
  71. private JButton download = new JButton();
  72. private JLabel jLabel4 = new JLabel();
  73. private JTextField frameRemaining = new JTextField();
  74. private JLabel frameLengthLabel = new JLabel();
  75. private JTextField frameLengthText = new JTextField();
  76. private JButton frameDuration = new JButton();
  77. private JButton fullScreenButton = new JButton();
  78. private JButton playAnimation = new JButton();
  79. private JButton exitButton;
  80. private JButton playAnimationFullscreen = new JButton();
  81. private JPanel previewPanel = new JPanel();
  82. private JPanel framePanel = new JPanel();
  83. private JPanel animPanel = new JPanel();
  84. private JPanel filePanel = new JPanel();
  85. private JPanel serialPanel = new JPanel();
  86. private JPanel settingsPanel = new JPanel();
  87. // Ende Attribute
  88. public cubeWorker worker = new cubeWorker(this);
  89. private boolean fileSelected = false;
  90. private Frame thisFrame;
  91. // Ende Variablen
  92. private int saveExitDialog() {
  93. String[] Optionen = { "Yes", "No" };
  94. int Auswahl = JOptionPane.showOptionDialog(this,
  95. "Do you want to save your changes?", "Save?",
  96. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  97. Optionen, Optionen[0]);
  98. if (Auswahl == JOptionPane.YES_OPTION) {
  99. return 1;
  100. } else {
  101. return 0;
  102. }
  103. }
  104. public String askString(String title, String text) {
  105. return JOptionPane.showInputDialog(null, text, title,
  106. JOptionPane.QUESTION_MESSAGE);
  107. }
  108. public void errorMessage(String s) {
  109. String[] Optionen = { "OK" };
  110. JOptionPane.showOptionDialog(this, s, "Error!", JOptionPane.YES_OPTION,
  111. JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
  112. }
  113. public void errorMessage(String title, String msg) {
  114. String[] Optionen = { "OK" };
  115. JOptionPane.showOptionDialog(this, msg, title, JOptionPane.YES_OPTION,
  116. JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
  117. }
  118. public void valueChanged(ListSelectionEvent evt) {
  119. if ((!evt.getValueIsAdjusting())
  120. && ((evt.getSource() == animList) || (evt.getSource() == frameList))) {
  121. // If animList or framsList is the source, we act...
  122. if ((evt.getSource() == animList)
  123. && (animList.getSelectedIndex() != -1)) {
  124. // animList selection changed, update frameList
  125. frameListModel.clear();
  126. // System.out.println("Selected Animation: " + animList.getSelectedIndex());
  127. int max = worker.getAnimation(animList.getSelectedIndex()).size();
  128. for (int i = 0; i < max; i++) {
  129. frameListModel.addElement(worker.getAnimation(
  130. animList.getSelectedIndex()).getFrame(i).getName());
  131. }
  132. frameList.setModel(frameListModel);
  133. }
  134. // If both selections are valid, update Frame duration and set 3D
  135. // data
  136. if ((animList.getSelectedIndex() != -1) && (frameList.getSelectedIndex() != -1)) {
  137. ledView.setData(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getData());
  138. frameLengthText.setText(Integer.toString(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime()));
  139. } else {
  140. // clear Frame duration
  141. frameLengthText.setText("");
  142. }
  143. }
  144. }
  145. private void save() {
  146. if (fileSelected == false) {
  147. JFileChooser fc = new JFileChooser();
  148. int ret = fc.showSaveDialog(this);
  149. if (ret == JFileChooser.APPROVE_OPTION) {
  150. File file = fc.getSelectedFile();
  151. fileSelected = true;
  152. animPath.setText(file.getPath());
  153. worker.saveState(animPath.getText());
  154. }
  155. } else {
  156. worker.saveState(animPath.getText());
  157. }
  158. }
  159. public Frame(String title) {
  160. // Frame-Initialisierung
  161. super(title);
  162. thisFrame = this;
  163. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  164. String[] sPorts = HelperUtility.getPorts();
  165. for (int i = 0; i < sPorts.length; i++) {
  166. jComboBox1.addItem(sPorts[i]);
  167. }
  168. for (int i = 0; i < worker.size(); i++) {
  169. animModel.addElement(worker.getAnimation(i).getName());
  170. }
  171. addWindowListener(new WindowAdapter() {
  172. public void windowClosing(WindowEvent evt) {
  173. if (worker.changedStateSinceSave()) {
  174. if (saveExitDialog() == 1) {
  175. save();
  176. } else {
  177. return;
  178. }
  179. }
  180. System.exit(0);
  181. }
  182. });
  183. int frameWidth = 672;
  184. int frameHeight = 656;
  185. setSize(frameWidth, frameHeight);
  186. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  187. int x = (d.width - getSize().width) / 2;
  188. int y = (d.height - getSize().height) / 2;
  189. setLocation(x, y);
  190. Container cp = getContentPane();
  191. cp.setLayout(null);
  192. // Anfang Komponenten
  193. // ----- 3D-----
  194. // -------------
  195. cubeCanvas.setBounds(18, 31, 275, 275);
  196. cubeCanvas.addMouseListener(new MouseListener() {
  197. public void mouseClicked(MouseEvent e){
  198. Point2d mousePos = convertMousePositionToWorld(e.getX(), e.getY());
  199. }
  200. public void mouseExited(MouseEvent e){}
  201. public void mouseEntered(MouseEvent e){}
  202. public void mouseReleased(MouseEvent e){}
  203. public void mousePressed(MouseEvent e){}
  204. });
  205. cp.add(cubeCanvas);
  206. // -------------
  207. editA.setBounds(299, 32, 102, 29);
  208. editA.setText("Layer A");
  209. editA.setFont(new Font("Dialog", Font.PLAIN, 13));
  210. cp.add(editA);
  211. editA.addActionListener(new ActionListener() {
  212. public void actionPerformed(ActionEvent evt) {
  213. if (animList.getSelectedIndex() == -1) {
  214. errorMessage("Please select an animation.");
  215. } else if (frameList.getSelectedIndex() == -1) {
  216. errorMessage("Please select a Frame.");
  217. } else {
  218. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 0, worker, thisFrame);
  219. }
  220. }
  221. });
  222. editB.setBounds(299, 67, 102, 29);
  223. editB.setText("Layer B");
  224. editB.setFont(new Font("Dialog", Font.PLAIN, 13));
  225. cp.add(editB);
  226. editB.addActionListener(new ActionListener() {
  227. public void actionPerformed(ActionEvent evt) {
  228. if (animList.getSelectedIndex() == -1) {
  229. errorMessage("Please select an animation.");
  230. } else if (frameList.getSelectedIndex() == -1) {
  231. errorMessage("Please select a Frame.");
  232. } else {
  233. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 1, worker, thisFrame);
  234. }
  235. }
  236. });
  237. editC.setBounds(299, 102, 102, 29);
  238. editC.setText("Layer C");
  239. editC.setFont(new Font("Dialog", Font.PLAIN, 13));
  240. cp.add(editC);
  241. editC.addActionListener(new ActionListener() {
  242. public void actionPerformed(ActionEvent evt) {
  243. if (animList.getSelectedIndex() == -1) {
  244. errorMessage("Please select an animation.");
  245. } else if (frameList.getSelectedIndex() == -1) {
  246. errorMessage("Please select a Frame.");
  247. } else {
  248. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 2, worker, thisFrame);
  249. }
  250. }
  251. });
  252. editD.setBounds(299, 137, 102, 29);
  253. editD.setText("Layer D");
  254. editD.setFont(new Font("Dialog", Font.PLAIN, 13));
  255. cp.add(editD);
  256. editD.addActionListener(new ActionListener() {
  257. public void actionPerformed(ActionEvent evt) {
  258. if (animList.getSelectedIndex() == -1) {
  259. errorMessage("Please select an animation.");
  260. } else if (frameList.getSelectedIndex() == -1) {
  261. errorMessage("Please select a Frame.");
  262. } else {
  263. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 3, worker, thisFrame);
  264. }
  265. }
  266. });
  267. editE.setBounds(299, 172, 102, 29);
  268. editE.setText("Layer E");
  269. editE.setFont(new Font("Dialog", Font.PLAIN, 13));
  270. cp.add(editE);
  271. editE.addActionListener(new ActionListener() {
  272. public void actionPerformed(ActionEvent evt) {
  273. if (animList.getSelectedIndex() == -1) {
  274. errorMessage("Please select an animation.");
  275. } else if (frameList.getSelectedIndex() == -1) {
  276. errorMessage("Please select a Frame.");
  277. } else {
  278. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 4, worker, thisFrame);
  279. }
  280. }
  281. });
  282. editF.setBounds(299, 207, 102, 29);
  283. editF.setText("Layer F");
  284. editF.setFont(new Font("Dialog", Font.PLAIN, 13));
  285. cp.add(editF);
  286. editF.addActionListener(new ActionListener() {
  287. public void actionPerformed(ActionEvent evt) {
  288. if (animList.getSelectedIndex() == -1) {
  289. errorMessage("Please select an animation.");
  290. } else if (frameList.getSelectedIndex() == -1) {
  291. errorMessage("Please select a Frame.");
  292. } else {
  293. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 5, worker, thisFrame);
  294. }
  295. }
  296. });
  297. editG.setBounds(299, 242, 102, 29);
  298. editG.setText("Layer G");
  299. editG.setFont(new Font("Dialog", Font.PLAIN, 13));
  300. cp.add(editG);
  301. editG.addActionListener(new ActionListener() {
  302. public void actionPerformed(ActionEvent evt) {
  303. if (animList.getSelectedIndex() == -1) {
  304. errorMessage("Please select an animation.");
  305. } else if (frameList.getSelectedIndex() == -1) {
  306. errorMessage("Please select a Frame.");
  307. } else {
  308. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 6, worker, thisFrame);
  309. }
  310. }
  311. });
  312. editH.setBounds(299, 277, 102, 29);
  313. editH.setText("Layer H");
  314. editH.setFont(new Font("Dialog", Font.PLAIN, 13));
  315. cp.add(editH);
  316. editH.addActionListener(new ActionListener() {
  317. public void actionPerformed(ActionEvent evt) {
  318. if (animList.getSelectedIndex() == -1) {
  319. errorMessage("Please select an animation.");
  320. } else if (frameList.getSelectedIndex() == -1) {
  321. errorMessage("Please select a Frame.");
  322. } else {
  323. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 7, worker, thisFrame);
  324. }
  325. }
  326. });
  327. frameListScrollPane.setBounds(339, 379, 187, 218);
  328. frameList.setModel(frameListModel);
  329. cp.add(frameListScrollPane);
  330. frameUp.setBounds(532, 378, 110, 39);
  331. frameUp.setText("Move up");
  332. frameUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  333. cp.add(frameUp);
  334. frameUp.addActionListener(new ActionListener() {
  335. public void actionPerformed(ActionEvent evt) {
  336. int i = frameList.getSelectedIndex();
  337. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  338. Object tmp = frameListModel.get(i);
  339. frameListModel.set(i, frameListModel.get(i - 1));
  340. frameListModel.set(i - 1, tmp);
  341. frameList.setSelectedIndex(i - 1);
  342. worker.getAnimation(animList.getSelectedIndex()).moveFrameUp(i);
  343. }
  344. }
  345. });
  346. frameDown.setBounds(532, 558, 110, 39);
  347. frameDown.setText("Move down");
  348. frameDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  349. cp.add(frameDown);
  350. frameDown.addActionListener(new ActionListener() {
  351. public void actionPerformed(ActionEvent evt) {
  352. int i = frameList.getSelectedIndex();
  353. if ((i >= 0) && (frameListModel.getSize() >= 2) && (i < (frameListModel.getSize() - 1))) {
  354. Object tmp = frameListModel.get(i);
  355. frameListModel.set(i, frameListModel.get(i + 1));
  356. frameListModel.set(i + 1, tmp);
  357. frameList.setSelectedIndex(i + 1);
  358. worker.getAnimation(animList.getSelectedIndex()).moveFrameDown(i);
  359. }
  360. }
  361. });
  362. frameAdd.setBounds(532, 423, 110, 39);
  363. frameAdd.setText("Add");
  364. frameAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  365. cp.add(frameAdd);
  366. frameAdd.addActionListener(new ActionListener() {
  367. public void actionPerformed(ActionEvent evt) {
  368. if (animList.getSelectedIndex() == -1) {
  369. errorMessage("Please select an animation!");
  370. } else {
  371. int n = worker.getAnimation(animList.getSelectedIndex()).size();
  372. worker.getAnimation(animList.getSelectedIndex()).addFrame();
  373. // Not reaching past this comment if frame list empty
  374. frameRemaining.setText(Integer.toString(worker.memoryRemaining()));
  375. frameListModel.add(n, worker.getAnimation(animList.getSelectedIndex()).getFrame(n).getName());
  376. frameList.setModel(frameListModel);
  377. }
  378. }
  379. });
  380. frameRemove.setBounds(532, 468, 110, 39);
  381. frameRemove.setText("Remove");
  382. frameRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  383. cp.add(frameRemove);
  384. frameRemove.addActionListener(new ActionListener() {
  385. public void actionPerformed(ActionEvent evt) {
  386. if (animList.getSelectedIndex() == -1) {
  387. errorMessage("Select an animation.");
  388. } else if (frameList.getSelectedIndex() == -1) {
  389. errorMessage("Select a Frame.");
  390. } else {
  391. worker.getAnimation(animList.getSelectedIndex()).removeFrame(frameList.getSelectedIndex());
  392. frameRemaining.setText(Integer.toString(worker.memoryRemaining()));
  393. frameListModel.removeElementAt(frameList.getSelectedIndex());
  394. frameList.setModel(frameListModel);
  395. }
  396. }
  397. });
  398. frameRename.setBounds(532, 513, 110, 39);
  399. frameRename.setText("Rename");
  400. frameRename.setFont(new Font("Dialog", Font.PLAIN, 13));
  401. cp.add(frameRename);
  402. frameRename.addActionListener(new ActionListener() {
  403. public void actionPerformed(ActionEvent evt) {
  404. int a = animList.getSelectedIndex();
  405. if (a < 0) {
  406. errorMessage("Select an animation!");
  407. return;
  408. }
  409. int f = frameList.getSelectedIndex();
  410. if (f < 0) {
  411. errorMessage("Select a Frame!");
  412. return;
  413. }
  414. worker.getAnimation(a).getFrame(f).setName(askString("Rename", "Rename " + frameList.getSelectedValue() + "?"));
  415. frameListModel.set(f, worker.getAnimation(a).getFrame(f).getName());
  416. frameList.setModel(frameListModel);
  417. }
  418. });
  419. frameLengthLabel.setBounds(429, 118, 82, 13);
  420. frameLengthLabel.setText("Time (1/24 sec)");
  421. frameLengthLabel.setFont(new Font("Dialog", Font.PLAIN, 13));
  422. cp.add(frameLengthLabel);
  423. frameLengthText.setBounds(429, 134, 31, 20);
  424. frameLengthText.setText("");
  425. frameLengthText.setFont(new Font("Dialog", Font.PLAIN, 13));
  426. cp.add(frameLengthText);
  427. fullScreenButton.setText("Fullscreen");
  428. fullScreenButton.setBounds(18, 312, 134, 35);
  429. fullScreenButton.setFont(new Font("Dialog", Font.PLAIN, 13));
  430. cp.add(fullScreenButton);
  431. fullScreenButton.addActionListener(new ActionListener() {
  432. public void actionPerformed(ActionEvent evt) {
  433. ledView.enterFullscreen();
  434. setLocation(0,0);
  435. setSize(700, 700);
  436. int w = Toolkit.getDefaultToolkit().getScreenSize().width;
  437. int h = Toolkit.getDefaultToolkit().getScreenSize().height;
  438. setSize(w - 5, h - 30);
  439. playAnimationFullscreen.setVisible(true);
  440. cubeCanvas.setBounds(0, 0, w - 5, h - 80);
  441. }
  442. });
  443. exitButton = new JButton("Exit Fullscreen");
  444. exitButton.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-150, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  445. cp.add(exitButton);
  446. exitButton.addActionListener(new ActionListener() {
  447. public void actionPerformed(ActionEvent evt) {
  448. playAnimationFullscreen.setVisible(false);
  449. setLocation(0,0);
  450. setSize(661, 440);
  451. ledView.leaveFullscreen();
  452. cubeCanvas.setBounds(8,8, 250,250);
  453. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  454. int x = (d.width - getSize().width) / 2;
  455. int y = (d.height - getSize().height) / 2;
  456. setLocation(x, y);
  457. }
  458. });
  459. playAnimation.setText("Play");
  460. playAnimation.setBounds(159, 312, 134, 35);
  461. playAnimation.setFont(new Font("Dialog", Font.PLAIN, 13));
  462. cp.add(playAnimation);
  463. playAnimation.addActionListener(new ActionListener() {
  464. public void actionPerformed(ActionEvent evt){
  465. if (animList.getSelectedIndex() == -1) {
  466. errorMessage("Please select an animation.");
  467. } else if (frameList.getSelectedIndex() == -1) {
  468. errorMessage("Please select a Frame.");
  469. } else {
  470. for(int i = 0; i < frameList.getModel().getSize(); i++){
  471. frameList.setSelectedIndex(i);
  472. short time1 = worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  473. long time = (long) (((time1+1) * 1/24) * 1000);
  474. try {
  475. Thread.sleep(time);
  476. } catch(Exception e) {
  477. System.out.println(e);
  478. }
  479. }
  480. }
  481. }
  482. });
  483. playAnimationFullscreen.setText("Play");
  484. playAnimationFullscreen.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-310, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  485. playAnimationFullscreen.setFont(new Font("Dialog", Font.PLAIN, 13));
  486. playAnimationFullscreen.setVisible(false);
  487. cp.add(playAnimationFullscreen);
  488. playAnimationFullscreen.addActionListener(new ActionListener() {
  489. public void actionPerformed(ActionEvent evt){
  490. if (animList.getSelectedIndex() == -1) {
  491. errorMessage("Please select an animation.");
  492. } else if (frameList.getSelectedIndex() == -1) {
  493. errorMessage("Please select a Frame.");
  494. } else {
  495. for(int i = 0; i < frameList.getModel().getSize(); i++){
  496. frameList.setSelectedIndex(i);
  497. short time1 = worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  498. long time = (long) (((time1+1) * 1/24) * 1000);
  499. try {
  500. Thread.sleep(time);
  501. } catch(Exception e) {
  502. System.out.println(e);
  503. }
  504. }
  505. }
  506. }
  507. });
  508. frameDuration.setBounds(462, 134, 49, 20);
  509. frameDuration.setText("OK");
  510. frameDuration.setFont(new Font("Dialog", Font.PLAIN, 13));
  511. cp.add(frameDuration);
  512. frameDuration.addActionListener(new ActionListener() {
  513. public void actionPerformed(ActionEvent evt) {
  514. if (frameLengthText.getText().equals("0")) {
  515. errorMessage("0 is not a valid value!");
  516. frameLengthText.setText("1");
  517. } else if (Integer.parseInt(frameLengthText.getText()) > 256) {
  518. errorMessage("Value too high. Max: 256");
  519. frameLengthText.setText("256");
  520. return;
  521. } else {
  522. if (animList.getSelectedIndex() == -1) {
  523. errorMessage("Please select an animation!");
  524. return;
  525. }
  526. if (frameList.getSelectedIndex() == -1) {
  527. errorMessage("Please select a Frame!");
  528. return;
  529. }
  530. worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).setTime((byte)(Integer.parseInt(frameLengthText.getText()) - 1));
  531. }
  532. }
  533. });
  534. animScrollPane.setBounds(18, 378, 187, 219);
  535. animList.setModel(animModel);
  536. cp.add(animScrollPane);
  537. animUp.setBounds(211, 378, 110, 39);
  538. animUp.setText("Move up");
  539. animUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  540. cp.add(animUp);
  541. animUp.addActionListener(new ActionListener() {
  542. public void actionPerformed(ActionEvent evt) {
  543. int i = animList.getSelectedIndex();
  544. if ((i > 0) && (animModel.getSize() >= 2)) {
  545. Object tmp = animModel.get(i);
  546. animModel.set(i, animModel.get(i - 1));
  547. animModel.set(i - 1, tmp);
  548. animList.setSelectedIndex(i - 1);
  549. worker.moveAnimationUp(i);
  550. }
  551. }
  552. });
  553. animDown.setBounds(211, 423, 110, 39);
  554. animDown.setText("Move down");
  555. animDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  556. cp.add(animDown);
  557. animDown.addActionListener(new ActionListener() {
  558. public void actionPerformed(ActionEvent evt) {
  559. int i = animList.getSelectedIndex();
  560. if ((i >= 0) && (animModel.getSize() >= 2) && (i < (animModel.getSize() - 1))) {
  561. Object tmp = animModel.get(i);
  562. animModel.set(i, animModel.get(i + 1));
  563. animModel.set(i + 1, tmp);
  564. animList.setSelectedIndex(i + 1);
  565. worker.moveAnimationDown(i);
  566. }
  567. }
  568. });
  569. animRename.setBounds(211, 468, 110, 39);
  570. animRename.setText("Rename");
  571. animRename.setFont(new Font("Dialog", Font.PLAIN, 13));
  572. cp.add(animRename);
  573. animRename.addActionListener(new ActionListener() {
  574. public void actionPerformed(ActionEvent evt) {
  575. int a = animList.getSelectedIndex();
  576. if (a < 0) {
  577. errorMessage("Select an animation!");
  578. return;
  579. }
  580. worker.getAnimation(a).setName(askString("Rename", "Rename " + animList.getSelectedValue() + "?"));
  581. animModel.set(a, worker.getAnimation(a).getName());
  582. animList.setModel(animModel);
  583. }
  584. });
  585. animAdd.setBounds(211, 513, 110, 39);
  586. animAdd.setText("Add");
  587. animAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  588. cp.add(animAdd);
  589. animAdd.addActionListener(new ActionListener() {
  590. public void actionPerformed(ActionEvent evt) {
  591. if (worker.addAnimation() == -1) {
  592. errorMessage("Could not add animation!");
  593. } else {
  594. animModel.clear();
  595. for (int i = 0; i < worker.size(); i++) {
  596. animModel.add(i, worker.getAnimation(i).getName());
  597. }
  598. animList.setModel(animModel);
  599. }
  600. }
  601. });
  602. animRemove.setBounds(211, 558, 110, 39);
  603. animRemove.setText("Remove");
  604. animRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  605. cp.add(animRemove);
  606. animRemove.addActionListener(new ActionListener() {
  607. public void actionPerformed(ActionEvent evt) {
  608. if (animList.getSelectedIndex() == -1) {
  609. errorMessage("Select an animation.");
  610. } else {
  611. worker.removeAnimation(animList.getSelectedIndex());
  612. animModel.removeElementAt(animList.getSelectedIndex());
  613. animList.setModel(animModel);
  614. }
  615. }
  616. });
  617. animPath.setBounds(417, 281, 228, 20);
  618. animPath.setEditable(false);
  619. animPath.setText("Load/Save an animation file...");
  620. animPath.setFont(new Font("Dialog", Font.PLAIN, 13));
  621. cp.add(animPath);
  622. load.setBounds(344, 296, 100, 25);
  623. load.setText("Load");
  624. load.setFont(new Font("Dialog", Font.PLAIN, 13));
  625. cp.add(load);
  626. load.addActionListener(new ActionListener() {
  627. public void actionPerformed(ActionEvent evt) {
  628. JFileChooser fc = new JFileChooser();
  629. int ret = fc.showOpenDialog(thisFrame);
  630. if (ret == JFileChooser.APPROVE_OPTION) {
  631. File file = fc.getSelectedFile();
  632. if (fileSelected == false) {
  633. fileSelected = true;
  634. }
  635. animPath.setText(file.getPath());
  636. worker.loadState(animPath.getText());
  637. animModel.clear();
  638. for (int i = 0; i < worker.size(); i++) {
  639. animModel.addElement(worker.getAnimation(i).getName());
  640. }
  641. animList.setModel(animModel);
  642. frameListModel.clear();
  643. frameList.setModel(frameListModel);
  644. }
  645. }
  646. });
  647. save.setBounds(495, 307, 72, 39);
  648. save.setText("Save");
  649. save.setFont(new Font("Dialog", Font.PLAIN, 13));
  650. cp.add(save);
  651. save.addActionListener(new ActionListener() {
  652. public void actionPerformed(ActionEvent evt) {
  653. if (fileSelected == false) {
  654. JFileChooser fc = new JFileChooser();
  655. int ret = fc.showSaveDialog(thisFrame);
  656. if (ret == JFileChooser.APPROVE_OPTION) {
  657. File file = fc.getSelectedFile();
  658. fileSelected = true;
  659. animPath.setText(file.getPath());
  660. worker.saveState(animPath.getText());
  661. }
  662. } else {
  663. worker.saveState(animPath.getText());
  664. }
  665. }
  666. });
  667. saveAs.setBounds(573, 307, 72, 39);
  668. saveAs.setText("Save As");
  669. saveAs.setFont(new Font("Dialog", Font.PLAIN, 13));
  670. cp.add(saveAs);
  671. saveAs.addActionListener(new ActionListener() {
  672. public void actionPerformed(ActionEvent evt) {
  673. JFileChooser fc;
  674. if (fileSelected == true) {
  675. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  676. } else {
  677. fc = new JFileChooser();
  678. }
  679. int ret = fc.showSaveDialog(thisFrame);
  680. if (ret == JFileChooser.APPROVE_OPTION) {
  681. File file = fc.getSelectedFile();
  682. if (fileSelected == false) {
  683. fileSelected = true;
  684. }
  685. animPath.setText(file.getPath());
  686. worker.saveState(animPath.getText());
  687. }
  688. }
  689. });
  690. jComboBox1.setBounds(417, 186, 228, 21);
  691. jComboBox1.setFont(new Font("Dialog", Font.PLAIN, 13));
  692. cp.add(jComboBox1);
  693. upload.setBounds(417, 212, 111, 39);
  694. upload.setText("Upload");
  695. upload.setFont(new Font("Dialog", Font.PLAIN, 13));
  696. cp.add(upload);
  697. upload.addActionListener(new ActionListener() {
  698. public void actionPerformed(ActionEvent evt) {
  699. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  700. // errorMessage("No serial port selected...");
  701. } else {
  702. if (worker.cubeProbeConnected((String) jComboBox1.getSelectedItem())) {
  703. if (worker.cubeSendState((String) jComboBox1.getSelectedItem()) != 0) {
  704. // errorMessage("Could not upload data!");
  705. }
  706. } else {
  707. // errorMessage("Cube does not respond...");
  708. }
  709. }
  710. }
  711. });
  712. download.setBounds(534, 212, 111, 39);
  713. download.setText("Download");
  714. download.setFont(new Font("Dialog", Font.PLAIN, 13));
  715. cp.add(download);
  716. download.addActionListener(new ActionListener() {
  717. public void actionPerformed(ActionEvent evt) {
  718. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  719. // errorMessage("No serial port selected...");
  720. } else {
  721. if (worker.cubeProbeConnected((String) jComboBox1.getSelectedItem())) {
  722. if (worker.cubeGetState((String) jComboBox1.getSelectedItem()) != 0) {
  723. // errorMessage("Could not download data!");
  724. }
  725. } else {
  726. // errorMessage("Cube does not respond...");
  727. }
  728. }
  729. }
  730. });
  731. jLabel4.setBounds536, 208, 112, 20);
  732. jLabel4.setText("Remaining:");
  733. jLabel4.setFont(new Font("Dialog", Font.PLAIN, 13));
  734. cp.add(jLabel4);
  735. frameRemaining.setBounds(520, 134, 49, 20);
  736. frameRemaining.setEditable(false);
  737. frameRemaining.setText(String.valueOf(worker.memoryRemaining()));
  738. frameRemaining.setFont(new Font("Dialog", Font.PLAIN, 13));
  739. cp.add(frameRemaining);
  740. animList.setFont(new Font("Dialog", Font.PLAIN, 13));
  741. frameList.setFont(new Font("Dialog", Font.PLAIN, 13));
  742. previewPanel.setBounds(8, 12, 399, 342);
  743. previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
  744. cp.add(previewPanel);
  745. framePanel.setBounds(331, 360, 321, 246);
  746. framePanel.setBorder(BorderFactory.createTitledBorder("Frame"));
  747. cp.add(framePanel);
  748. animPanel.setBounds(8, 360, 321, 246);
  749. animPanel.setBorder(BorderFactory.createTitledBorder("Animation"));
  750. cp.add(animPanel);
  751. filePanel.setBounds(409, 262, 243, 92);
  752. filePanel.setBorder(BorderFactory.createTitledBorder("Load/Save"));
  753. cp.add(filePanel);
  754. serialPanel.setBounds(409, 167, 243, 92);
  755. serialPanel.setBorder(BorderFactory.createTitledBorder("Serial communication"));
  756. cp.add(serialPanel);
  757. settingsPanel.setBounds(409, 100, 243, 65);
  758. settingsPanel.setBorder(BorderFactory.createTitledBorder("Serial communication"));
  759. cp.add(settingsPanel);
  760. // Ende Komponenten
  761. animList.addListSelectionListener(this);
  762. frameList.addListSelectionListener(this);
  763. setResizable(false);
  764. setVisible(true);
  765. }
  766. public Led3D get3D() {
  767. return ledView;
  768. }
  769. public static void main(String[] args) {
  770. Frame f = new Frame("Cube Control");
  771. Led3D l = f.get3D();
  772. String lastCommand = null;
  773. java.util.Scanner sc = new java.util.Scanner(System.in);
  774. System.out.println("#### Cube Control Debug Console ####");
  775. System.out.println("## Enter a Command ('h' for help) ##");
  776. System.out.print("$> ");
  777. do {
  778. if (sc.hasNextLine()) {
  779. String s = sc.nextLine();
  780. // Up arrow key
  781. if (s.equals("\u001B[A")) {
  782. if (lastCommand != null) {
  783. System.out.println("Last command: " + lastCommand);
  784. s = new String(lastCommand);
  785. } else {
  786. System.out.println("No last command!");
  787. }
  788. }
  789. if (s.equals("p") || (s.equals("print")))
  790. l.printTranslationData();
  791. if (s.equals("q") || s.equals("quit"))
  792. System.exit(0);
  793. if (s.equals("on") || s.equals("1")) {
  794. short[] d = new short[64];
  795. for (int i = 0; i < d.length; i++) {
  796. d[i] = 0xFF;
  797. }
  798. l.setData(d);
  799. System.out.println("All LEDs on now...");
  800. }
  801. if (s.equals("off") || s.equals("0")) {
  802. short[] d = new short[64];
  803. for (int i = 0; i < d.length; i++) {
  804. d[i] = 0x00;
  805. }
  806. l.setData(d);
  807. System.out.println("All LEDs off now...");
  808. }
  809. if (s.equals("r") || s.equals("reset")) {
  810. l.resetView();
  811. }
  812. if (s.startsWith("port ")) {
  813. f.jComboBox1.addItem(s.substring(5));
  814. f.jComboBox1.setSelectedItem(s.substring(5));
  815. }
  816. if (s.startsWith("send ")) {
  817. HelperUtility.openPort((String) f.jComboBox1
  818. .getSelectedItem());
  819. short[] dat = toShortArray(s.substring(5));
  820. HelperUtility.writeData(dat, dat.length);
  821. HelperUtility.closePort();
  822. }
  823. if (s.startsWith("read ")) {
  824. int leng = Integer.parseInt(s.substring(5));
  825. HelperUtility.openPort((String) f.jComboBox1
  826. .getSelectedItem());
  827. short[] data = HelperUtility.readData(leng);
  828. System.out.println(shortToString(data));
  829. HelperUtility.closePort();
  830. }
  831. if (s.startsWith("frames ")) {
  832. int anim = Integer.parseInt(s.substring(7));
  833. if (anim >= f.worker.size()) {
  834. System.out.println("Animation does not exist! Max: " + (f.worker.size() - 1));
  835. } else {
  836. System.out.println("Animation: " + f.worker.getAnimation(anim).getName());
  837. for (int i = 0; i < f.worker.getAnimation(anim).size(); i++) {
  838. AFrame frame = f.worker.getAnimation(anim).getFrame(i);
  839. System.out.println("\tFrame (" + frame.getTime() + "): " + frame.getName());
  840. }
  841. }
  842. }
  843. if (s.equals("a") || s.equals("anims")) {
  844. for (int i = 0; i < f.worker.size(); i++) {
  845. Animation anim = f.worker.getAnimation(i);
  846. System.out.println("\tAnimation: " + anim.getName());
  847. }
  848. }
  849. if (s.equals("s") || s.equals("scan")) {
  850. String[] sPorts = HelperUtility.getPorts();
  851. f.jComboBox1.removeAllItems();
  852. for (int i = 0; i < sPorts.length; i++) {
  853. System.out.println("Port " + i + ": " + sPorts[i]);
  854. f.jComboBox1.addItem(sPorts[i]);
  855. }
  856. }
  857. if (s.equals("h") || (s.equals("help"))) {
  858. System.out.println("Commands:");
  859. System.out
  860. .println("\t'port *name*'\t:\tSet serial port to this");
  861. System.out
  862. .println("\t'send *datas*'\t:\tSend data to serial port");
  863. System.out
  864. .println("\t'read *leng*'\t:\tRead data from serial port");
  865. System.out
  866. .println("\t'scan' / 's'\t:\tScan for serial ports");
  867. System.out
  868. .println("\t'on' / '1'\t:\tToggle all LEDs on");
  869. System.out
  870. .println("\t'off' / '0'\t:\tToggle all LEDs off");
  871. System.out
  872. .println("\t'print' / 'p'\t:\tPrint 3D Translation Matrix Data");
  873. System.out
  874. .println("\t'reset' / 'r'\t:\tReset rotation of cube");
  875. System.out
  876. .println("\t'anims' / 'a'\t:\tPrint animation tree");
  877. System.out
  878. .println("\t'frames *anim*\t:\tPrint frame tree");
  879. System.out
  880. .println("\t'help' / 'h'\t:\tShow this message");
  881. System.out
  882. .println("\t'quit' / 'q'\t:\tExit Cube Control");
  883. }
  884. lastCommand = new String(s);
  885. System.out.print("$> ");
  886. }
  887. } while (true);
  888. }
  889. private static short[] toShortArray(String s) {
  890. char[] d = s.toCharArray();
  891. System.out.println("Length: " + d.length);
  892. short[] r = new short[d.length];
  893. for (int i = 0; i < d.length; i++) {
  894. r[i] = (short) d[i];
  895. }
  896. return r;
  897. }
  898. private static String shortToString(short[] d) {
  899. String s = "";
  900. for (int i = 0; i < d.length; i++) {
  901. s += String.valueOf((char) d[i]);
  902. }
  903. return s;
  904. }
  905. private Point2d convertMousePositionToWorld (int iX, int iY)
  906. {
  907. Point3d eye_pos = new Point3d();
  908. Point3d mousePosn = new Point3d();
  909. //get the eye point and mouse click point
  910. this.cubeCanvas.getCenterEyeInImagePlate(eye_pos);
  911. this.cubeCanvas.getPixelLocationInImagePlate(iX, iY, mousePosn);
  912. //Transform from ImagePlate coordinates to Vworld coordinates
  913. Transform3D motion = new Transform3D();
  914. this.cubeCanvas.getImagePlateToVworld(motion);
  915. motion.transform(eye_pos);
  916. motion.transform(mousePosn);
  917. //calculate the intersection point on Z=0
  918. double dblX = (-eye_pos.z / (mousePosn.z - eye_pos.z)) * (mousePosn.x - eye_pos.x) + eye_pos.x;
  919. double dblY = (-eye_pos.z / (mousePosn.z - eye_pos.z)) * (mousePosn.y - eye_pos.y) + eye_pos.y;
  920. return new Point2d (dblX, dblY);
  921. }
  922. }