Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Frame.java 32KB

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