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

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