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

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