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

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