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

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