Simple single-color 8x8x8 LED Cube with AVRs
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Frame.java 32KB

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