Simple single-color 8x8x8 LED Cube with AVRs
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Frame.java 31KB

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