Simple single-color 8x8x8 LED Cube with AVRs
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. short time1 = worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  562. long time = (long) (((time1+1) * 1/24) * 1000);
  563. try {
  564. Thread.sleep(time);
  565. } catch(Exception e) {
  566. System.out.println(e);
  567. }
  568. }
  569. }
  570. }
  571. });
  572. frameDuration.setBounds(462, 129, 65, 20);
  573. frameDuration.setText("Save");
  574. cp.add(frameDuration);
  575. frameDuration.addActionListener(new ActionListener() {
  576. public void actionPerformed(ActionEvent evt) {
  577. if (frameLengthText.getText().equals("0")) {
  578. errorMessage("0 is not a valid value!");
  579. frameLengthText.setText("1");
  580. } else if (Integer.parseInt(frameLengthText.getText()) > 256) {
  581. errorMessage("Value too high. Max: 256");
  582. frameLengthText.setText("256");
  583. return;
  584. } else {
  585. if (animList.getSelectedIndex() == -1) {
  586. errorMessage("Please select an animation!");
  587. return;
  588. }
  589. if (frameList.getSelectedIndex() == -1) {
  590. errorMessage("Please select a Frame!");
  591. return;
  592. }
  593. durationSlider.setValue(Integer.parseInt(frameLengthText.getText()));
  594. worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).setTime((short)(Integer.parseInt(frameLengthText.getText())));
  595. }
  596. }
  597. });
  598. animPath.setBounds(417, 281, 228, 20);
  599. animPath.setEditable(false);
  600. animPath.setText("Select an animation file...");
  601. cp.add(animPath);
  602. load.setBounds(417, 307, 72, 39);
  603. load.setText("Load");
  604. cp.add(load);
  605. load.addActionListener(new ActionListener() {
  606. public void actionPerformed(ActionEvent evt) {
  607. JFileChooser fc = new JFileChooser();
  608. int ret = fc.showOpenDialog(thisFrame);
  609. if (ret == JFileChooser.APPROVE_OPTION) {
  610. File file = fc.getSelectedFile();
  611. if (fileSelected == false) {
  612. fileSelected = true;
  613. }
  614. animPath.setText(file.getPath());
  615. worker.loadState(animPath.getText());
  616. animListModel.clear();
  617. for (int i = 0; i < worker.size(); i++) {
  618. animListModel.addElement(worker.getAnimation(i).getName());
  619. }
  620. animList.setModel(animListModel);
  621. animList.setSelectedIndex(0);
  622. rebuildFrameList();
  623. }
  624. }
  625. });
  626. save.setBounds(491, 307, 72, 39);
  627. save.setText("Save");
  628. cp.add(save);
  629. save.addActionListener(new ActionListener() {
  630. public void actionPerformed(ActionEvent evt) {
  631. if (fileSelected == false) {
  632. JFileChooser fc = new JFileChooser();
  633. int ret = fc.showSaveDialog(thisFrame);
  634. if (ret == JFileChooser.APPROVE_OPTION) {
  635. File file = fc.getSelectedFile();
  636. fileSelected = true;
  637. animPath.setText(file.getPath());
  638. worker.saveState(animPath.getText());
  639. }
  640. } else {
  641. worker.saveState(animPath.getText());
  642. }
  643. }
  644. });
  645. saveAs.setBounds(565, 307, 82, 39);
  646. saveAs.setText("Save As");
  647. cp.add(saveAs);
  648. saveAs.addActionListener(new ActionListener() {
  649. public void actionPerformed(ActionEvent evt) {
  650. JFileChooser fc;
  651. if (fileSelected == true) {
  652. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  653. } else {
  654. fc = new JFileChooser();
  655. }
  656. int ret = fc.showSaveDialog(thisFrame);
  657. if (ret == JFileChooser.APPROVE_OPTION) {
  658. File file = fc.getSelectedFile();
  659. if (fileSelected == false) {
  660. fileSelected = true;
  661. }
  662. animPath.setText(file.getPath());
  663. worker.saveState(animPath.getText());
  664. }
  665. }
  666. });
  667. serialPortSelector.setBounds(417, 182, 228, 25);
  668. cp.add(serialPortSelector);
  669. upload.setBounds(417, 212, 111, 39);
  670. upload.setText("Upload");
  671. cp.add(upload);
  672. upload.addActionListener(new ActionListener() {
  673. public void actionPerformed(ActionEvent evt) {
  674. // Error messages are generated by worker
  675. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  676. worker.cubeSendState((String) serialPortSelector.getSelectedItem());
  677. }
  678. }
  679. });
  680. download.setBounds(534, 212, 111, 39);
  681. download.setText("Download");
  682. cp.add(download);
  683. download.addActionListener(new ActionListener() {
  684. public void actionPerformed(ActionEvent evt) {
  685. // Error messages are generated by worker
  686. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  687. worker.cubeGetState((String) serialPortSelector.getSelectedItem());
  688. }
  689. }
  690. });
  691. frameLengthLabel.setBounds(420, 97, 130, 13);
  692. frameLengthLabel.setText("Time (1/24 sec)");
  693. cp.add(frameLengthLabel);
  694. frameLengthText.setBounds(419, 129, 40, 20);
  695. cp.add(frameLengthText);
  696. remainingLabel.setBounds(530, 113, 90, 13);
  697. remainingLabel.setText("Remaining:");
  698. cp.add(remainingLabel);
  699. durationSlider.setBounds(417, 108, 115, 23);
  700. durationSlider.addChangeListener(this);
  701. cp.add(durationSlider);
  702. frameRemaining.setBounds(530, 129, 49, 20);
  703. frameRemaining.setEditable(false);
  704. frameRemaining.setText(String.valueOf(worker.memoryRemaining()));
  705. cp.add(frameRemaining);
  706. previewPanel.setBounds(8, 12, 399, 342);
  707. previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
  708. cp.add(previewPanel);
  709. framePanel.setBounds(331, 360, 321, 246);
  710. framePanel.setBorder(BorderFactory.createTitledBorder("Frame"));
  711. cp.add(framePanel);
  712. animPanel.setBounds(8, 360, 321, 246);
  713. animPanel.setBorder(BorderFactory.createTitledBorder("Animation"));
  714. cp.add(animPanel);
  715. filePanel.setBounds(409, 262, 243, 92);
  716. filePanel.setBorder(BorderFactory.createTitledBorder("Animation File"));
  717. cp.add(filePanel);
  718. serialPanel.setBounds(409, 160, 243, 99);
  719. serialPanel.setBorder(BorderFactory.createTitledBorder("Serial communication"));
  720. cp.add(serialPanel);
  721. settingsPanel.setBounds(409, 75, 243, 82);
  722. settingsPanel.setBorder(BorderFactory.createTitledBorder("Frame duration"));
  723. cp.add(settingsPanel);
  724. logoText.setText("CubeControl");
  725. logoText.setBounds(415, 10, 243, 65);
  726. logoText.setFont(new Font("Default", Font.BOLD, 38));
  727. cp.add(logoText);
  728. // Ende Komponenten
  729. animList.addListSelectionListener(this);
  730. frameList.addListSelectionListener(this);
  731. setResizable(false);
  732. setVisible(true);
  733. valueChanged(new ListSelectionEvent(animList, 0, 0, false));
  734. }
  735. public Led3D get3D() {
  736. return ledView;
  737. }
  738. public static void main(String[] args) {
  739. Frame f = new Frame("Cube Control");
  740. Led3D l = f.get3D();
  741. String lastCommand = null;
  742. java.util.Scanner sc = new java.util.Scanner(System.in);
  743. System.out.println("#### Cube Control Debug Console ####");
  744. System.out.println("## Enter a Command ('h' for help) ##");
  745. System.out.print("$> ");
  746. do {
  747. if (sc.hasNextLine()) {
  748. String s = sc.nextLine();
  749. // Up arrow key
  750. if (s.equals("\u001B[A")) {
  751. if (lastCommand != null) {
  752. System.out.println("Last command: " + lastCommand);
  753. s = new String(lastCommand);
  754. } else {
  755. System.out.println("No last command!");
  756. }
  757. }
  758. if (s.equals("p") || (s.equals("print")))
  759. l.printTranslationData();
  760. if (s.equals("q") || s.equals("quit"))
  761. System.exit(0);
  762. if (s.equals("r") || s.equals("reset"))
  763. l.resetView();
  764. if (s.equals("on") || s.equals("1")) {
  765. short[] d = new short[64];
  766. for (int i = 0; i < d.length; i++) {
  767. d[i] = 0xFF;
  768. }
  769. l.setData(d);
  770. System.out.println("All LEDs on now...");
  771. }
  772. if (s.equals("off") || s.equals("0")) {
  773. short[] d = new short[64];
  774. for (int i = 0; i < d.length; i++) {
  775. d[i] = 0x00;
  776. }
  777. l.setData(d);
  778. System.out.println("All LEDs off now...");
  779. }
  780. if (s.startsWith("port ")) {
  781. f.serialPortSelector.addItem(s.substring(5));
  782. f.serialPortSelector.setSelectedItem(s.substring(5));
  783. }
  784. if (s.startsWith("send ")) {
  785. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  786. short[] dat = toShortArray(s.substring(5));
  787. HelperUtility.writeData(dat, dat.length);
  788. HelperUtility.closePort();
  789. }
  790. if (s.startsWith("read ")) {
  791. int leng = Integer.parseInt(s.substring(5));
  792. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  793. short[] data = HelperUtility.readData(leng);
  794. System.out.println(shortToString(data));
  795. HelperUtility.closePort();
  796. }
  797. if (s.startsWith("frames ")) {
  798. int anim = Integer.parseInt(s.substring(7));
  799. if (anim >= f.worker.size()) {
  800. System.out.println("Animation does not exist! Max: " + (f.worker.size() - 1));
  801. } else {
  802. System.out.println("Animation: " + f.worker.getAnimation(anim).getName());
  803. for (int i = 0; i < f.worker.getAnimation(anim).size(); i++) {
  804. AFrame frame = f.worker.getAnimation(anim).getFrame(i);
  805. System.out.println("\tFrame (" + frame.getTime() + "): " + frame.getName());
  806. }
  807. }
  808. }
  809. if (s.equals("a") || s.equals("anims")) {
  810. for (int i = 0; i < f.worker.size(); i++) {
  811. Animation anim = f.worker.getAnimation(i);
  812. System.out.println("\tAnimation: " + anim.getName());
  813. }
  814. }
  815. if (s.equals("s") || s.equals("scan")) {
  816. String[] sPorts = HelperUtility.getPorts();
  817. f.serialPortSelector.removeAllItems();
  818. for (int i = 0; i < sPorts.length; i++) {
  819. System.out.println("Port " + i + ": " + sPorts[i]);
  820. f.serialPortSelector.addItem(sPorts[i]);
  821. }
  822. }
  823. if (s.equals("h") || (s.equals("help"))) {
  824. System.out.println("Commands:");
  825. System.out.println("\t'port *name*'\t:\tSet serial port to this");
  826. System.out.println("\t'send *datas*'\t:\tSend data to serial port");
  827. System.out.println("\t'read *leng*'\t:\tRead data from serial port");
  828. System.out.println("\t'scan' / 's'\t:\tScan for serial ports");
  829. System.out.println("\t'on' / '1'\t:\tToggle all LEDs on");
  830. System.out.println("\t'off'/ '0'\t:\tToggle all LEDs off");
  831. System.out.println("\t'print'/ 'p'\t:\tPrint 3D Translation Matrix Data");
  832. System.out.println("\t'reset'/ 'r'\t:\tReset rotation of cube");
  833. System.out.println("\t'anims'/ 'a'\t:\tPrint animation tree");
  834. System.out.println("\t'frames *anim*\t:\tPrint frame tree");
  835. System.out.println("\t'help' / 'h'\t:\tShow this message");
  836. System.out.println("\t'quit' / 'q'\t:\tExit Cube Control");
  837. }
  838. lastCommand = new String(s);
  839. System.out.print("$> ");
  840. }
  841. } while (true);
  842. }
  843. private static short[] toShortArray(String s) {
  844. char[] d = s.toCharArray();
  845. System.out.println("Length: " + d.length);
  846. short[] r = new short[d.length];
  847. for (int i = 0; i < d.length; i++) {
  848. r[i] = (short) d[i];
  849. }
  850. return r;
  851. }
  852. private static String shortToString(short[] d) {
  853. String s = "";
  854. for (int i = 0; i < d.length; i++) {
  855. s += String.valueOf((char) d[i]);
  856. }
  857. return s;
  858. }
  859. }