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

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