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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. }
  406. frameListModel.set(f, worker.getAnimation(a).getFrame(f).getName());
  407. frameList.setModel(frameListModel);
  408. }
  409. });
  410. // Add Move Animation Up Button
  411. animUp.setBounds(211, 378, 110, 39);
  412. animUp.setText("Move up");
  413. cp.add(animUp);
  414. animUp.addActionListener(new ActionListener() {
  415. public void actionPerformed(ActionEvent evt) {
  416. int i = animList.getSelectedIndex();
  417. if ((i > 0) && (animListModel.getSize() >= 2)) {
  418. Object tmp = animListModel.get(i);
  419. animListModel.set(i, animListModel.get(i - 1));
  420. animListModel.set(i - 1, tmp);
  421. animList.setSelectedIndex(i - 1);
  422. worker.moveAnimationUp(i);
  423. }
  424. }
  425. });
  426. // Add Move Animation Down Button
  427. animDown.setBounds(211, 558, 110, 39);
  428. animDown.setText("Move down");
  429. cp.add(animDown);
  430. animDown.addActionListener(new ActionListener() {
  431. public void actionPerformed(ActionEvent evt) {
  432. int i = animList.getSelectedIndex();
  433. if ((i >= 0) && (animListModel.getSize() >= 2) && (i < (animListModel.getSize() - 1))) {
  434. Object tmp = animListModel.get(i);
  435. animListModel.set(i, animListModel.get(i + 1));
  436. animListModel.set(i + 1, tmp);
  437. animList.setSelectedIndex(i + 1);
  438. worker.moveAnimationDown(i);
  439. }
  440. }
  441. });
  442. // Add Rename Animation Button
  443. animRename.setBounds(211, 513, 110, 39);
  444. animRename.setText("Rename");
  445. cp.add(animRename);
  446. animRename.addActionListener(new ActionListener() {
  447. public void actionPerformed(ActionEvent evt) {
  448. int a = animList.getSelectedIndex();
  449. if (a < 0) {
  450. errorMessage("Select an animation!");
  451. return;
  452. }
  453. String newName = askString("Rename", "Rename " + animList.getSelectedValue() + "?");
  454. if (!newName.equals("")) {
  455. worker.getAnimation(a).setName(newName);
  456. }
  457. animListModel.set(a, worker.getAnimation(a).getName());
  458. animList.setModel(animListModel);
  459. }
  460. });
  461. // Add button for adding animations
  462. animAdd.setBounds(211, 423, 110, 39);
  463. animAdd.setText("Add");
  464. cp.add(animAdd);
  465. animAdd.addActionListener(new ActionListener() {
  466. public void actionPerformed(ActionEvent evt) {
  467. if (worker.addAnimation() == -1) {
  468. errorMessage("Could not add animation!");
  469. } else {
  470. animListModel.clear();
  471. for (int i = 0; i < worker.size(); i++) {
  472. animListModel.add(i, worker.getAnimation(i).getName());
  473. }
  474. animList.setModel(animListModel);
  475. }
  476. }
  477. });
  478. // Add Animation remove button
  479. animRemove.setBounds(211, 468, 110, 39);
  480. animRemove.setText("Remove");
  481. cp.add(animRemove);
  482. animRemove.addActionListener(new ActionListener() {
  483. public void actionPerformed(ActionEvent evt) {
  484. if (animList.getSelectedIndex() == -1) {
  485. errorMessage("Select an animation.");
  486. } else {
  487. worker.removeAnimation(animList.getSelectedIndex());
  488. animListModel.removeElementAt(animList.getSelectedIndex());
  489. animList.setModel(animListModel);
  490. }
  491. }
  492. });
  493. fullScreenButton.setText("Fullscreen");
  494. fullScreenButton.setBounds(18, 312, 134, 35);
  495. cp.add(fullScreenButton);
  496. fullScreenButton.addActionListener(new ActionListener() {
  497. public void actionPerformed(ActionEvent evt) {
  498. ledView.enterFullscreen();
  499. setLocation(0,0);
  500. setSize(700, 700);
  501. int w = Toolkit.getDefaultToolkit().getScreenSize().width;
  502. int h = Toolkit.getDefaultToolkit().getScreenSize().height;
  503. setSize(w - 5, h - 30);
  504. playAnimationFullscreen.setVisible(true);
  505. cubeCanvas.setBounds(0, 0, w - 5, h - 80);
  506. }
  507. });
  508. exitButton = new JButton("Exit Fullscreen");
  509. exitButton.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-150, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  510. cp.add(exitButton);
  511. exitButton.addActionListener(new ActionListener() {
  512. public void actionPerformed(ActionEvent evt) {
  513. playAnimationFullscreen.setVisible(false);
  514. setLocation(0,0);
  515. setSize(672, 656);
  516. ledView.leaveFullscreen();
  517. cubeCanvas.setBounds(18, 31, 275, 275);
  518. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  519. int x = (d.width - getSize().width) / 2;
  520. int y = (d.height - getSize().height) / 2;
  521. setLocation(x, y);
  522. }
  523. });
  524. playAnimation.setText("Play");
  525. playAnimation.setBounds(159, 312, 134, 35);
  526. cp.add(playAnimation);
  527. playAnimation.addActionListener(new ActionListener() {
  528. public void actionPerformed(ActionEvent evt){
  529. if (animList.getSelectedIndex() == -1) {
  530. errorMessage("Please select an animation.");
  531. } else if (frameList.getSelectedIndex() == -1) {
  532. errorMessage("Please select a Frame.");
  533. } else {
  534. for (int i = 0; i < frameList.getModel().getSize(); i++){
  535. frameList.setSelectedIndex(i);
  536. long time1 = (long) worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  537. long time = (long) (((time1+1) * 1000) / 24);
  538. System.out.println("Wert: " + time1 + " Zeit: " + time);
  539. try {
  540. Thread.sleep(time);
  541. } catch (Exception e) {
  542. System.out.println(e);
  543. }
  544. }
  545. }
  546. }
  547. });
  548. playAnimationFullscreen.setText("Play");
  549. playAnimationFullscreen.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-310, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  550. playAnimationFullscreen.setVisible(false);
  551. cp.add(playAnimationFullscreen);
  552. playAnimationFullscreen.addActionListener(new ActionListener() {
  553. public void actionPerformed(ActionEvent evt){
  554. if (animList.getSelectedIndex() == -1) {
  555. errorMessage("Please select an animation.");
  556. } else if (frameList.getSelectedIndex() == -1) {
  557. errorMessage("Please select a Frame.");
  558. } else {
  559. for (int i = 0; i < frameList.getModel().getSize(); i++){
  560. frameList.setSelectedIndex(i);
  561. long time1 = (long) worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  562. long time = (long) (((time1+1) * 1000) / 24);
  563. System.out.println("Wert: " + time1 + " Zeit: " + time);
  564. try {
  565. Thread.sleep(time);
  566. } catch (Exception e) {
  567. System.out.println(e);
  568. }
  569. }
  570. }
  571. }
  572. });
  573. frameDuration.setBounds(462, 129, 65, 20);
  574. frameDuration.setText("Save");
  575. cp.add(frameDuration);
  576. frameDuration.addActionListener(new ActionListener() {
  577. public void actionPerformed(ActionEvent evt) {
  578. if (frameLengthText.getText().equals("0")) {
  579. errorMessage("0 is not a valid value!");
  580. frameLengthText.setText("1");
  581. } else if (Integer.parseInt(frameLengthText.getText()) > 256) {
  582. errorMessage("Value too high. Max: 256");
  583. frameLengthText.setText("256");
  584. return;
  585. } else {
  586. if (animList.getSelectedIndex() == -1) {
  587. errorMessage("Please select an animation!");
  588. return;
  589. }
  590. if (frameList.getSelectedIndex() == -1) {
  591. errorMessage("Please select a Frame!");
  592. return;
  593. }
  594. durationSlider.setValue(Integer.parseInt(frameLengthText.getText()));
  595. worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).setTime((short)(Integer.parseInt(frameLengthText.getText())));
  596. }
  597. }
  598. });
  599. animPath.setBounds(417, 281, 228, 20);
  600. animPath.setEditable(false);
  601. animPath.setText("Select an animation file...");
  602. cp.add(animPath);
  603. load.setBounds(417, 307, 72, 39);
  604. load.setText("Load");
  605. cp.add(load);
  606. load.addActionListener(new ActionListener() {
  607. public void actionPerformed(ActionEvent evt) {
  608. JFileChooser fc = new JFileChooser();
  609. int ret = fc.showOpenDialog(thisFrame);
  610. if (ret == JFileChooser.APPROVE_OPTION) {
  611. File file = fc.getSelectedFile();
  612. if (fileSelected == false) {
  613. fileSelected = true;
  614. }
  615. animPath.setText(file.getPath());
  616. worker.loadState(animPath.getText());
  617. animListModel.clear();
  618. for (int i = 0; i < worker.size(); i++) {
  619. animListModel.addElement(worker.getAnimation(i).getName());
  620. }
  621. animList.setModel(animListModel);
  622. animList.setSelectedIndex(0);
  623. rebuildFrameList();
  624. }
  625. }
  626. });
  627. save.setBounds(491, 307, 72, 39);
  628. save.setText("Save");
  629. cp.add(save);
  630. save.addActionListener(new ActionListener() {
  631. public void actionPerformed(ActionEvent evt) {
  632. if (fileSelected == false) {
  633. JFileChooser fc = new JFileChooser();
  634. int ret = fc.showSaveDialog(thisFrame);
  635. if (ret == JFileChooser.APPROVE_OPTION) {
  636. File file = fc.getSelectedFile();
  637. fileSelected = true;
  638. animPath.setText(file.getPath());
  639. worker.saveState(animPath.getText());
  640. }
  641. } else {
  642. worker.saveState(animPath.getText());
  643. }
  644. }
  645. });
  646. saveAs.setBounds(565, 307, 82, 39);
  647. saveAs.setText("Save As");
  648. cp.add(saveAs);
  649. saveAs.addActionListener(new ActionListener() {
  650. public void actionPerformed(ActionEvent evt) {
  651. JFileChooser fc;
  652. if (fileSelected == true) {
  653. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  654. } else {
  655. fc = new JFileChooser();
  656. }
  657. int ret = fc.showSaveDialog(thisFrame);
  658. if (ret == JFileChooser.APPROVE_OPTION) {
  659. File file = fc.getSelectedFile();
  660. if (fileSelected == false) {
  661. fileSelected = true;
  662. }
  663. animPath.setText(file.getPath());
  664. worker.saveState(animPath.getText());
  665. }
  666. }
  667. });
  668. serialPortSelector.setBounds(417, 182, 228, 25);
  669. cp.add(serialPortSelector);
  670. upload.setBounds(417, 212, 111, 39);
  671. upload.setText("Upload");
  672. cp.add(upload);
  673. upload.addActionListener(new ActionListener() {
  674. public void actionPerformed(ActionEvent evt) {
  675. // Error messages are generated by worker
  676. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  677. worker.cubeSendState((String) serialPortSelector.getSelectedItem());
  678. }
  679. }
  680. });
  681. download.setBounds(534, 212, 111, 39);
  682. download.setText("Download");
  683. cp.add(download);
  684. download.addActionListener(new ActionListener() {
  685. public void actionPerformed(ActionEvent evt) {
  686. // Error messages are generated by worker
  687. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  688. worker.cubeGetState((String) serialPortSelector.getSelectedItem());
  689. }
  690. }
  691. });
  692. frameLengthLabel.setBounds(420, 97, 130, 13);
  693. frameLengthLabel.setText("Time (1/24 sec)");
  694. cp.add(frameLengthLabel);
  695. frameLengthText.setBounds(419, 129, 40, 20);
  696. cp.add(frameLengthText);
  697. remainingLabel.setBounds(530, 113, 90, 13);
  698. remainingLabel.setText("Remaining:");
  699. cp.add(remainingLabel);
  700. durationSlider.setBounds(417, 108, 115, 23);
  701. durationSlider.addChangeListener(this);
  702. cp.add(durationSlider);
  703. frameRemaining.setBounds(530, 129, 49, 20);
  704. frameRemaining.setEditable(false);
  705. frameRemaining.setText(String.valueOf(worker.memoryRemaining()));
  706. cp.add(frameRemaining);
  707. previewPanel.setBounds(8, 12, 399, 342);
  708. previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
  709. cp.add(previewPanel);
  710. framePanel.setBounds(331, 360, 321, 246);
  711. framePanel.setBorder(BorderFactory.createTitledBorder("Frame"));
  712. cp.add(framePanel);
  713. animPanel.setBounds(8, 360, 321, 246);
  714. animPanel.setBorder(BorderFactory.createTitledBorder("Animation"));
  715. cp.add(animPanel);
  716. filePanel.setBounds(409, 262, 243, 92);
  717. filePanel.setBorder(BorderFactory.createTitledBorder("Animation File"));
  718. cp.add(filePanel);
  719. serialPanel.setBounds(409, 160, 243, 99);
  720. serialPanel.setBorder(BorderFactory.createTitledBorder("Serial communication"));
  721. cp.add(serialPanel);
  722. settingsPanel.setBounds(409, 75, 243, 82);
  723. settingsPanel.setBorder(BorderFactory.createTitledBorder("Frame duration"));
  724. cp.add(settingsPanel);
  725. logoText.setText("CubeControl");
  726. logoText.setBounds(415, 10, 243, 65);
  727. logoText.setFont(new Font("Default", Font.BOLD, 38));
  728. cp.add(logoText);
  729. // Ende Komponenten
  730. animList.addListSelectionListener(this);
  731. frameList.addListSelectionListener(this);
  732. setResizable(false);
  733. setVisible(true);
  734. valueChanged(new ListSelectionEvent(animList, 0, 0, false));
  735. }
  736. public Led3D get3D() {
  737. return ledView;
  738. }
  739. public static void main(String[] args) {
  740. Frame f = new Frame("Cube Control");
  741. Led3D l = f.get3D();
  742. String lastCommand = null;
  743. java.util.Scanner sc = new java.util.Scanner(System.in);
  744. System.out.println("#### Cube Control Debug Console ####");
  745. System.out.println("## Enter a Command ('h' for help) ##");
  746. System.out.print("$> ");
  747. do {
  748. if (sc.hasNextLine()) {
  749. String s = sc.nextLine();
  750. // Up arrow key
  751. if (s.equals("\u001B[A")) {
  752. if (lastCommand != null) {
  753. System.out.println("Last command: " + lastCommand);
  754. s = new String(lastCommand);
  755. } else {
  756. System.out.println("No last command!");
  757. }
  758. }
  759. if (s.equals("p") || (s.equals("print")))
  760. l.printTranslationData();
  761. if (s.equals("q") || s.equals("quit"))
  762. System.exit(0);
  763. if (s.equals("r") || s.equals("reset"))
  764. l.resetView();
  765. if (s.equals("on") || s.equals("1")) {
  766. short[] d = new short[64];
  767. for (int i = 0; i < d.length; i++) {
  768. d[i] = 0xFF;
  769. }
  770. l.setData(d);
  771. System.out.println("All LEDs on now...");
  772. }
  773. if (s.equals("off") || s.equals("0")) {
  774. short[] d = new short[64];
  775. for (int i = 0; i < d.length; i++) {
  776. d[i] = 0x00;
  777. }
  778. l.setData(d);
  779. System.out.println("All LEDs off now...");
  780. }
  781. if (s.startsWith("port ")) {
  782. f.serialPortSelector.addItem(s.substring(5));
  783. f.serialPortSelector.setSelectedItem(s.substring(5));
  784. }
  785. if (s.startsWith("send ")) {
  786. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  787. short[] dat = toShortArray(s.substring(5));
  788. HelperUtility.writeData(dat, dat.length);
  789. HelperUtility.closePort();
  790. }
  791. if (s.startsWith("read ")) {
  792. int leng = Integer.parseInt(s.substring(5));
  793. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  794. short[] data = HelperUtility.readData(leng);
  795. System.out.println(shortToString(data));
  796. HelperUtility.closePort();
  797. }
  798. if (s.startsWith("frames ")) {
  799. int anim = Integer.parseInt(s.substring(7));
  800. if (anim >= f.worker.size()) {
  801. System.out.println("Animation does not exist! Max: " + (f.worker.size() - 1));
  802. } else {
  803. System.out.println("Animation: " + f.worker.getAnimation(anim).getName());
  804. for (int i = 0; i < f.worker.getAnimation(anim).size(); i++) {
  805. AFrame frame = f.worker.getAnimation(anim).getFrame(i);
  806. System.out.println("\tFrame (" + frame.getTime() + "): " + frame.getName());
  807. }
  808. }
  809. }
  810. if (s.equals("a") || s.equals("anims")) {
  811. for (int i = 0; i < f.worker.size(); i++) {
  812. Animation anim = f.worker.getAnimation(i);
  813. System.out.println("\tAnimation: " + anim.getName());
  814. }
  815. }
  816. if (s.equals("s") || s.equals("scan")) {
  817. String[] sPorts = HelperUtility.getPorts();
  818. f.serialPortSelector.removeAllItems();
  819. for (int i = 0; i < sPorts.length; i++) {
  820. System.out.println("Port " + i + ": " + sPorts[i]);
  821. f.serialPortSelector.addItem(sPorts[i]);
  822. }
  823. }
  824. if (s.equals("h") || (s.equals("help"))) {
  825. System.out.println("Commands:");
  826. System.out.println("\t'port *name*'\t:\tSet serial port to this");
  827. System.out.println("\t'send *datas*'\t:\tSend data to serial port");
  828. System.out.println("\t'read *leng*'\t:\tRead data from serial port");
  829. System.out.println("\t'scan' / 's'\t:\tScan for serial ports");
  830. System.out.println("\t'on' / '1'\t:\tToggle all LEDs on");
  831. System.out.println("\t'off'/ '0'\t:\tToggle all LEDs off");
  832. System.out.println("\t'print'/ 'p'\t:\tPrint 3D Translation Matrix Data");
  833. System.out.println("\t'reset'/ 'r'\t:\tReset rotation of cube");
  834. System.out.println("\t'anims'/ 'a'\t:\tPrint animation tree");
  835. System.out.println("\t'frames *anim*\t:\tPrint frame tree");
  836. System.out.println("\t'help' / 'h'\t:\tShow this message");
  837. System.out.println("\t'quit' / 'q'\t:\tExit Cube Control");
  838. }
  839. lastCommand = new String(s);
  840. System.out.print("$> ");
  841. }
  842. } while (true);
  843. }
  844. private static short[] toShortArray(String s) {
  845. char[] d = s.toCharArray();
  846. System.out.println("Length: " + d.length);
  847. short[] r = new short[d.length];
  848. for (int i = 0; i < d.length; i++) {
  849. r[i] = (short) d[i];
  850. }
  851. return r;
  852. }
  853. private static String shortToString(short[] d) {
  854. String s = "";
  855. for (int i = 0; i < d.length; i++) {
  856. s += String.valueOf((char) d[i]);
  857. }
  858. return s;
  859. }
  860. }