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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. short time1 = worker.getFrameTime(animList.getSelectedIndex(), frameList.getSelectedIndex());
  513. long time = (long) (((time1+1) * 1/24) * 1000);
  514. try{
  515. Thread.sleep(time);
  516. }catch(Exception e){
  517. System.out.println(e);
  518. }
  519. }
  520. }
  521. }
  522. public void enterFullscreen(ActionEvent evt) {
  523. ledView.enterFullscreen();
  524. FullscreenWindow fw = new FullscreenWindow(worker, cubeCanvas, ledView, this);
  525. }
  526. public void editA_ActionPerformed(ActionEvent evt) {
  527. if (animList.getSelectedIndex() == -1) {
  528. errorMessage("Please select an animation.");
  529. } else if (frameList.getSelectedIndex() == -1) {
  530. errorMessage("Please select a Frame.");
  531. } else {
  532. @SuppressWarnings("unused")
  533. layerEditFrame layerFrame1 = new layerEditFrame(
  534. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  535. 0, worker, this);
  536. }
  537. }
  538. public void editB_ActionPerformed(ActionEvent evt) {
  539. if (animList.getSelectedIndex() == -1) {
  540. errorMessage("Please select an animation.");
  541. } else if (frameList.getSelectedIndex() == -1) {
  542. errorMessage("Please select a Frame.");
  543. } else {
  544. @SuppressWarnings("unused")
  545. layerEditFrame layerFrame1 = new layerEditFrame(
  546. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  547. 1, worker, this);
  548. }
  549. }
  550. public void editC_ActionPerformed(ActionEvent evt) {
  551. if (animList.getSelectedIndex() == -1) {
  552. errorMessage("Please select an animation.");
  553. } else if (frameList.getSelectedIndex() == -1) {
  554. errorMessage("Please select a Frame.");
  555. } else {
  556. @SuppressWarnings("unused")
  557. layerEditFrame layerFrame1 = new layerEditFrame(
  558. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  559. 2, worker, this);
  560. }
  561. }
  562. public void editD_ActionPerformed(ActionEvent evt) {
  563. if (animList.getSelectedIndex() == -1) {
  564. errorMessage("Please select an animation.");
  565. } else if (frameList.getSelectedIndex() == -1) {
  566. errorMessage("Please select a Frame.");
  567. } else {
  568. @SuppressWarnings("unused")
  569. layerEditFrame layerFrame1 = new layerEditFrame(
  570. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  571. 3, worker, this);
  572. }
  573. }
  574. public void editE_ActionPerformed(ActionEvent evt) {
  575. if (animList.getSelectedIndex() == -1) {
  576. errorMessage("Please select an animation.");
  577. } else if (frameList.getSelectedIndex() == -1) {
  578. errorMessage("Please select a Frame.");
  579. } else {
  580. @SuppressWarnings("unused")
  581. layerEditFrame layerFrame1 = new layerEditFrame(
  582. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  583. 4, worker, this);
  584. }
  585. }
  586. public void editF_ActionPerformed(ActionEvent evt) {
  587. if (animList.getSelectedIndex() == -1) {
  588. errorMessage("Please select an animation.");
  589. } else if (frameList.getSelectedIndex() == -1) {
  590. errorMessage("Please select a Frame.");
  591. } else {
  592. @SuppressWarnings("unused")
  593. layerEditFrame layerFrame1 = new layerEditFrame(
  594. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  595. 5, worker, this);
  596. }
  597. }
  598. public void editG_ActionPerformed(ActionEvent evt) {
  599. if (animList.getSelectedIndex() == -1) {
  600. errorMessage("Please select an animation.");
  601. } else if (frameList.getSelectedIndex() == -1) {
  602. errorMessage("Please select a Frame.");
  603. } else {
  604. @SuppressWarnings("unused")
  605. layerEditFrame layerFrame1 = new layerEditFrame(
  606. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  607. 6, worker, this);
  608. }
  609. }
  610. public void editH_ActionPerformed(ActionEvent evt) {
  611. if (animList.getSelectedIndex() == -1) {
  612. errorMessage("Please select an animation.");
  613. } else if (frameList.getSelectedIndex() == -1) {
  614. errorMessage("Please select a Frame.");
  615. } else {
  616. @SuppressWarnings("unused")
  617. layerEditFrame layerFrame1 = new layerEditFrame(
  618. animList.getSelectedIndex(), frameList.getSelectedIndex(),
  619. 7, worker, this);
  620. }
  621. }
  622. public void frameUp_ActionPerformed(ActionEvent evt) {
  623. int i = frameList.getSelectedIndex();
  624. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  625. Object tmp = frameListModel.get(i);
  626. frameListModel.set(i, frameListModel.get(i - 1));
  627. frameListModel.set(i - 1, tmp);
  628. frameList.setSelectedIndex(i - 1);
  629. worker.moveFrame(worker.UP, animList.getSelectedIndex(),
  630. frameList.getSelectedIndex());
  631. }
  632. }
  633. public void frameDown_ActionPerformed(ActionEvent evt) {
  634. int i = frameList.getSelectedIndex();
  635. if ((i >= 0) && (frameListModel.getSize() >= 2)
  636. && (i < (frameListModel.getSize() - 1))) {
  637. Object tmp = frameListModel.get(i);
  638. frameListModel.set(i, frameListModel.get(i + 1));
  639. frameListModel.set(i + 1, tmp);
  640. frameList.setSelectedIndex(i + 1);
  641. worker.moveFrame(worker.DOWN, animList.getSelectedIndex(),
  642. frameList.getSelectedIndex());
  643. }
  644. }
  645. public void frameAdd_ActionPerformed(ActionEvent evt) {
  646. if (animList.getSelectedIndex() == -1) {
  647. errorMessage("Please select an animation!");
  648. } else {
  649. worker.addFrame(animList.getSelectedIndex());
  650. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  651. int n = worker.numOfFrames(animList.getSelectedIndex()) - 1;
  652. if (n < 0) {
  653. n = 0;
  654. }
  655. frameListModel.add(n,
  656. worker.getFrameName(animList.getSelectedIndex(), n));
  657. frameList.setModel(frameListModel);
  658. }
  659. }
  660. public void frameRemove_ActionPerformed(ActionEvent evt) {
  661. if (animList.getSelectedIndex() == -1) {
  662. errorMessage("Select an animation.");
  663. } else if (frameList.getSelectedIndex() == -1) {
  664. errorMessage("Select a Frame.");
  665. } else {
  666. worker.removeFrame(animList.getSelectedIndex(),
  667. frameList.getSelectedIndex());
  668. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  669. frameListModel.removeElementAt(frameList.getSelectedIndex());
  670. frameList.setModel(frameListModel);
  671. }
  672. }
  673. public void animUp_ActionPerformed(ActionEvent evt) {
  674. int i = animList.getSelectedIndex();
  675. if ((i > 0) && (animModel.getSize() >= 2)) {
  676. Object tmp = animModel.get(i);
  677. animModel.set(i, animModel.get(i - 1));
  678. animModel.set(i - 1, tmp);
  679. animList.setSelectedIndex(i - 1);
  680. worker.moveAnimation(worker.UP, animList.getSelectedIndex());
  681. }
  682. }
  683. public void animDown_ActionPerformed(ActionEvent evt) {
  684. int i = animList.getSelectedIndex();
  685. if ((i >= 0) && (animModel.getSize() >= 2)
  686. && (i < (animModel.getSize() - 1))) {
  687. Object tmp = animModel.get(i);
  688. animModel.set(i, animModel.get(i + 1));
  689. animModel.set(i + 1, tmp);
  690. animList.setSelectedIndex(i + 1);
  691. worker.moveAnimation(worker.DOWN, animList.getSelectedIndex());
  692. }
  693. }
  694. public void animAdd_ActionPerformed(ActionEvent evt) {
  695. if (worker.addAnimation() == -1) {
  696. errorMessage("Could not add animation!");
  697. } else {
  698. animModel.clear();
  699. for (int i = 0; i < worker.numOfAnimations(); i++) {
  700. animModel.add(i, worker.getAnimationName(i));
  701. }
  702. animList.setModel(animModel);
  703. }
  704. }
  705. public void animRemove_ActionPerformed(ActionEvent evt) {
  706. if (animList.getSelectedIndex() == -1) {
  707. errorMessage("Select an animation.");
  708. } else {
  709. worker.removeAnimation(animList.getSelectedIndex());
  710. animModel.removeElementAt(animList.getSelectedIndex());
  711. animList.setModel(animModel);
  712. }
  713. }
  714. public void load_ActionPerformed(ActionEvent evt) {
  715. JFileChooser fc = new JFileChooser();
  716. int ret = fc.showOpenDialog(this);
  717. if (ret == JFileChooser.APPROVE_OPTION) {
  718. File file = fc.getSelectedFile();
  719. if (fileSelected == false) {
  720. fileSelected = true;
  721. }
  722. animPath.setText(file.getPath());
  723. worker.loadState(animPath.getText());
  724. animModel.clear();
  725. for (int i = 0; i < worker.numOfAnimations(); i++) {
  726. animModel.addElement(worker.getAnimationName(i));
  727. }
  728. animList.setModel(animModel);
  729. frameListModel.clear();
  730. frameList.setModel(frameListModel);
  731. }
  732. }
  733. public void save_ActionPerformed(ActionEvent evt) {
  734. if (fileSelected == false) {
  735. JFileChooser fc = new JFileChooser();
  736. int ret = fc.showSaveDialog(this);
  737. if (ret == JFileChooser.APPROVE_OPTION) {
  738. File file = fc.getSelectedFile();
  739. fileSelected = true;
  740. animPath.setText(file.getPath());
  741. worker.saveState(animPath.getText());
  742. }
  743. } else {
  744. worker.saveState(animPath.getText());
  745. }
  746. }
  747. public void saveAs_ActionPerformed(ActionEvent evt) {
  748. JFileChooser fc;
  749. if (fileSelected == true) {
  750. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  751. } else {
  752. fc = new JFileChooser();
  753. }
  754. int ret = fc.showSaveDialog(this);
  755. if (ret == JFileChooser.APPROVE_OPTION) {
  756. File file = fc.getSelectedFile();
  757. if (fileSelected == false) {
  758. fileSelected = true;
  759. }
  760. animPath.setText(file.getPath());
  761. worker.saveState(animPath.getText());
  762. }
  763. }
  764. public void upload_ActionPerformed(ActionEvent evt) {
  765. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  766. errorMessage("No serial port selected...");
  767. } else {
  768. if (worker
  769. .probeCubeConnected((String) jComboBox1.getSelectedItem())) {
  770. if (worker.uploadState((String) jComboBox1.getSelectedItem()) != 0) {
  771. errorMessage("Could not upload data!");
  772. }
  773. } else {
  774. errorMessage("Cube does not respond...");
  775. }
  776. }
  777. }
  778. public void download_ActionPerformed(ActionEvent evt) {
  779. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  780. errorMessage("No serial port selected...");
  781. } else {
  782. worker.downloadState((String) jComboBox1.getSelectedItem());
  783. }
  784. }
  785. public Led3D get3D() {
  786. return ledView;
  787. }
  788. public static void main(String[] args) {
  789. Frame f = new Frame("Cube Control");
  790. Led3D l = f.get3D();
  791. java.util.Scanner sc = new java.util.Scanner(System.in);
  792. System.out.println("#### Cube Control Debug Console ####");
  793. System.out.println("## Enter a Command ('h' for help) ##");
  794. System.out.print("$> ");
  795. do {
  796. if (sc.hasNextLine()) {
  797. String s = sc.nextLine();
  798. if (s.equals("p") || (s.equals("print")))
  799. l.printTranslationData();
  800. if (s.equals("q") || s.equals("quit"))
  801. System.exit(0);
  802. if (s.equals("on") || s.equals("1")) {
  803. short[] d = new short[64];
  804. for (int i = 0; i < d.length; i++) {
  805. d[i] = 0xFF;
  806. }
  807. l.setData(d);
  808. System.out.println("All LEDs on now...");
  809. }
  810. if (s.equals("off") || s.equals("0")) {
  811. short[] d = new short[64];
  812. for (int i = 0; i < d.length; i++) {
  813. d[i] = 0x00;
  814. }
  815. l.setData(d);
  816. System.out.println("All LEDs off now...");
  817. }
  818. if (s.equals("r") || s.equals("reset")) {
  819. l.resetView();
  820. }
  821. if (s.startsWith("port ")) {
  822. s = s.substring(5);
  823. f.jComboBox1.addItem(s);
  824. f.jComboBox1.setSelectedItem(s);
  825. }
  826. if (s.startsWith("send ")) {
  827. s = s.substring(5);
  828. HelperUtility.openPort(((String) f.jComboBox1
  829. .getSelectedItem()) + "\n");
  830. short[] dat = toShortArray(s);
  831. HelperUtility.writeData(dat, dat.length);
  832. HelperUtility.closePort();
  833. }
  834. if (s.startsWith("read ")) {
  835. s = s.substring(5);
  836. int leng = Integer.parseInt(s);
  837. HelperUtility.openPort((String) f.jComboBox1
  838. .getSelectedItem());
  839. short[] data = HelperUtility.readData(leng);
  840. System.out.println(shortToString(data));
  841. HelperUtility.closePort();
  842. }
  843. if (s.equals("s") || s.equals("scan")) {
  844. String[] sPorts = f.worker.getSerialPorts();
  845. f.jComboBox1.removeAllItems();
  846. for (int i = 0; i < sPorts.length; i++) {
  847. f.jComboBox1.addItem(sPorts[i]);
  848. }
  849. }
  850. if (s.equals("h") || (s.equals("help"))) {
  851. System.out.println("Commands:");
  852. System.out
  853. .println("\t'port *name*'\t:\tSet serial port to this");
  854. System.out
  855. .println("\t'send *datas*'\t:\tSend data to serial port");
  856. System.out
  857. .println("\t'read *leng*'\t:\tRead data from serial port");
  858. System.out
  859. .println("\t'scan' / 's'\t:\tScan for serial ports");
  860. System.out
  861. .println("\t'on' / '1'\t:\tToggle all LEDs on");
  862. System.out
  863. .println("\t'off' / '0'\t:\tToggle all LEDs off");
  864. System.out
  865. .println("\t'print' / 'p'\t:\tPrint 3D Translation Matrix Data");
  866. System.out
  867. .println("\t'reset' / 'r'\t:\tReset rotation of cube");
  868. System.out
  869. .println("\t'help' / 'h'\t:\tShow this message");
  870. System.out
  871. .println("\t'quit' / 'q'\t:\tExit Cube Control");
  872. }
  873. System.out.print("$> ");
  874. }
  875. } while (true);
  876. }
  877. private static short[] toShortArray(String s) {
  878. char[] d = s.toCharArray();
  879. System.out.println("Length: " + d.length);
  880. short[] r = new short[d.length];
  881. for (int i = 0; i < d.length; i++) {
  882. r[i] = (short) d[i];
  883. }
  884. return r;
  885. }
  886. private static String shortToString(short[] d) {
  887. String s = "";
  888. for (int i = 0; i < d.length; i++) {
  889. s += String.valueOf((char) d[i]);
  890. }
  891. return s;
  892. }
  893. }