Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Frame.java 30KB

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