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

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