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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. String lastCommand = null;
  840. java.util.Scanner sc = new java.util.Scanner(System.in);
  841. System.out.println("#### Cube Control Debug Console ####");
  842. System.out.println("## Enter a Command ('h' for help) ##");
  843. System.out.print("$> ");
  844. do {
  845. if (sc.hasNextLine()) {
  846. String s = sc.nextLine();
  847. // Up arrow key
  848. if (s.equals("\u001B[A")) {
  849. if (lastCommand != null) {
  850. System.out.println("Last command: " + lastCommand);
  851. s = new String(lastCommand);
  852. } else {
  853. System.out.println("No last command!");
  854. }
  855. }
  856. if (s.equals("p") || (s.equals("print")))
  857. l.printTranslationData();
  858. if (s.equals("q") || s.equals("quit"))
  859. System.exit(0);
  860. if (s.equals("on") || s.equals("1")) {
  861. short[] d = new short[64];
  862. for (int i = 0; i < d.length; i++) {
  863. d[i] = 0xFF;
  864. }
  865. l.setData(d);
  866. System.out.println("All LEDs on now...");
  867. }
  868. if (s.equals("off") || s.equals("0")) {
  869. short[] d = new short[64];
  870. for (int i = 0; i < d.length; i++) {
  871. d[i] = 0x00;
  872. }
  873. l.setData(d);
  874. System.out.println("All LEDs off now...");
  875. }
  876. if (s.equals("r") || s.equals("reset")) {
  877. l.resetView();
  878. }
  879. if (s.startsWith("port ")) {
  880. f.jComboBox1.addItem(s.substring(5));
  881. f.jComboBox1.setSelectedItem(s.substring(5));
  882. }
  883. if (s.startsWith("send ")) {
  884. HelperUtility.openPort((String) f.jComboBox1
  885. .getSelectedItem());
  886. short[] dat = toShortArray(s.substring(5));
  887. HelperUtility.writeData(dat, dat.length);
  888. HelperUtility.closePort();
  889. }
  890. if (s.startsWith("read ")) {
  891. int leng = Integer.parseInt(s.substring(5));
  892. HelperUtility.openPort((String) f.jComboBox1
  893. .getSelectedItem());
  894. short[] data = HelperUtility.readData(leng);
  895. System.out.println(shortToString(data));
  896. HelperUtility.closePort();
  897. }
  898. if (s.startsWith("frames ")) {
  899. int anim = Integer.parseInt(s.substring(7));
  900. if (anim >= f.worker.numOfAnimations()) {
  901. System.out.println("Animation does not exist! Max: " + (f.worker.numOfAnimations() - 1));
  902. } else {
  903. System.out.println("Animation: " + f.worker.getAnimationName(anim));
  904. for (int i = 0; i < f.worker.numOfFrames(anim); i++) {
  905. AFrame frame = f.worker.getAnimationList().get(anim).get(i);
  906. System.out.println("\tFrame " + frame.getOrder() + " (" + frame.getTime() + "): " + frame.getName());
  907. }
  908. }
  909. }
  910. if (s.equals("s") || s.equals("scan")) {
  911. String[] sPorts = f.worker.getSerialPorts();
  912. f.jComboBox1.removeAllItems();
  913. for (int i = 0; i < sPorts.length; i++) {
  914. f.jComboBox1.addItem(sPorts[i]);
  915. }
  916. }
  917. if (s.equals("h") || (s.equals("help"))) {
  918. System.out.println("Commands:");
  919. System.out
  920. .println("\t'port *name*'\t:\tSet serial port to this");
  921. System.out
  922. .println("\t'send *datas*'\t:\tSend data to serial port");
  923. System.out
  924. .println("\t'read *leng*'\t:\tRead data from serial port");
  925. System.out
  926. .println("\t'scan' / 's'\t:\tScan for serial ports");
  927. System.out
  928. .println("\t'on' / '1'\t:\tToggle all LEDs on");
  929. System.out
  930. .println("\t'off' / '0'\t:\tToggle all LEDs off");
  931. System.out
  932. .println("\t'print' / 'p'\t:\tPrint 3D Translation Matrix Data");
  933. System.out
  934. .println("\t'reset' / 'r'\t:\tReset rotation of cube");
  935. System.out
  936. .println("\t'anims' / 'a'\t:\tPrint animation tree");
  937. System.out
  938. .println("\t'frames *anim*\t:\tPrint frame tree");
  939. System.out
  940. .println("\t'help' / 'h'\t:\tShow this message");
  941. System.out
  942. .println("\t'quit' / 'q'\t:\tExit Cube Control");
  943. }
  944. lastCommand = new String(s);
  945. System.out.print("$> ");
  946. }
  947. } while (true);
  948. }
  949. private static short[] toShortArray(String s) {
  950. char[] d = s.toCharArray();
  951. System.out.println("Length: " + d.length);
  952. short[] r = new short[d.length];
  953. for (int i = 0; i < d.length; i++) {
  954. r[i] = (short) d[i];
  955. }
  956. return r;
  957. }
  958. private static String shortToString(short[] d) {
  959. String s = "";
  960. for (int i = 0; i < d.length; i++) {
  961. s += String.valueOf((char) d[i]);
  962. }
  963. return s;
  964. }
  965. }