Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Frame.java 32KB

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