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

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