Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Frame.java 32KB

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