Simple single-color 8x8x8 LED Cube with AVRs
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Frame.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. import javax.vecmath.Point2d;
  32. import javax.vecmath.Point3d;
  33. import java.util.Arrays;
  34. public class Frame extends JFrame implements ListSelectionListener, ChangeListener{
  35. private GraphicsConfiguration gConfig;
  36. private Canvas3D cubeCanvas;
  37. public Led3D ledView;
  38. private JButton editA = new JButton();
  39. private JButton editB = new JButton();
  40. private JButton editC = new JButton();
  41. private JButton editD = new JButton();
  42. private JButton editE = new JButton();
  43. private JButton editF = new JButton();
  44. private JButton editG = new JButton();
  45. private JButton editH = new JButton();
  46. private JButton[] editButtons = { editA, editB, editC, editD, editE, editF, editG, editH };
  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 animListModel = 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 serialPortSelector = new JComboBox();
  68. private JButton upload = new JButton();
  69. private JButton download = new JButton();
  70. private JLabel remainingLabel = 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. private JPanel previewPanel = new JPanel();
  80. private JPanel framePanel = new JPanel();
  81. private JPanel animPanel = new JPanel();
  82. private JPanel filePanel = new JPanel();
  83. private JPanel serialPanel = new JPanel();
  84. private JPanel settingsPanel = new JPanel();
  85. private JSlider durationSlider = new JSlider(1, 256 ,1); //min, max, value
  86. private JLabel logoText = new JLabel();
  87. private int lastSelectedFrame = 0;
  88. private int lastSelectedAnim = 0;
  89. public cubeWorker worker = new cubeWorker(this);
  90. private boolean fileSelected = false;
  91. private Frame thisFrame;
  92. private static Frame recentFrame;
  93. private int saveExitDialog() {
  94. String[] Optionen = { "Yes", "No" };
  95. int Auswahl = JOptionPane.showOptionDialog(this,
  96. "Do you want to save your changes?", "Save?",
  97. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  98. Optionen, Optionen[0]);
  99. if (Auswahl == JOptionPane.YES_OPTION) {
  100. return 1;
  101. } else {
  102. return 0;
  103. }
  104. }
  105. /**
  106. * Ask the user to enter a string via this Frame.
  107. * @param title Title of the message box
  108. * @param text Text in the message box
  109. * @return String user entered
  110. */
  111. public String askString(String title, String text) {
  112. return JOptionPane.showInputDialog(null, text, title,
  113. JOptionPane.QUESTION_MESSAGE);
  114. }
  115. /**
  116. * Show an error message to the user via this Frame.
  117. * @param s Error Message
  118. */
  119. public void errorMessage(String s) {
  120. String[] Optionen = { "OK" };
  121. JOptionPane.showOptionDialog(this, s, "Error!", JOptionPane.YES_OPTION,
  122. JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
  123. }
  124. /**
  125. * Show an error message to the user via this Frame.
  126. * @param title Title of message box
  127. * @param msg Error message.
  128. */
  129. public void errorMessage(String title, String msg) {
  130. String[] Optionen = { "OK" };
  131. JOptionPane.showOptionDialog(this, msg, title, JOptionPane.YES_OPTION,
  132. JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
  133. }
  134. /**
  135. * Show an error message to the user via the most recent Frame.
  136. * @param s Error Message
  137. */
  138. public static void errorMessageStat(String s) {
  139. recentFrame.errorMessage(s);
  140. }
  141. /**
  142. * Show an error message to the user via the most recent Frame.
  143. * @param title Title of message box
  144. * @param msg Error message.
  145. */
  146. public static void errorMessageStat(String title, String msg) {
  147. recentFrame.errorMessage(title, msg);
  148. }
  149. /**
  150. * Toggle a LED in the current selected anim & frame
  151. * @param x X Coordinate
  152. * @param y Y Coord.
  153. * @param z Z Coord.
  154. */
  155. public void toggleLED(int x, int y, int z) {
  156. if ((animList.getSelectedIndex() != -1) && (frameList.getSelectedIndex() != -1)) {
  157. worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).toggleLED(x, y, z);
  158. ledView.setData(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getData());
  159. }
  160. }
  161. private void rebuildFrameList() {
  162. // animList selection changed, update frameList
  163. frameListModel.clear();
  164. // System.out.println("Selected Animation: " + animList.getSelectedIndex());
  165. int max = worker.getAnimation(animList.getSelectedIndex()).size();
  166. for (int i = 0; i < max; i++) {
  167. frameListModel.addElement(worker.getAnimation(
  168. animList.getSelectedIndex()).getFrame(i).getName());
  169. }
  170. frameList.setModel(frameListModel);
  171. }
  172. /**
  173. * ListSelectionListener that updates Anim & Frame List
  174. * and reloads the data for the 3d view
  175. *
  176. * @param evt List that generated the event. Has to be animList or frameList
  177. */
  178. public void valueChanged(ListSelectionEvent evt) {
  179. if ((!evt.getValueIsAdjusting()) && ((evt.getSource() == animList) || (evt.getSource() == frameList))) {
  180. // Select an animation, if not already done
  181. if (animList.getSelectedIndex() == -1) {
  182. if ((animListModel.getSize() > lastSelectedAnim) && (lastSelectedAnim != -1)) {
  183. animList.setSelectedIndex(lastSelectedAnim);
  184. } else {
  185. animList.setSelectedIndex(0);
  186. }
  187. }
  188. // Rebuild the frame list, if possible & needed
  189. if ((evt.getSource() == animList) && (animList.getSelectedIndex() != -1)) {
  190. rebuildFrameList();
  191. }
  192. // Select a frame, if not already done
  193. if (frameList.getSelectedIndex() == -1) {
  194. if ((frameListModel.getSize() > lastSelectedFrame) && (lastSelectedFrame != -1)) {
  195. frameList.setSelectedIndex(lastSelectedFrame);
  196. } else {
  197. frameList.setSelectedIndex(0);
  198. }
  199. lastSelectedFrame = frameList.getSelectedIndex();
  200. }
  201. // If possible, set all data for selected frame
  202. if ((animList.getSelectedIndex() != -1) && (frameList.getSelectedIndex() != -1)) {
  203. ledView.setData(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getData());
  204. frameLengthText.setText(Integer.toString(worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime()));
  205. durationSlider.setValue(Integer.parseInt(frameLengthText.getText()));
  206. }
  207. if (frameList.getSelectedIndex() != -1) {
  208. lastSelectedAnim = animList.getSelectedIndex();
  209. }
  210. if (frameList.getSelectedIndex() != -1) {
  211. lastSelectedFrame = frameList.getSelectedIndex();
  212. }
  213. if (lastSelectedAnim == -1) {
  214. lastSelectedAnim = 0;
  215. }
  216. if (lastSelectedFrame == -1) {
  217. lastSelectedFrame = 0;
  218. }
  219. }
  220. }
  221. public void stateChanged(ChangeEvent e){
  222. frameLengthText.setText(durationSlider.getValue() + "");
  223. }
  224. private void save() {
  225. if (fileSelected == false) {
  226. JFileChooser fc = new JFileChooser();
  227. int ret = fc.showSaveDialog(this);
  228. if (ret == JFileChooser.APPROVE_OPTION) {
  229. File file = fc.getSelectedFile();
  230. fileSelected = true;
  231. animPath.setText(file.getPath());
  232. worker.saveState(animPath.getText());
  233. }
  234. } else {
  235. worker.saveState(animPath.getText());
  236. }
  237. }
  238. private void showLayerEditFrame(int num) {
  239. if (animList.getSelectedIndex() == -1) {
  240. errorMessage("Please select an animation.");
  241. } else if (frameList.getSelectedIndex() == -1) {
  242. errorMessage("Please select a Frame.");
  243. } else {
  244. // Show edit frame
  245. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), num, worker, thisFrame);
  246. }
  247. }
  248. public Frame(String title) {
  249. super(title);
  250. thisFrame = this;
  251. recentFrame = this;
  252. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  253. // Build serial port selection
  254. String[] sPorts = HelperUtility.getPorts();
  255. for (int i = 0; i < sPorts.length; i++) {
  256. serialPortSelector.addItem(sPorts[i]);
  257. }
  258. // Build Animation List
  259. for (int i = 0; i < worker.size(); i++) {
  260. animListModel.addElement(worker.getAnimation(i).getName());
  261. }
  262. // Ask user to save before closing the window
  263. addWindowListener(new WindowAdapter() {
  264. public void windowClosing(WindowEvent evt) {
  265. if (worker.changedStateSinceSave()) {
  266. if (saveExitDialog() == 1) {
  267. save();
  268. } else {
  269. return;
  270. }
  271. }
  272. System.exit(0);
  273. }
  274. });
  275. // Set window size & position
  276. int frameWidth = 672;
  277. int frameHeight = 646;
  278. setSize(frameWidth, frameHeight);
  279. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  280. int x = (d.width - frameWidth) / 2;
  281. int y = (d.height - frameHeight) / 2;
  282. setLocation(x, y);
  283. Container cp = getContentPane();
  284. cp.setLayout(null);
  285. // ----- 3D Stuff -----
  286. // --------------------
  287. gConfig = SimpleUniverse.getPreferredConfiguration();
  288. cubeCanvas = new Canvas3D(gConfig);
  289. cubeCanvas.setBounds(18, 31, 275, 275); // 3d view
  290. ledView = new Led3D(cubeCanvas, this);
  291. cp.add(cubeCanvas);
  292. // --------------------
  293. // Add Layer edit buttons
  294. for (int i = 0; i < 8; i++) {
  295. final String nameStart = "Layer ";
  296. editButtons[i].setText(nameStart + (char)('A' + i));
  297. editButtons[i].setBounds(299, 32 + (35 * i), 102, 29);
  298. cp.add(editButtons[i]);
  299. editButtons[i].addActionListener(new ActionListener() {
  300. public void actionPerformed(ActionEvent evt) {
  301. JButton tmp = (JButton)evt.getSource();
  302. String name = tmp.getText();
  303. name = name.substring(nameStart.length());
  304. // Name now is one of A - H
  305. char c = name.charAt(0);
  306. c -= 'A';
  307. showLayerEditFrame((int)c);
  308. }
  309. });
  310. }
  311. // Add Frame List
  312. frameListScrollPane.setBounds(339, 379, 187, 218);
  313. frameList.setModel(frameListModel);
  314. cp.add(frameListScrollPane);
  315. // Add Animation List
  316. animScrollPane.setBounds(18, 378, 187, 219);
  317. animList.setModel(animListModel);
  318. cp.add(animScrollPane);
  319. // Add Move Frame Up Button
  320. frameUp.setBounds(532, 378, 110, 39);
  321. frameUp.setText("Move up");
  322. cp.add(frameUp);
  323. frameUp.addActionListener(new ActionListener() {
  324. public void actionPerformed(ActionEvent evt) {
  325. int i = frameList.getSelectedIndex();
  326. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  327. Object tmp = frameListModel.get(i);
  328. frameListModel.set(i, frameListModel.get(i - 1));
  329. frameListModel.set(i - 1, tmp);
  330. frameList.setSelectedIndex(i - 1);
  331. worker.getAnimation(animList.getSelectedIndex()).moveFrameUp(i);
  332. }
  333. }
  334. });
  335. // Add Move Frame Down Button
  336. frameDown.setBounds(532, 558, 110, 39);
  337. frameDown.setText("Move down");
  338. cp.add(frameDown);
  339. frameDown.addActionListener(new ActionListener() {
  340. public void actionPerformed(ActionEvent evt) {
  341. int i = frameList.getSelectedIndex();
  342. if ((i >= 0) && (frameListModel.getSize() >= 2) && (i < (frameListModel.getSize() - 1))) {
  343. Object tmp = frameListModel.get(i);
  344. frameListModel.set(i, frameListModel.get(i + 1));
  345. frameListModel.set(i + 1, tmp);
  346. frameList.setSelectedIndex(i + 1);
  347. worker.getAnimation(animList.getSelectedIndex()).moveFrameDown(i);
  348. }
  349. }
  350. });
  351. // Add 'Frame Add' Button
  352. frameAdd.setBounds(532, 423, 110, 39);
  353. frameAdd.setText("Add");
  354. cp.add(frameAdd);
  355. frameAdd.addActionListener(new ActionListener() {
  356. public void actionPerformed(ActionEvent evt) {
  357. if (animList.getSelectedIndex() == -1) {
  358. errorMessage("Please select an animation!");
  359. } else {
  360. int n = worker.getAnimation(animList.getSelectedIndex()).size();
  361. worker.getAnimation(animList.getSelectedIndex()).addFrame();
  362. frameRemaining.setText(Integer.toString(worker.memoryRemaining()));
  363. frameListModel.add(n, worker.getAnimation(animList.getSelectedIndex()).getFrame(n).getName());
  364. frameList.setModel(frameListModel);
  365. }
  366. }
  367. });
  368. // Add button for frame removal
  369. frameRemove.setBounds(532, 468, 110, 39);
  370. frameRemove.setText("Remove");
  371. cp.add(frameRemove);
  372. frameRemove.addActionListener(new ActionListener() {
  373. public void actionPerformed(ActionEvent evt) {
  374. if (animList.getSelectedIndex() == -1) {
  375. errorMessage("Select an animation.");
  376. } else if (frameList.getSelectedIndex() == -1) {
  377. errorMessage("Select a Frame.");
  378. } else {
  379. worker.getAnimation(animList.getSelectedIndex()).removeFrame(frameList.getSelectedIndex());
  380. frameRemaining.setText(Integer.toString(worker.memoryRemaining()));
  381. frameListModel.removeElementAt(frameList.getSelectedIndex());
  382. frameList.setModel(frameListModel);
  383. }
  384. }
  385. });
  386. // Add frame rename button
  387. frameRename.setBounds(532, 513, 110, 39);
  388. frameRename.setText("Rename");
  389. cp.add(frameRename);
  390. frameRename.addActionListener(new ActionListener() {
  391. public void actionPerformed(ActionEvent evt) {
  392. int a = animList.getSelectedIndex();
  393. if (a < 0) {
  394. errorMessage("Select an animation!");
  395. return;
  396. }
  397. int f = frameList.getSelectedIndex();
  398. if (f < 0) {
  399. errorMessage("Select a Frame!");
  400. return;
  401. }
  402. worker.getAnimation(a).getFrame(f).setName(askString("Rename", "Rename " + frameList.getSelectedValue() + "?"));
  403. frameListModel.set(f, worker.getAnimation(a).getFrame(f).getName());
  404. frameList.setModel(frameListModel);
  405. }
  406. });
  407. // Add Move Animation Up Button
  408. animUp.setBounds(211, 378, 110, 39);
  409. animUp.setText("Move up");
  410. cp.add(animUp);
  411. animUp.addActionListener(new ActionListener() {
  412. public void actionPerformed(ActionEvent evt) {
  413. int i = animList.getSelectedIndex();
  414. if ((i > 0) && (animListModel.getSize() >= 2)) {
  415. Object tmp = animListModel.get(i);
  416. animListModel.set(i, animListModel.get(i - 1));
  417. animListModel.set(i - 1, tmp);
  418. animList.setSelectedIndex(i - 1);
  419. worker.moveAnimationUp(i);
  420. }
  421. }
  422. });
  423. // Add Move Animation Down Button
  424. animDown.setBounds(211, 558, 110, 39);
  425. animDown.setText("Move down");
  426. cp.add(animDown);
  427. animDown.addActionListener(new ActionListener() {
  428. public void actionPerformed(ActionEvent evt) {
  429. int i = animList.getSelectedIndex();
  430. if ((i >= 0) && (animListModel.getSize() >= 2) && (i < (animListModel.getSize() - 1))) {
  431. Object tmp = animListModel.get(i);
  432. animListModel.set(i, animListModel.get(i + 1));
  433. animListModel.set(i + 1, tmp);
  434. animList.setSelectedIndex(i + 1);
  435. worker.moveAnimationDown(i);
  436. }
  437. }
  438. });
  439. // Add Rename Animation Button
  440. animRename.setBounds(211, 513, 110, 39);
  441. animRename.setText("Rename");
  442. cp.add(animRename);
  443. animRename.addActionListener(new ActionListener() {
  444. public void actionPerformed(ActionEvent evt) {
  445. int a = animList.getSelectedIndex();
  446. if (a < 0) {
  447. errorMessage("Select an animation!");
  448. return;
  449. }
  450. worker.getAnimation(a).setName(askString("Rename", "Rename " + animList.getSelectedValue() + "?"));
  451. animListModel.set(a, worker.getAnimation(a).getName());
  452. animList.setModel(animListModel);
  453. }
  454. });
  455. // Add button for adding animations
  456. animAdd.setBounds(211, 423, 110, 39);
  457. animAdd.setText("Add");
  458. cp.add(animAdd);
  459. animAdd.addActionListener(new ActionListener() {
  460. public void actionPerformed(ActionEvent evt) {
  461. if (worker.addAnimation() == -1) {
  462. errorMessage("Could not add animation!");
  463. } else {
  464. animListModel.clear();
  465. for (int i = 0; i < worker.size(); i++) {
  466. animListModel.add(i, worker.getAnimation(i).getName());
  467. }
  468. animList.setModel(animListModel);
  469. }
  470. }
  471. });
  472. // Add Animation remove button
  473. animRemove.setBounds(211, 468, 110, 39);
  474. animRemove.setText("Remove");
  475. cp.add(animRemove);
  476. animRemove.addActionListener(new ActionListener() {
  477. public void actionPerformed(ActionEvent evt) {
  478. if (animList.getSelectedIndex() == -1) {
  479. errorMessage("Select an animation.");
  480. } else {
  481. worker.removeAnimation(animList.getSelectedIndex());
  482. animListModel.removeElementAt(animList.getSelectedIndex());
  483. animList.setModel(animListModel);
  484. }
  485. }
  486. });
  487. fullScreenButton.setText("Fullscreen");
  488. fullScreenButton.setBounds(18, 312, 134, 35);
  489. cp.add(fullScreenButton);
  490. fullScreenButton.addActionListener(new ActionListener() {
  491. public void actionPerformed(ActionEvent evt) {
  492. ledView.enterFullscreen();
  493. setLocation(0,0);
  494. setSize(700, 700);
  495. int w = Toolkit.getDefaultToolkit().getScreenSize().width;
  496. int h = Toolkit.getDefaultToolkit().getScreenSize().height;
  497. setSize(w - 5, h - 30);
  498. playAnimationFullscreen.setVisible(true);
  499. cubeCanvas.setBounds(0, 0, w - 5, h - 80);
  500. }
  501. });
  502. exitButton = new JButton("Exit Fullscreen");
  503. exitButton.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-150, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  504. cp.add(exitButton);
  505. exitButton.addActionListener(new ActionListener() {
  506. public void actionPerformed(ActionEvent evt) {
  507. playAnimationFullscreen.setVisible(false);
  508. setLocation(0,0);
  509. setSize(672, 656);
  510. ledView.leaveFullscreen();
  511. cubeCanvas.setBounds(18, 31, 275, 275);
  512. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  513. int x = (d.width - getSize().width) / 2;
  514. int y = (d.height - getSize().height) / 2;
  515. setLocation(x, y);
  516. }
  517. });
  518. playAnimation.setText("Play");
  519. playAnimation.setBounds(159, 312, 134, 35);
  520. cp.add(playAnimation);
  521. playAnimation.addActionListener(new ActionListener() {
  522. public void actionPerformed(ActionEvent evt){
  523. if (animList.getSelectedIndex() == -1) {
  524. errorMessage("Please select an animation.");
  525. } else if (frameList.getSelectedIndex() == -1) {
  526. errorMessage("Please select a Frame.");
  527. } else {
  528. for (int i = 0; i < frameList.getModel().getSize(); i++){
  529. frameList.setSelectedIndex(i);
  530. long time1 = (long) worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  531. long time = (long) (((time1+1) * 1000) / 24);
  532. System.out.println("Wert: " + time1 + " Zeit: " + time);
  533. try {
  534. Thread.sleep(time);
  535. } catch (Exception e) {
  536. System.out.println(e);
  537. }
  538. }
  539. }
  540. }
  541. });
  542. playAnimationFullscreen.setText("Play");
  543. playAnimationFullscreen.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-310, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  544. playAnimationFullscreen.setVisible(false);
  545. cp.add(playAnimationFullscreen);
  546. playAnimationFullscreen.addActionListener(new ActionListener() {
  547. public void actionPerformed(ActionEvent evt){
  548. if (animList.getSelectedIndex() == -1) {
  549. errorMessage("Please select an animation.");
  550. } else if (frameList.getSelectedIndex() == -1) {
  551. errorMessage("Please select a Frame.");
  552. } else {
  553. for (int i = 0; i < frameList.getModel().getSize(); i++){
  554. frameList.setSelectedIndex(i);
  555. short time1 = worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  556. long time = (long) (((time1+1) * 1/24) * 1000);
  557. try {
  558. Thread.sleep(time);
  559. } catch(Exception e) {
  560. System.out.println(e);
  561. }
  562. }
  563. }
  564. }
  565. });
  566. frameDuration.setBounds(462, 129, 65, 20);
  567. frameDuration.setText("Save");
  568. cp.add(frameDuration);
  569. frameDuration.addActionListener(new ActionListener() {
  570. public void actionPerformed(ActionEvent evt) {
  571. if (frameLengthText.getText().equals("0")) {
  572. errorMessage("0 is not a valid value!");
  573. frameLengthText.setText("1");
  574. } else if (Integer.parseInt(frameLengthText.getText()) > 256) {
  575. errorMessage("Value too high. Max: 256");
  576. frameLengthText.setText("256");
  577. return;
  578. } else {
  579. if (animList.getSelectedIndex() == -1) {
  580. errorMessage("Please select an animation!");
  581. return;
  582. }
  583. if (frameList.getSelectedIndex() == -1) {
  584. errorMessage("Please select a Frame!");
  585. return;
  586. }
  587. durationSlider.setValue(Integer.parseInt(frameLengthText.getText()));
  588. worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).setTime((short)(Integer.parseInt(frameLengthText.getText())));
  589. }
  590. }
  591. });
  592. animPath.setBounds(417, 281, 228, 20);
  593. animPath.setEditable(false);
  594. animPath.setText("Select an animation file...");
  595. cp.add(animPath);
  596. load.setBounds(417, 307, 72, 39);
  597. load.setText("Load");
  598. cp.add(load);
  599. load.addActionListener(new ActionListener() {
  600. public void actionPerformed(ActionEvent evt) {
  601. JFileChooser fc = new JFileChooser();
  602. int ret = fc.showOpenDialog(thisFrame);
  603. if (ret == JFileChooser.APPROVE_OPTION) {
  604. File file = fc.getSelectedFile();
  605. if (fileSelected == false) {
  606. fileSelected = true;
  607. }
  608. animPath.setText(file.getPath());
  609. worker.loadState(animPath.getText());
  610. animListModel.clear();
  611. for (int i = 0; i < worker.size(); i++) {
  612. animListModel.addElement(worker.getAnimation(i).getName());
  613. }
  614. animList.setModel(animListModel);
  615. animList.setSelectedIndex(0);
  616. rebuildFrameList();
  617. }
  618. }
  619. });
  620. save.setBounds(491, 307, 72, 39);
  621. save.setText("Save");
  622. cp.add(save);
  623. save.addActionListener(new ActionListener() {
  624. public void actionPerformed(ActionEvent evt) {
  625. if (fileSelected == false) {
  626. JFileChooser fc = new JFileChooser();
  627. int ret = fc.showSaveDialog(thisFrame);
  628. if (ret == JFileChooser.APPROVE_OPTION) {
  629. File file = fc.getSelectedFile();
  630. fileSelected = true;
  631. animPath.setText(file.getPath());
  632. worker.saveState(animPath.getText());
  633. }
  634. } else {
  635. worker.saveState(animPath.getText());
  636. }
  637. }
  638. });
  639. saveAs.setBounds(565, 307, 82, 39);
  640. saveAs.setText("Save As");
  641. cp.add(saveAs);
  642. saveAs.addActionListener(new ActionListener() {
  643. public void actionPerformed(ActionEvent evt) {
  644. JFileChooser fc;
  645. if (fileSelected == true) {
  646. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  647. } else {
  648. fc = new JFileChooser();
  649. }
  650. int ret = fc.showSaveDialog(thisFrame);
  651. if (ret == JFileChooser.APPROVE_OPTION) {
  652. File file = fc.getSelectedFile();
  653. if (fileSelected == false) {
  654. fileSelected = true;
  655. }
  656. animPath.setText(file.getPath());
  657. worker.saveState(animPath.getText());
  658. }
  659. }
  660. });
  661. serialPortSelector.setBounds(417, 182, 228, 25);
  662. cp.add(serialPortSelector);
  663. upload.setBounds(417, 212, 111, 39);
  664. upload.setText("Upload");
  665. cp.add(upload);
  666. upload.addActionListener(new ActionListener() {
  667. public void actionPerformed(ActionEvent evt) {
  668. // Error messages are generated by worker
  669. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  670. worker.cubeSendState((String) serialPortSelector.getSelectedItem());
  671. }
  672. }
  673. });
  674. download.setBounds(534, 212, 111, 39);
  675. download.setText("Download");
  676. cp.add(download);
  677. download.addActionListener(new ActionListener() {
  678. public void actionPerformed(ActionEvent evt) {
  679. // Error messages are generated by worker
  680. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  681. worker.cubeGetState((String) serialPortSelector.getSelectedItem());
  682. }
  683. }
  684. });
  685. frameLengthLabel.setBounds(420, 97, 130, 13);
  686. frameLengthLabel.setText("Time (1/24 sec)");
  687. cp.add(frameLengthLabel);
  688. frameLengthText.setBounds(419, 129, 40, 20);
  689. cp.add(frameLengthText);
  690. remainingLabel.setBounds(530, 113, 90, 13);
  691. remainingLabel.setText("Remaining:");
  692. cp.add(remainingLabel);
  693. durationSlider.setBounds(417, 108, 115, 23);
  694. durationSlider.addChangeListener(this);
  695. cp.add(durationSlider);
  696. frameRemaining.setBounds(530, 129, 49, 20);
  697. frameRemaining.setEditable(false);
  698. frameRemaining.setText(String.valueOf(worker.memoryRemaining()));
  699. cp.add(frameRemaining);
  700. previewPanel.setBounds(8, 12, 399, 342);
  701. previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
  702. cp.add(previewPanel);
  703. framePanel.setBounds(331, 360, 321, 246);
  704. framePanel.setBorder(BorderFactory.createTitledBorder("Frame"));
  705. cp.add(framePanel);
  706. animPanel.setBounds(8, 360, 321, 246);
  707. animPanel.setBorder(BorderFactory.createTitledBorder("Animation"));
  708. cp.add(animPanel);
  709. filePanel.setBounds(409, 262, 243, 92);
  710. filePanel.setBorder(BorderFactory.createTitledBorder("Animation File"));
  711. cp.add(filePanel);
  712. serialPanel.setBounds(409, 160, 243, 99);
  713. serialPanel.setBorder(BorderFactory.createTitledBorder("Serial communication"));
  714. cp.add(serialPanel);
  715. settingsPanel.setBounds(409, 75, 243, 82);
  716. settingsPanel.setBorder(BorderFactory.createTitledBorder("Frame duration"));
  717. cp.add(settingsPanel);
  718. logoText.setText("CubeControl");
  719. logoText.setBounds(415, 10, 243, 65);
  720. logoText.setFont(new Font("Default", Font.BOLD, 38));
  721. cp.add(logoText);
  722. // Ende Komponenten
  723. animList.addListSelectionListener(this);
  724. frameList.addListSelectionListener(this);
  725. setResizable(false);
  726. setVisible(true);
  727. valueChanged(new ListSelectionEvent(animList, 0, 0, false));
  728. }
  729. public Led3D get3D() {
  730. return ledView;
  731. }
  732. public static void main(String[] args) {
  733. Frame f = new Frame("Cube Control");
  734. Led3D l = f.get3D();
  735. String lastCommand = null;
  736. java.util.Scanner sc = new java.util.Scanner(System.in);
  737. System.out.println("#### Cube Control Debug Console ####");
  738. System.out.println("## Enter a Command ('h' for help) ##");
  739. System.out.print("$> ");
  740. do {
  741. if (sc.hasNextLine()) {
  742. String s = sc.nextLine();
  743. // Up arrow key
  744. if (s.equals("\u001B[A")) {
  745. if (lastCommand != null) {
  746. System.out.println("Last command: " + lastCommand);
  747. s = new String(lastCommand);
  748. } else {
  749. System.out.println("No last command!");
  750. }
  751. }
  752. if (s.equals("p") || (s.equals("print")))
  753. l.printTranslationData();
  754. if (s.equals("q") || s.equals("quit"))
  755. System.exit(0);
  756. if (s.equals("r") || s.equals("reset"))
  757. l.resetView();
  758. if (s.equals("on") || s.equals("1")) {
  759. short[] d = new short[64];
  760. for (int i = 0; i < d.length; i++) {
  761. d[i] = 0xFF;
  762. }
  763. l.setData(d);
  764. System.out.println("All LEDs on now...");
  765. }
  766. if (s.equals("off") || s.equals("0")) {
  767. short[] d = new short[64];
  768. for (int i = 0; i < d.length; i++) {
  769. d[i] = 0x00;
  770. }
  771. l.setData(d);
  772. System.out.println("All LEDs off now...");
  773. }
  774. if (s.startsWith("port ")) {
  775. f.serialPortSelector.addItem(s.substring(5));
  776. f.serialPortSelector.setSelectedItem(s.substring(5));
  777. }
  778. if (s.startsWith("send ")) {
  779. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  780. short[] dat = toShortArray(s.substring(5));
  781. HelperUtility.writeData(dat, dat.length);
  782. HelperUtility.closePort();
  783. }
  784. if (s.startsWith("read ")) {
  785. int leng = Integer.parseInt(s.substring(5));
  786. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  787. short[] data = HelperUtility.readData(leng);
  788. System.out.println(shortToString(data));
  789. HelperUtility.closePort();
  790. }
  791. if (s.startsWith("frames ")) {
  792. int anim = Integer.parseInt(s.substring(7));
  793. if (anim >= f.worker.size()) {
  794. System.out.println("Animation does not exist! Max: " + (f.worker.size() - 1));
  795. } else {
  796. System.out.println("Animation: " + f.worker.getAnimation(anim).getName());
  797. for (int i = 0; i < f.worker.getAnimation(anim).size(); i++) {
  798. AFrame frame = f.worker.getAnimation(anim).getFrame(i);
  799. System.out.println("\tFrame (" + frame.getTime() + "): " + frame.getName());
  800. }
  801. }
  802. }
  803. if (s.equals("a") || s.equals("anims")) {
  804. for (int i = 0; i < f.worker.size(); i++) {
  805. Animation anim = f.worker.getAnimation(i);
  806. System.out.println("\tAnimation: " + anim.getName());
  807. }
  808. }
  809. if (s.equals("s") || s.equals("scan")) {
  810. String[] sPorts = HelperUtility.getPorts();
  811. f.serialPortSelector.removeAllItems();
  812. for (int i = 0; i < sPorts.length; i++) {
  813. System.out.println("Port " + i + ": " + sPorts[i]);
  814. f.serialPortSelector.addItem(sPorts[i]);
  815. }
  816. }
  817. if (s.equals("h") || (s.equals("help"))) {
  818. System.out.println("Commands:");
  819. System.out.println("\t'port *name*'\t:\tSet serial port to this");
  820. System.out.println("\t'send *datas*'\t:\tSend data to serial port");
  821. System.out.println("\t'read *leng*'\t:\tRead data from serial port");
  822. System.out.println("\t'scan' / 's'\t:\tScan for serial ports");
  823. System.out.println("\t'on' / '1'\t:\tToggle all LEDs on");
  824. System.out.println("\t'off'/ '0'\t:\tToggle all LEDs off");
  825. System.out.println("\t'print'/ 'p'\t:\tPrint 3D Translation Matrix Data");
  826. System.out.println("\t'reset'/ 'r'\t:\tReset rotation of cube");
  827. System.out.println("\t'anims'/ 'a'\t:\tPrint animation tree");
  828. System.out.println("\t'frames *anim*\t:\tPrint frame tree");
  829. System.out.println("\t'help' / 'h'\t:\tShow this message");
  830. System.out.println("\t'quit' / 'q'\t:\tExit Cube Control");
  831. }
  832. lastCommand = new String(s);
  833. System.out.print("$> ");
  834. }
  835. } while (true);
  836. }
  837. private static short[] toShortArray(String s) {
  838. char[] d = s.toCharArray();
  839. System.out.println("Length: " + d.length);
  840. short[] r = new short[d.length];
  841. for (int i = 0; i < d.length; i++) {
  842. r[i] = (short) d[i];
  843. }
  844. return r;
  845. }
  846. private static String shortToString(short[] d) {
  847. String s = "";
  848. for (int i = 0; i < d.length; i++) {
  849. s += String.valueOf((char) d[i]);
  850. }
  851. return s;
  852. }
  853. }