Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Frame.java 32KB

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