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

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