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

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