Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Frame.java 32KB

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