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 33KB

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