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 33KB

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