Simple single-color 8x8x8 LED Cube with AVRs
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Frame.java 32KB

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