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

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