Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Frame.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. long time1 = (long) worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  524. long time = (long) (((time1+1) * 1000) / 24);
  525. System.out.println("Wert: " + time1 + " Zeit: " + time);
  526. try {
  527. Thread.sleep(time);
  528. } catch (Exception e) {
  529. System.out.println(e);
  530. }
  531. }
  532. }
  533. }
  534. });
  535. playAnimationFullscreen.setText("Play");
  536. playAnimationFullscreen.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-310, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  537. playAnimationFullscreen.setFont(font);
  538. playAnimationFullscreen.setVisible(false);
  539. cp.add(playAnimationFullscreen);
  540. playAnimationFullscreen.addActionListener(new ActionListener() {
  541. public void actionPerformed(ActionEvent evt){
  542. if (animList.getSelectedIndex() == -1) {
  543. errorMessage("Please select an animation.");
  544. } else if (frameList.getSelectedIndex() == -1) {
  545. errorMessage("Please select a Frame.");
  546. } else {
  547. for (int i = 0; i < frameList.getModel().getSize(); i++){
  548. frameList.setSelectedIndex(i);
  549. short time1 = worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  550. long time = (long) (((time1+1) * 1/24) * 1000);
  551. try {
  552. Thread.sleep(time);
  553. } catch(Exception e) {
  554. System.out.println(e);
  555. }
  556. }
  557. }
  558. }
  559. });
  560. frameDuration.setBounds(462, 134, 55, 20);
  561. frameDuration.setText("OK");
  562. frameDuration.setFont(font);
  563. cp.add(frameDuration);
  564. frameDuration.addActionListener(new ActionListener() {
  565. public void actionPerformed(ActionEvent evt) {
  566. if (frameLengthText.getText().equals("0")) {
  567. errorMessage("0 is not a valid value!");
  568. frameLengthText.setText("1");
  569. } else if (Integer.parseInt(frameLengthText.getText()) > 256) {
  570. errorMessage("Value too high. Max: 256");
  571. frameLengthText.setText("256");
  572. return;
  573. } else {
  574. if (animList.getSelectedIndex() == -1) {
  575. errorMessage("Please select an animation!");
  576. return;
  577. }
  578. if (frameList.getSelectedIndex() == -1) {
  579. errorMessage("Please select a Frame!");
  580. return;
  581. }
  582. worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).setTime((byte)(Integer.parseInt(frameLengthText.getText())));
  583. }
  584. }
  585. });
  586. animPath.setBounds(417, 281, 228, 20);
  587. animPath.setEditable(false);
  588. animPath.setText("Load/Save an animation file...");
  589. animPath.setFont(font);
  590. cp.add(animPath);
  591. load.setBounds(417, 307, 72, 39);
  592. load.setText("Load");
  593. load.setFont(font);
  594. cp.add(load);
  595. load.addActionListener(new ActionListener() {
  596. public void actionPerformed(ActionEvent evt) {
  597. JFileChooser fc = new JFileChooser();
  598. int ret = fc.showOpenDialog(thisFrame);
  599. if (ret == JFileChooser.APPROVE_OPTION) {
  600. File file = fc.getSelectedFile();
  601. if (fileSelected == false) {
  602. fileSelected = true;
  603. }
  604. animPath.setText(file.getPath());
  605. worker.loadState(animPath.getText());
  606. animModel.clear();
  607. for (int i = 0; i < worker.size(); i++) {
  608. animModel.addElement(worker.getAnimation(i).getName());
  609. }
  610. animList.setModel(animModel);
  611. frameListModel.clear();
  612. frameList.setModel(frameListModel);
  613. }
  614. }
  615. });
  616. save.setBounds(491, 307, 72, 39);
  617. save.setText("Save");
  618. save.setFont(font);
  619. cp.add(save);
  620. save.addActionListener(new ActionListener() {
  621. public void actionPerformed(ActionEvent evt) {
  622. if (fileSelected == false) {
  623. JFileChooser fc = new JFileChooser();
  624. int ret = fc.showSaveDialog(thisFrame);
  625. if (ret == JFileChooser.APPROVE_OPTION) {
  626. File file = fc.getSelectedFile();
  627. fileSelected = true;
  628. animPath.setText(file.getPath());
  629. worker.saveState(animPath.getText());
  630. }
  631. } else {
  632. worker.saveState(animPath.getText());
  633. }
  634. }
  635. });
  636. saveAs.setBounds(565, 307, 82, 39);
  637. saveAs.setText("Save As");
  638. saveAs.setFont(font);
  639. cp.add(saveAs);
  640. saveAs.addActionListener(new ActionListener() {
  641. public void actionPerformed(ActionEvent evt) {
  642. JFileChooser fc;
  643. if (fileSelected == true) {
  644. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  645. } else {
  646. fc = new JFileChooser();
  647. }
  648. int ret = fc.showSaveDialog(thisFrame);
  649. if (ret == JFileChooser.APPROVE_OPTION) {
  650. File file = fc.getSelectedFile();
  651. if (fileSelected == false) {
  652. fileSelected = true;
  653. }
  654. animPath.setText(file.getPath());
  655. worker.saveState(animPath.getText());
  656. }
  657. }
  658. });
  659. serialPortSelector.setBounds(417, 186, 228, 21);
  660. serialPortSelector.setFont(font);
  661. cp.add(serialPortSelector);
  662. upload.setBounds(417, 212, 111, 39);
  663. upload.setText("Upload");
  664. upload.setFont(font);
  665. cp.add(upload);
  666. upload.addActionListener(new ActionListener() {
  667. public void actionPerformed(ActionEvent evt) {
  668. // Error messages are generated by worker
  669. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  670. worker.cubeSendState((String) serialPortSelector.getSelectedItem());
  671. }
  672. }
  673. });
  674. download.setBounds(534, 212, 111, 39);
  675. download.setText("Download");
  676. download.setFont(font);
  677. cp.add(download);
  678. download.addActionListener(new ActionListener() {
  679. public void actionPerformed(ActionEvent evt) {
  680. // Error messages are generated by worker
  681. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  682. worker.cubeGetState((String) serialPortSelector.getSelectedItem());
  683. }
  684. }
  685. });
  686. remainingLabel.setBounds(530, 118, 90, 13);
  687. remainingLabel.setText("Remaining:");
  688. remainingLabel.setFont(font);
  689. cp.add(remainingLabel);
  690. frameRemaining.setBounds(530, 134, 49, 20);
  691. frameRemaining.setEditable(false);
  692. frameRemaining.setText(String.valueOf(worker.memoryRemaining()));
  693. frameRemaining.setFont(font);
  694. cp.add(frameRemaining);
  695. animList.setFont(font);
  696. frameList.setFont(font);
  697. previewPanel.setBounds(8, 12, 399, 342);
  698. previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
  699. cp.add(previewPanel);
  700. framePanel.setBounds(331, 360, 321, 246);
  701. framePanel.setBorder(BorderFactory.createTitledBorder("Frame"));
  702. cp.add(framePanel);
  703. animPanel.setBounds(8, 360, 321, 246);
  704. animPanel.setBorder(BorderFactory.createTitledBorder("Animation"));
  705. cp.add(animPanel);
  706. filePanel.setBounds(409, 262, 243, 92);
  707. filePanel.setBorder(BorderFactory.createTitledBorder("Load/Save"));
  708. cp.add(filePanel);
  709. serialPanel.setBounds(409, 167, 243, 92);
  710. serialPanel.setBorder(BorderFactory.createTitledBorder("Serial communication"));
  711. cp.add(serialPanel);
  712. settingsPanel.setBounds(409, 100, 243, 65);
  713. settingsPanel.setBorder(BorderFactory.createTitledBorder("Frame duration"));
  714. cp.add(settingsPanel);
  715. // Ende Komponenten
  716. animList.addListSelectionListener(this);
  717. frameList.addListSelectionListener(this);
  718. setResizable(false);
  719. setVisible(true);
  720. }
  721. public Led3D get3D() {
  722. return ledView;
  723. }
  724. public static void main(String[] args) {
  725. Frame f = new Frame("Cube Control");
  726. Led3D l = f.get3D();
  727. String lastCommand = null;
  728. java.util.Scanner sc = new java.util.Scanner(System.in);
  729. System.out.println("#### Cube Control Debug Console ####");
  730. System.out.println("## Enter a Command ('h' for help) ##");
  731. System.out.print("$> ");
  732. do {
  733. if (sc.hasNextLine()) {
  734. String s = sc.nextLine();
  735. // Up arrow key
  736. if (s.equals("\u001B[A")) {
  737. if (lastCommand != null) {
  738. System.out.println("Last command: " + lastCommand);
  739. s = new String(lastCommand);
  740. } else {
  741. System.out.println("No last command!");
  742. }
  743. }
  744. if (s.equals("p") || (s.equals("print")))
  745. l.printTranslationData();
  746. if (s.equals("q") || s.equals("quit"))
  747. System.exit(0);
  748. if (s.equals("r") || s.equals("reset"))
  749. l.resetView();
  750. if (s.equals("on") || s.equals("1")) {
  751. short[] d = new short[64];
  752. for (int i = 0; i < d.length; i++) {
  753. d[i] = 0xFF;
  754. }
  755. l.setData(d);
  756. System.out.println("All LEDs on now...");
  757. }
  758. if (s.equals("off") || s.equals("0")) {
  759. short[] d = new short[64];
  760. for (int i = 0; i < d.length; i++) {
  761. d[i] = 0x00;
  762. }
  763. l.setData(d);
  764. System.out.println("All LEDs off now...");
  765. }
  766. if (s.startsWith("port ")) {
  767. f.serialPortSelector.addItem(s.substring(5));
  768. f.serialPortSelector.setSelectedItem(s.substring(5));
  769. }
  770. if (s.startsWith("send ")) {
  771. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  772. short[] dat = toShortArray(s.substring(5));
  773. HelperUtility.writeData(dat, dat.length);
  774. HelperUtility.closePort();
  775. }
  776. if (s.startsWith("read ")) {
  777. int leng = Integer.parseInt(s.substring(5));
  778. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  779. short[] data = HelperUtility.readData(leng);
  780. System.out.println(shortToString(data));
  781. HelperUtility.closePort();
  782. }
  783. if (s.startsWith("frames ")) {
  784. int anim = Integer.parseInt(s.substring(7));
  785. if (anim >= f.worker.size()) {
  786. System.out.println("Animation does not exist! Max: " + (f.worker.size() - 1));
  787. } else {
  788. System.out.println("Animation: " + f.worker.getAnimation(anim).getName());
  789. for (int i = 0; i < f.worker.getAnimation(anim).size(); i++) {
  790. AFrame frame = f.worker.getAnimation(anim).getFrame(i);
  791. System.out.println("\tFrame (" + frame.getTime() + "): " + frame.getName());
  792. }
  793. }
  794. }
  795. if (s.equals("a") || s.equals("anims")) {
  796. for (int i = 0; i < f.worker.size(); i++) {
  797. Animation anim = f.worker.getAnimation(i);
  798. System.out.println("\tAnimation: " + anim.getName());
  799. }
  800. }
  801. if (s.equals("s") || s.equals("scan")) {
  802. String[] sPorts = HelperUtility.getPorts();
  803. f.serialPortSelector.removeAllItems();
  804. for (int i = 0; i < sPorts.length; i++) {
  805. System.out.println("Port " + i + ": " + sPorts[i]);
  806. f.serialPortSelector.addItem(sPorts[i]);
  807. }
  808. }
  809. if (s.equals("h") || (s.equals("help"))) {
  810. System.out.println("Commands:");
  811. System.out.println("\t'port *name*'\t:\tSet serial port to this");
  812. System.out.println("\t'send *datas*'\t:\tSend data to serial port");
  813. System.out.println("\t'read *leng*'\t:\tRead data from serial port");
  814. System.out.println("\t'scan' / 's'\t:\tScan for serial ports");
  815. System.out.println("\t'on' / '1'\t:\tToggle all LEDs on");
  816. System.out.println("\t'off'/ '0'\t:\tToggle all LEDs off");
  817. System.out.println("\t'print'/ 'p'\t:\tPrint 3D Translation Matrix Data");
  818. System.out.println("\t'reset'/ 'r'\t:\tReset rotation of cube");
  819. System.out.println("\t'anims'/ 'a'\t:\tPrint animation tree");
  820. System.out.println("\t'frames *anim*\t:\tPrint frame tree");
  821. System.out.println("\t'help' / 'h'\t:\tShow this message");
  822. System.out.println("\t'quit' / 'q'\t:\tExit Cube Control");
  823. }
  824. lastCommand = new String(s);
  825. System.out.print("$> ");
  826. }
  827. } while (true);
  828. }
  829. private static short[] toShortArray(String s) {
  830. char[] d = s.toCharArray();
  831. System.out.println("Length: " + d.length);
  832. short[] r = new short[d.length];
  833. for (int i = 0; i < d.length; i++) {
  834. r[i] = (short) d[i];
  835. }
  836. return r;
  837. }
  838. private static String shortToString(short[] d) {
  839. String s = "";
  840. for (int i = 0; i < d.length; i++) {
  841. s += String.valueOf((char) d[i]);
  842. }
  843. return s;
  844. }
  845. }