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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. //----- 3D-----
  162. //-------------
  163. cubeCanvas.setBounds(8, 8, 250, 250);
  164. cp.add(cubeCanvas);
  165. //-------------
  166. editA.setBounds(264, 8, 107, 25);
  167. editA.setText("Layer A");
  168. editA.setFont(new Font("Dialog", Font.PLAIN, 13));
  169. cp.add(editA);
  170. editA.addActionListener(new ActionListener() {
  171. public void actionPerformed(ActionEvent evt) {
  172. editA_ActionPerformed(evt);
  173. }
  174. });
  175. editB.setBounds(264, 40, 107, 25);
  176. editB.setText("Layer B");
  177. editB.setFont(new Font("Dialog", Font.PLAIN, 13));
  178. cp.add(editB);
  179. editB.addActionListener(new ActionListener() {
  180. public void actionPerformed(ActionEvent evt) {
  181. editB_ActionPerformed(evt);
  182. }
  183. });
  184. editC.setBounds(264, 72, 107, 25);
  185. editC.setText("Layer C");
  186. editC.setFont(new Font("Dialog", Font.PLAIN, 13));
  187. cp.add(editC);
  188. editC.addActionListener(new ActionListener() {
  189. public void actionPerformed(ActionEvent evt) {
  190. editC_ActionPerformed(evt);
  191. }
  192. });
  193. editD.setBounds(264, 104, 107, 25);
  194. editD.setText("Layer D");
  195. editD.setFont(new Font("Dialog", Font.PLAIN, 13));
  196. cp.add(editD);
  197. editD.addActionListener(new ActionListener() {
  198. public void actionPerformed(ActionEvent evt) {
  199. editD_ActionPerformed(evt);
  200. }
  201. });
  202. editE.setBounds(264, 136, 107, 25);
  203. editE.setText("Layer E");
  204. editE.setFont(new Font("Dialog", Font.PLAIN, 13));
  205. cp.add(editE);
  206. editE.addActionListener(new ActionListener() {
  207. public void actionPerformed(ActionEvent evt) {
  208. editE_ActionPerformed(evt);
  209. }
  210. });
  211. editF.setBounds(264, 168, 107, 25);
  212. editF.setText("Layer F");
  213. editF.setFont(new Font("Dialog", Font.PLAIN, 13));
  214. cp.add(editF);
  215. editF.addActionListener(new ActionListener() {
  216. public void actionPerformed(ActionEvent evt) {
  217. editF_ActionPerformed(evt);
  218. }
  219. });
  220. editG.setBounds(264, 200, 107, 25);
  221. editG.setText("Layer G");
  222. editG.setFont(new Font("Dialog", Font.PLAIN, 13));
  223. cp.add(editG);
  224. editG.addActionListener(new ActionListener() {
  225. public void actionPerformed(ActionEvent evt) {
  226. editG_ActionPerformed(evt);
  227. }
  228. });
  229. editH.setBounds(264, 232, 107, 25);
  230. editH.setText("Layer H");
  231. editH.setFont(new Font("Dialog", Font.PLAIN, 13));
  232. cp.add(editH);
  233. editH.addActionListener(new ActionListener() {
  234. public void actionPerformed(ActionEvent evt) {
  235. editH_ActionPerformed(evt);
  236. }
  237. });
  238. frameListScrollPane.setBounds(384, 8, 145, 249);
  239. frameList.setModel(frameListModel);
  240. cp.add(frameListScrollPane);
  241. frameUp.setBounds(544, 8, 107, 28);
  242. frameUp.setText("Move up");
  243. frameUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  244. cp.add(frameUp);
  245. frameUp.addActionListener(new ActionListener() {
  246. public void actionPerformed(ActionEvent evt) {
  247. frameUp_ActionPerformed(evt);
  248. }
  249. });
  250. frameAdd.setBounds(544, 39, 107, 28);
  251. frameAdd.setText("Add");
  252. frameAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  253. cp.add(frameAdd);
  254. frameAdd.addActionListener(new ActionListener() {
  255. public void actionPerformed(ActionEvent evt) {
  256. frameAdd_ActionPerformed(evt);
  257. }
  258. });
  259. frameRemove.setBounds(544, 70, 107, 28);
  260. frameRemove.setText("Remove");
  261. frameRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  262. cp.add(frameRemove);
  263. frameRemove.addActionListener(new ActionListener() {
  264. public void actionPerformed(ActionEvent evt) {
  265. frameRemove_ActionPerformed(evt);
  266. }
  267. });
  268. frameRename.setBounds(544, 101, 107, 28);
  269. frameRename.setText("Rename");
  270. frameRename.setFont(new Font("Dialog", Font.PLAIN, 13));
  271. cp.add(frameRename);
  272. frameRename.addActionListener(new ActionListener() {
  273. public void actionPerformed(ActionEvent evt) {
  274. int a = animList.getSelectedIndex();
  275. if (a < 0) {
  276. errorMessage("Select an animation!");
  277. return;
  278. }
  279. int f = frameList.getSelectedIndex();
  280. if (f < 0) {
  281. errorMessage("Select a Frame!");
  282. return;
  283. }
  284. worker.setFrameName(askString("Rename", "Rename " + frameList.getSelectedValue() + "?"), a, f);
  285. frameListModel.set(f, worker.getFrameName(a, f));
  286. frameList.setModel(frameListModel);
  287. }
  288. });
  289. frameDown.setBounds(544, 132, 107, 28);
  290. frameDown.setText("Move down");
  291. frameDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  292. cp.add(frameDown);
  293. frameDown.addActionListener(new ActionListener() {
  294. public void actionPerformed(ActionEvent evt) {
  295. frameDown_ActionPerformed(evt);
  296. }
  297. });
  298. frmLngthLbl.setBounds(536, 160, 113, 24);
  299. frmLngthLbl.setText("Time (1/24 sec)");
  300. frmLngthLbl.setFont(new Font("Dialog", Font.PLAIN, 13));
  301. cp.add(frmLngthLbl);
  302. frmLngthTxt.setBounds(536, 184, 50, 24);
  303. frmLngthTxt.setText("");
  304. frmLngthTxt.setFont(new Font("Dialog", Font.PLAIN, 13));
  305. cp.add(frmLngthTxt);
  306. frameDuration.setBounds(590, 184, 50, 24);
  307. frameDuration.setText("OK");
  308. frameDuration.setFont(new Font("Dialog", Font.PLAIN, 13));
  309. cp.add(frameDuration);
  310. frameDuration.addActionListener(new ActionListener() {
  311. public void actionPerformed(ActionEvent evt) {
  312. if (frmLngthTxt.getText().equals("0")) {
  313. errorMessage("0 is not a valid value!");
  314. frmLngthTxt.setText("1");
  315. } else if (Integer.parseInt(frmLngthTxt.getText()) > 256) {
  316. errorMessage("Value too high. Max: 256");
  317. frmLngthTxt.setText("256");
  318. return;
  319. } else {
  320. if (animList.getSelectedIndex() == -1) {
  321. errorMessage("Please select an animation!");
  322. return;
  323. }
  324. if (frameList.getSelectedIndex() == -1) {
  325. errorMessage("Please select a Frame!");
  326. return;
  327. }
  328. worker.setFrameTime((byte)(Integer.parseInt(frmLngthTxt.getText()) - 1), animList.getSelectedIndex(), frameList.getSelectedIndex());
  329. }
  330. }
  331. });
  332. animScrollPane.setBounds(8, 264, 209, 121);
  333. animList.setModel(animModel);
  334. cp.add(animScrollPane);
  335. animUp.setBounds(224, 264, 99, 25);
  336. animUp.setText("Move up");
  337. animUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  338. cp.add(animUp);
  339. animUp.addActionListener(new ActionListener() {
  340. public void actionPerformed(ActionEvent evt) {
  341. animUp_ActionPerformed(evt);
  342. }
  343. });
  344. animDown.setBounds(224, 368, 99, 25);
  345. animDown.setText("Move down");
  346. animDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  347. cp.add(animDown);
  348. animDown.addActionListener(new ActionListener() {
  349. public void actionPerformed(ActionEvent evt) {
  350. animDown_ActionPerformed(evt);
  351. }
  352. });
  353. animRename.setBounds(224, 342, 99, 25);
  354. animRename.setText("Rename");
  355. animRename.setFont(new Font("Dialog", Font.PLAIN, 13));
  356. cp.add(animRename);
  357. animRename.addActionListener(new ActionListener() {
  358. public void actionPerformed(ActionEvent evt) {
  359. int a = animList.getSelectedIndex();
  360. if (a < 0) {
  361. errorMessage("Select an animation!");
  362. return;
  363. }
  364. worker.setAnimationName(askString("Rename", "Rename " + animList.getSelectedValue() + "?"), a);
  365. animModel.set(a, worker.getAnimationName(a));
  366. animList.setModel(animModel);
  367. }
  368. });
  369. animAdd.setBounds(224, 290, 99, 25);
  370. animAdd.setText("Add");
  371. animAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  372. cp.add(animAdd);
  373. animAdd.addActionListener(new ActionListener() {
  374. public void actionPerformed(ActionEvent evt) {
  375. animAdd_ActionPerformed(evt);
  376. }
  377. });
  378. animRemove.setBounds(224, 316, 99, 25);
  379. animRemove.setText("Remove");
  380. animRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  381. cp.add(animRemove);
  382. animRemove.addActionListener(new ActionListener() {
  383. public void actionPerformed(ActionEvent evt) {
  384. animRemove_ActionPerformed(evt);
  385. }
  386. });
  387. animPath.setBounds(344, 264, 305, 24);
  388. animPath.setEditable(false);
  389. animPath.setText("Load/Save an animation file...");
  390. animPath.setFont(new Font("Dialog", Font.PLAIN, 13));
  391. cp.add(animPath);
  392. load.setBounds(344, 296, 100, 25);
  393. load.setText("Load");
  394. load.setFont(new Font("Dialog", Font.PLAIN, 13));
  395. cp.add(load);
  396. load.addActionListener(new ActionListener() {
  397. public void actionPerformed(ActionEvent evt) {
  398. load_ActionPerformed(evt);
  399. }
  400. });
  401. save.setBounds(454, 296, 100, 25);
  402. save.setText("Save");
  403. save.setFont(new Font("Dialog", Font.PLAIN, 13));
  404. cp.add(save);
  405. save.addActionListener(new ActionListener() {
  406. public void actionPerformed(ActionEvent evt) {
  407. save_ActionPerformed(evt);
  408. }
  409. });
  410. saveAs.setBounds(564, 296, 90, 25);
  411. saveAs.setText("Save As");
  412. saveAs.setFont(new Font("Dialog", Font.PLAIN, 13));
  413. cp.add(saveAs);
  414. saveAs.addActionListener(new ActionListener() {
  415. public void actionPerformed(ActionEvent evt) {
  416. saveAs_ActionPerformed(evt);
  417. }
  418. });
  419. jComboBox1.setBounds(344, 328, 305, 24);
  420. jComboBox1.setFont(new Font("Dialog", Font.PLAIN, 13));
  421. cp.add(jComboBox1);
  422. upload.setBounds(344, 360, 147, 25);
  423. upload.setText("Upload");
  424. upload.setFont(new Font("Dialog", Font.PLAIN, 13));
  425. cp.add(upload);
  426. upload.addActionListener(new ActionListener() {
  427. public void actionPerformed(ActionEvent evt) {
  428. upload_ActionPerformed(evt);
  429. }
  430. });
  431. download.setBounds(504, 360, 147, 25);
  432. download.setText("Download");
  433. download.setFont(new Font("Dialog", Font.PLAIN, 13));
  434. cp.add(download);
  435. download.addActionListener(new ActionListener() {
  436. public void actionPerformed(ActionEvent evt) {
  437. download_ActionPerformed(evt);
  438. }
  439. });
  440. jLabel4.setBounds(536, 208, 112, 20);
  441. jLabel4.setText("Remaining:");
  442. jLabel4.setFont(new Font("Dialog", Font.PLAIN, 13));
  443. cp.add(jLabel4);
  444. frameRemaining.setBounds(536, 232, 113, 24);
  445. frameRemaining.setEditable(false);
  446. frameRemaining.setText(String.valueOf(worker.framesRemaining()));
  447. frameRemaining.setFont(new Font("Dialog", Font.PLAIN, 13));
  448. cp.add(frameRemaining);
  449. animList.setFont(new Font("Dialog", Font.PLAIN, 13));
  450. frameList.setFont(new Font("Dialog", Font.PLAIN, 13));
  451. // Ende Komponenten
  452. animList.addListSelectionListener(this);
  453. frameList.addListSelectionListener(this);
  454. setResizable(false);
  455. setVisible(true);
  456. }
  457. // Anfang Methoden
  458. // Anfang Ereignisprozeduren
  459. public void editA_ActionPerformed(ActionEvent evt) {
  460. if(animList.getSelectedIndex() == -1){
  461. errorMessage("Please select an animation.");
  462. } else if(frameList.getSelectedIndex() == -1){
  463. errorMessage("Please select a Frame.");
  464. } else {
  465. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 0, worker, this);
  466. }
  467. }
  468. public void editB_ActionPerformed(ActionEvent evt) {
  469. if(animList.getSelectedIndex() == -1){
  470. errorMessage("Please select an animation.");
  471. } else if(frameList.getSelectedIndex() == -1){
  472. errorMessage("Please select a Frame.");
  473. } else {
  474. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 1, worker, this);
  475. }
  476. }
  477. public void editC_ActionPerformed(ActionEvent evt) {
  478. if(animList.getSelectedIndex() == -1){
  479. errorMessage("Please select an animation.");
  480. } else if(frameList.getSelectedIndex() == -1){
  481. errorMessage("Please select a Frame.");
  482. } else {
  483. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 2, worker, this);
  484. }
  485. }
  486. public void editD_ActionPerformed(ActionEvent evt) {
  487. if(animList.getSelectedIndex() == -1){
  488. errorMessage("Please select an animation.");
  489. } else if(frameList.getSelectedIndex() == -1){
  490. errorMessage("Please select a Frame.");
  491. } else {
  492. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 3, worker, this);
  493. }
  494. }
  495. public void editE_ActionPerformed(ActionEvent evt) {
  496. if(animList.getSelectedIndex() == -1){
  497. errorMessage("Please select an animation.");
  498. } else if(frameList.getSelectedIndex() == -1){
  499. errorMessage("Please select a Frame.");
  500. } else {
  501. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 4, worker, this);
  502. }
  503. }
  504. public void editF_ActionPerformed(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. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 5, worker, this);
  511. }
  512. }
  513. public void editG_ActionPerformed(ActionEvent evt) {
  514. if(animList.getSelectedIndex() == -1){
  515. errorMessage("Please select an animation.");
  516. } else if(frameList.getSelectedIndex() == -1){
  517. errorMessage("Please select a Frame.");
  518. } else {
  519. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 6, worker, this);
  520. }
  521. }
  522. public void editH_ActionPerformed(ActionEvent evt) {
  523. if(animList.getSelectedIndex() == -1){
  524. errorMessage("Please select an animation.");
  525. } else if(frameList.getSelectedIndex() == -1){
  526. errorMessage("Please select a Frame.");
  527. } else {
  528. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 7, worker, this);
  529. }
  530. }
  531. public void frameUp_ActionPerformed(ActionEvent evt) {
  532. int i = frameList.getSelectedIndex();
  533. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  534. Object tmp = frameListModel.get(i);
  535. frameListModel.set(i, frameListModel.get(i - 1));
  536. frameListModel.set(i - 1, tmp);
  537. frameList.setSelectedIndex(i - 1);
  538. worker.moveFrame(worker.UP, animList.getSelectedIndex(), frameList.getSelectedIndex());
  539. }
  540. }
  541. public void frameDown_ActionPerformed(ActionEvent evt) {
  542. int i = frameList.getSelectedIndex();
  543. if ((i >= 0) && (frameListModel.getSize() >= 2) && (i < (frameListModel.getSize() - 1))) {
  544. Object tmp = frameListModel.get(i);
  545. frameListModel.set(i, frameListModel.get(i + 1));
  546. frameListModel.set(i + 1, tmp);
  547. frameList.setSelectedIndex(i + 1);
  548. worker.moveFrame(worker.DOWN, animList.getSelectedIndex(), frameList.getSelectedIndex());
  549. }
  550. }
  551. public void frameAdd_ActionPerformed(ActionEvent evt) {
  552. if(animList.getSelectedIndex() == -1){
  553. errorMessage("Please select an animation!");
  554. } else {
  555. worker.addFrame(animList.getSelectedIndex());
  556. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  557. int n = worker.numOfFrames(animList.getSelectedIndex()) - 1;
  558. if (n < 0) {
  559. n = 0;
  560. }
  561. frameListModel.add(n, worker.getFrameName(animList.getSelectedIndex(), n));
  562. frameList.setModel(frameListModel);
  563. }
  564. }
  565. public void frameRemove_ActionPerformed(ActionEvent evt) {
  566. if(animList.getSelectedIndex() == -1){
  567. errorMessage("Select an animation.");
  568. } else if(frameList.getSelectedIndex() == -1){
  569. errorMessage("Select a Frame.");
  570. } else {
  571. worker.removeFrame(animList.getSelectedIndex(), frameList.getSelectedIndex());
  572. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  573. frameListModel.removeElementAt(frameList.getSelectedIndex());
  574. frameList.setModel(frameListModel);
  575. }
  576. }
  577. public void animUp_ActionPerformed(ActionEvent evt) {
  578. int i = animList.getSelectedIndex();
  579. if ((i > 0) && (animModel.getSize() >= 2)) {
  580. Object tmp = animModel.get(i);
  581. animModel.set(i, animModel.get(i - 1));
  582. animModel.set(i - 1, tmp);
  583. animList.setSelectedIndex(i - 1);
  584. worker.moveAnimation(worker.UP, animList.getSelectedIndex());
  585. }
  586. }
  587. public void animDown_ActionPerformed(ActionEvent evt) {
  588. int i = animList.getSelectedIndex();
  589. if ((i >= 0) && (animModel.getSize() >= 2) && (i < (animModel.getSize() - 1))) {
  590. Object tmp = animModel.get(i);
  591. animModel.set(i, animModel.get(i + 1));
  592. animModel.set(i + 1, tmp);
  593. animList.setSelectedIndex(i + 1);
  594. worker.moveAnimation(worker.DOWN, animList.getSelectedIndex());
  595. }
  596. }
  597. public void animAdd_ActionPerformed(ActionEvent evt) {
  598. if(worker.addAnimation() == -1){
  599. errorMessage("Could not add animation!");
  600. } else {
  601. animModel.clear();
  602. for (int i = 0; i < worker.numOfAnimations(); i++) {
  603. animModel.add(i, worker.getAnimationName(i));
  604. }
  605. animList.setModel(animModel);
  606. }
  607. }
  608. public void animRemove_ActionPerformed(ActionEvent evt) {
  609. if(animList.getSelectedIndex() == -1){
  610. errorMessage("Select an animation.");
  611. } else {
  612. worker.removeAnimation(animList.getSelectedIndex());
  613. animModel.removeElementAt(animList.getSelectedIndex());
  614. animList.setModel(animModel);
  615. }
  616. }
  617. public void load_ActionPerformed(ActionEvent evt) {
  618. JFileChooser fc = new JFileChooser();
  619. int ret = fc.showOpenDialog(this);
  620. if (ret == JFileChooser.APPROVE_OPTION) {
  621. File file = fc.getSelectedFile();
  622. if (fileSelected == false) {
  623. fileSelected = true;
  624. }
  625. animPath.setText(file.getPath());
  626. worker.loadState(animPath.getText());
  627. animModel.clear();
  628. for (int i = 0; i < worker.numOfAnimations(); i++) {
  629. animModel.addElement(worker.getAnimationName(i));
  630. }
  631. animList.setModel(animModel);
  632. frameListModel.clear();
  633. frameList.setModel(frameListModel);
  634. }
  635. }
  636. public void save_ActionPerformed(ActionEvent evt) {
  637. if (fileSelected == false) {
  638. JFileChooser fc = new JFileChooser();
  639. int ret = fc.showSaveDialog(this);
  640. if (ret == JFileChooser.APPROVE_OPTION) {
  641. File file = fc.getSelectedFile();
  642. fileSelected = true;
  643. animPath.setText(file.getPath());
  644. worker.saveState(animPath.getText());
  645. }
  646. } else {
  647. worker.saveState(animPath.getText());
  648. }
  649. }
  650. public void saveAs_ActionPerformed(ActionEvent evt) {
  651. JFileChooser fc;
  652. if (fileSelected == true) {
  653. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  654. } else {
  655. fc = new JFileChooser();
  656. }
  657. int ret = fc.showSaveDialog(this);
  658. if (ret == JFileChooser.APPROVE_OPTION) {
  659. File file = fc.getSelectedFile();
  660. if (fileSelected == false) {
  661. fileSelected = true;
  662. }
  663. animPath.setText(file.getPath());
  664. worker.saveState(animPath.getText());
  665. }
  666. }
  667. public void upload_ActionPerformed(ActionEvent evt) {
  668. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  669. errorMessage("No serial port selected...");
  670. } else {
  671. worker.uploadState((String)jComboBox1.getSelectedItem());
  672. }
  673. }
  674. public void download_ActionPerformed(ActionEvent evt) {
  675. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  676. errorMessage("No serial port selected...");
  677. } else {
  678. worker.downloadState((String)jComboBox1.getSelectedItem());
  679. }
  680. }
  681. public Led3D get3D() {
  682. return ledView;
  683. }
  684. public static void main(String[] args) {
  685. Frame f = new Frame("Cube Control");
  686. Led3D l = f.get3D();
  687. java.util.Scanner sc = new java.util.Scanner(System.in);
  688. System.out.println("#### Cube Control Debug Console ####");
  689. System.out.println("## Enter a Command ('h' for help) ##");
  690. System.out.print("$> ");
  691. do {
  692. if (sc.hasNext()) {
  693. String s = sc.next();
  694. if (s.equals("p") || (s.equals("print")))
  695. l.printTranslationData();
  696. if (s.equals("q") || s.equals("quit"))
  697. System.exit(0);
  698. if (s.equals("on") || s.equals("1")) {
  699. short[] d = new short[64];
  700. for (int i = 0; i < d.length; i++) {
  701. d[i] = 0xFF;
  702. }
  703. l.setData(d);
  704. System.out.println("All LEDs on now...");
  705. }
  706. if (s.equals("off") || s.equals("0")) {
  707. short[] d = new short[64];
  708. for (int i = 0; i < d.length; i++) {
  709. d[i] = 0x00;
  710. }
  711. l.setData(d);
  712. System.out.println("All LEDs off now...");
  713. }
  714. if (s.equals("h") || (s.equals("help"))) {
  715. System.out.println("Commands:");
  716. System.out.println("\t'on' / '1'\t:\tToggle all LEDs on");
  717. System.out.println("\t'off' / '0'\t:\tToggle all LEDs off");
  718. System.out.println("\t'print' / 'p'\t:\tPrint 3D Translation Matrix Data");
  719. System.out.println("\t'help' / 'h'\t:\tShow this message");
  720. System.out.println("\t'quit' / 'q'\t:\tExit Cube Control");
  721. }
  722. System.out.print("$> ");
  723. }
  724. } while (true);
  725. }
  726. }