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

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