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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /*
  2. * Frame.java
  3. *
  4. *
  5. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  6. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  7. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  8. *
  9. * This file is part of LED-Cube.
  10. *
  11. * LED-Cube is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * LED-Cube is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. import com.sun.j3d.utils.universe.*;
  25. import javax.media.j3d.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import javax.swing.*;
  29. import javax.swing.event.*;
  30. import java.io.File;
  31. 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. private 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. FullscreenWindow fw = new FullscreenWindow(worker);
  496. }
  497. public void editA_ActionPerformed(ActionEvent evt) {
  498. if (animList.getSelectedIndex() == -1) {
  499. errorMessage("Please select an animation.");
  500. } else if (frameList.getSelectedIndex() == -1) {
  501. errorMessage("Please select a Frame.");
  502. } else {
  503. @SuppressWarnings("unused")
  504. layerEditFrame layerFrame1 = new layerEditFrame(
  505. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  506. 0, worker, this);
  507. }
  508. }
  509. public void editB_ActionPerformed(ActionEvent evt) {
  510. if (animList.getSelectedIndex() == -1) {
  511. errorMessage("Please select an animation.");
  512. } else if (frameList.getSelectedIndex() == -1) {
  513. errorMessage("Please select a Frame.");
  514. } else {
  515. @SuppressWarnings("unused")
  516. layerEditFrame layerFrame1 = new layerEditFrame(
  517. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  518. 1, worker, this);
  519. }
  520. }
  521. public void editC_ActionPerformed(ActionEvent evt) {
  522. if (animList.getSelectedIndex() == -1) {
  523. errorMessage("Please select an animation.");
  524. } else if (frameList.getSelectedIndex() == -1) {
  525. errorMessage("Please select a Frame.");
  526. } else {
  527. @SuppressWarnings("unused")
  528. layerEditFrame layerFrame1 = new layerEditFrame(
  529. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  530. 2, worker, this);
  531. }
  532. }
  533. public void editD_ActionPerformed(ActionEvent evt) {
  534. if (animList.getSelectedIndex() == -1) {
  535. errorMessage("Please select an animation.");
  536. } else if (frameList.getSelectedIndex() == -1) {
  537. errorMessage("Please select a Frame.");
  538. } else {
  539. @SuppressWarnings("unused")
  540. layerEditFrame layerFrame1 = new layerEditFrame(
  541. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  542. 3, worker, this);
  543. }
  544. }
  545. public void editE_ActionPerformed(ActionEvent evt) {
  546. if (animList.getSelectedIndex() == -1) {
  547. errorMessage("Please select an animation.");
  548. } else if (frameList.getSelectedIndex() == -1) {
  549. errorMessage("Please select a Frame.");
  550. } else {
  551. @SuppressWarnings("unused")
  552. layerEditFrame layerFrame1 = new layerEditFrame(
  553. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  554. 4, worker, this);
  555. }
  556. }
  557. public void editF_ActionPerformed(ActionEvent evt) {
  558. if (animList.getSelectedIndex() == -1) {
  559. errorMessage("Please select an animation.");
  560. } else if (frameList.getSelectedIndex() == -1) {
  561. errorMessage("Please select a Frame.");
  562. } else {
  563. @SuppressWarnings("unused")
  564. layerEditFrame layerFrame1 = new layerEditFrame(
  565. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  566. 5, worker, this);
  567. }
  568. }
  569. public void editG_ActionPerformed(ActionEvent evt) {
  570. if (animList.getSelectedIndex() == -1) {
  571. errorMessage("Please select an animation.");
  572. } else if (frameList.getSelectedIndex() == -1) {
  573. errorMessage("Please select a Frame.");
  574. } else {
  575. @SuppressWarnings("unused")
  576. layerEditFrame layerFrame1 = new layerEditFrame(
  577. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  578. 6, worker, this);
  579. }
  580. }
  581. public void editH_ActionPerformed(ActionEvent evt) {
  582. if (animList.getSelectedIndex() == -1) {
  583. errorMessage("Please select an animation.");
  584. } else if (frameList.getSelectedIndex() == -1) {
  585. errorMessage("Please select a Frame.");
  586. } else {
  587. @SuppressWarnings("unused")
  588. layerEditFrame layerFrame1 = new layerEditFrame(
  589. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  590. 7, worker, this);
  591. }
  592. }
  593. public void frameUp_ActionPerformed(ActionEvent evt) {
  594. int i = frameList.getSelectedIndex();
  595. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  596. Object tmp = frameListModel.get(i);
  597. frameListModel.set(i, frameListModel.get(i - 1));
  598. frameListModel.set(i - 1, tmp);
  599. frameList.setSelectedIndex(i - 1);
  600. worker.moveFrame(worker.UP, animList.getSelectedIndex(),
  601. frameList.getSelectedIndex());
  602. }
  603. }
  604. public void frameDown_ActionPerformed(ActionEvent evt) {
  605. int i = frameList.getSelectedIndex();
  606. if ((i >= 0) && (frameListModel.getSize() >= 2)
  607. && (i < (frameListModel.getSize() - 1))) {
  608. Object tmp = frameListModel.get(i);
  609. frameListModel.set(i, frameListModel.get(i + 1));
  610. frameListModel.set(i + 1, tmp);
  611. frameList.setSelectedIndex(i + 1);
  612. worker.moveFrame(worker.DOWN, animList.getSelectedIndex(),
  613. frameList.getSelectedIndex());
  614. }
  615. }
  616. public void frameAdd_ActionPerformed(ActionEvent evt) {
  617. if (animList.getSelectedIndex() == -1) {
  618. errorMessage("Please select an animation!");
  619. } else {
  620. worker.addFrame(animList.getSelectedIndex());
  621. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  622. int n = worker.numOfFrames(animList.getSelectedIndex()) - 1;
  623. if (n < 0) {
  624. n = 0;
  625. }
  626. frameListModel.add(n,
  627. worker.getFrameName(animList.getSelectedIndex(), n));
  628. frameList.setModel(frameListModel);
  629. }
  630. }
  631. public void frameRemove_ActionPerformed(ActionEvent evt) {
  632. if (animList.getSelectedIndex() == -1) {
  633. errorMessage("Select an animation.");
  634. } else if (frameList.getSelectedIndex() == -1) {
  635. errorMessage("Select a Frame.");
  636. } else {
  637. worker.removeFrame(animList.getSelectedIndex(),
  638. frameList.getSelectedIndex());
  639. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  640. frameListModel.removeElementAt(frameList.getSelectedIndex());
  641. frameList.setModel(frameListModel);
  642. }
  643. }
  644. public void animUp_ActionPerformed(ActionEvent evt) {
  645. int i = animList.getSelectedIndex();
  646. if ((i > 0) && (animModel.getSize() >= 2)) {
  647. Object tmp = animModel.get(i);
  648. animModel.set(i, animModel.get(i - 1));
  649. animModel.set(i - 1, tmp);
  650. animList.setSelectedIndex(i - 1);
  651. worker.moveAnimation(worker.UP, animList.getSelectedIndex());
  652. }
  653. }
  654. public void animDown_ActionPerformed(ActionEvent evt) {
  655. int i = animList.getSelectedIndex();
  656. if ((i >= 0) && (animModel.getSize() >= 2)
  657. && (i < (animModel.getSize() - 1))) {
  658. Object tmp = animModel.get(i);
  659. animModel.set(i, animModel.get(i + 1));
  660. animModel.set(i + 1, tmp);
  661. animList.setSelectedIndex(i + 1);
  662. worker.moveAnimation(worker.DOWN, animList.getSelectedIndex());
  663. }
  664. }
  665. public void animAdd_ActionPerformed(ActionEvent evt) {
  666. if (worker.addAnimation() == -1) {
  667. errorMessage("Could not add animation!");
  668. } else {
  669. animModel.clear();
  670. for (int i = 0; i < worker.numOfAnimations(); i++) {
  671. animModel.add(i, worker.getAnimationName(i));
  672. }
  673. animList.setModel(animModel);
  674. }
  675. }
  676. public void animRemove_ActionPerformed(ActionEvent evt) {
  677. if (animList.getSelectedIndex() == -1) {
  678. errorMessage("Select an animation.");
  679. } else {
  680. worker.removeAnimation(animList.getSelectedIndex());
  681. animModel.removeElementAt(animList.getSelectedIndex());
  682. animList.setModel(animModel);
  683. }
  684. }
  685. public void load_ActionPerformed(ActionEvent evt) {
  686. JFileChooser fc = new JFileChooser();
  687. int ret = fc.showOpenDialog(this);
  688. if (ret == JFileChooser.APPROVE_OPTION) {
  689. File file = fc.getSelectedFile();
  690. if (fileSelected == false) {
  691. fileSelected = true;
  692. }
  693. animPath.setText(file.getPath());
  694. worker.loadState(animPath.getText());
  695. animModel.clear();
  696. for (int i = 0; i < worker.numOfAnimations(); i++) {
  697. animModel.addElement(worker.getAnimationName(i));
  698. }
  699. animList.setModel(animModel);
  700. frameListModel.clear();
  701. frameList.setModel(frameListModel);
  702. }
  703. }
  704. public void save_ActionPerformed(ActionEvent evt) {
  705. if (fileSelected == false) {
  706. JFileChooser fc = new JFileChooser();
  707. int ret = fc.showSaveDialog(this);
  708. if (ret == JFileChooser.APPROVE_OPTION) {
  709. File file = fc.getSelectedFile();
  710. fileSelected = true;
  711. animPath.setText(file.getPath());
  712. worker.saveState(animPath.getText());
  713. }
  714. } else {
  715. worker.saveState(animPath.getText());
  716. }
  717. }
  718. public void saveAs_ActionPerformed(ActionEvent evt) {
  719. JFileChooser fc;
  720. if (fileSelected == true) {
  721. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  722. } else {
  723. fc = new JFileChooser();
  724. }
  725. int ret = fc.showSaveDialog(this);
  726. if (ret == JFileChooser.APPROVE_OPTION) {
  727. File file = fc.getSelectedFile();
  728. if (fileSelected == false) {
  729. fileSelected = true;
  730. }
  731. animPath.setText(file.getPath());
  732. worker.saveState(animPath.getText());
  733. }
  734. }
  735. public void upload_ActionPerformed(ActionEvent evt) {
  736. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  737. errorMessage("No serial port selected...");
  738. } else {
  739. if (worker
  740. .probeCubeConnected((String) jComboBox1.getSelectedItem())) {
  741. if (worker.uploadState((String) jComboBox1.getSelectedItem()) != 0) {
  742. errorMessage("Could not upload data!");
  743. }
  744. } else {
  745. errorMessage("Cube does not respond...");
  746. }
  747. }
  748. }
  749. public void download_ActionPerformed(ActionEvent evt) {
  750. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  751. errorMessage("No serial port selected...");
  752. } else {
  753. worker.downloadState((String) jComboBox1.getSelectedItem());
  754. }
  755. }
  756. public Led3D get3D() {
  757. return ledView;
  758. }
  759. public static void main(String[] args) {
  760. Frame f = new Frame("Cube Control");
  761. Led3D l = f.get3D();
  762. java.util.Scanner sc = new java.util.Scanner(System.in);
  763. System.out.println("#### Cube Control Debug Console ####");
  764. System.out.println("## Enter a Command ('h' for help) ##");
  765. System.out.print("$> ");
  766. do {
  767. if (sc.hasNextLine()) {
  768. String s = sc.nextLine();
  769. if (s.equals("p") || (s.equals("print")))
  770. l.printTranslationData();
  771. if (s.equals("q") || s.equals("quit"))
  772. System.exit(0);
  773. if (s.equals("on") || s.equals("1")) {
  774. short[] d = new short[64];
  775. for (int i = 0; i < d.length; i++) {
  776. d[i] = 0xFF;
  777. }
  778. l.setData(d);
  779. System.out.println("All LEDs on now...");
  780. }
  781. if (s.equals("off") || s.equals("0")) {
  782. short[] d = new short[64];
  783. for (int i = 0; i < d.length; i++) {
  784. d[i] = 0x00;
  785. }
  786. l.setData(d);
  787. System.out.println("All LEDs off now...");
  788. }
  789. if (s.equals("r") || s.equals("reset")) {
  790. l.resetView();
  791. }
  792. if (s.startsWith("port ")) {
  793. s = s.substring(5);
  794. f.jComboBox1.addItem(s);
  795. f.jComboBox1.setSelectedItem(s);
  796. }
  797. if (s.startsWith("send ")) {
  798. s = s.substring(5);
  799. HelperUtility.openPort(((String) f.jComboBox1
  800. .getSelectedItem()) + "\n");
  801. short[] dat = toShortArray(s);
  802. HelperUtility.writeData(dat, dat.length);
  803. HelperUtility.closePort();
  804. }
  805. if (s.startsWith("read ")) {
  806. s = s.substring(5);
  807. int leng = Integer.parseInt(s);
  808. HelperUtility.openPort((String) f.jComboBox1
  809. .getSelectedItem());
  810. short[] data = HelperUtility.readData(leng);
  811. System.out.println(shortToString(data));
  812. HelperUtility.closePort();
  813. }
  814. if (s.equals("s") || s.equals("scan")) {
  815. String[] sPorts = f.worker.getSerialPorts();
  816. f.jComboBox1.removeAllItems();
  817. for (int i = 0; i < sPorts.length; i++) {
  818. f.jComboBox1.addItem(sPorts[i]);
  819. }
  820. }
  821. if (s.equals("h") || (s.equals("help"))) {
  822. System.out.println("Commands:");
  823. System.out
  824. .println("\t'port *name*'\t:\tSet serial port to this");
  825. System.out
  826. .println("\t'send *datas*'\t:\tSend data to serial port");
  827. System.out
  828. .println("\t'read *leng*'\t:\tRead data from serial port");
  829. System.out
  830. .println("\t'scan' / 's'\t:\tScan for serial ports");
  831. System.out
  832. .println("\t'on' / '1'\t:\tToggle all LEDs on");
  833. System.out
  834. .println("\t'off' / '0'\t:\tToggle all LEDs off");
  835. System.out
  836. .println("\t'print' / 'p'\t:\tPrint 3D Translation Matrix Data");
  837. System.out
  838. .println("\t'reset' / 'r'\t:\tReset rotation of cube");
  839. System.out
  840. .println("\t'help' / 'h'\t:\tShow this message");
  841. System.out
  842. .println("\t'quit' / 'q'\t:\tExit Cube Control");
  843. }
  844. System.out.print("$> ");
  845. }
  846. } while (true);
  847. }
  848. private static short[] toShortArray(String s) {
  849. char[] d = s.toCharArray();
  850. System.out.println("Length: " + d.length);
  851. short[] r = new short[d.length];
  852. for (int i = 0; i < d.length; i++) {
  853. r[i] = (short) d[i];
  854. }
  855. return r;
  856. }
  857. private static String shortToString(short[] d) {
  858. String s = "";
  859. for (int i = 0; i < d.length; i++) {
  860. s += String.valueOf((char) d[i]);
  861. }
  862. return s;
  863. }
  864. }