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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 = SimpleUniverse.getPreferredConfiguration();
  38. private Canvas3D cubeCanvas = new Canvas3D(gConfig);
  39. public Led3D ledView = new Led3D(cubeCanvas);
  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(10, 10);
  243. Container cp = getContentPane();
  244. cp.setLayout(null);
  245. Font font = new Font("Dialog", Font.PLAIN, 13);
  246. // ----- 3D Stuff -----
  247. // --------------------
  248. cubeCanvas.setBounds(18, 31, 275, 275); // 3d view
  249. cubeCanvas.addMouseListener(new MouseListener() { // React to clicks in 3d view
  250. public void mouseClicked(MouseEvent e) {
  251. Point2d mousePos = convertMousePositionToWorld(e.getX(), e.getY());
  252. }
  253. public void mouseExited(MouseEvent e) {
  254. }
  255. public void mouseEntered(MouseEvent e) {
  256. }
  257. public void mouseReleased(MouseEvent e) {
  258. }
  259. public void mousePressed(MouseEvent e) {
  260. }
  261. });
  262. cp.add(cubeCanvas);
  263. // --------------------
  264. // Add Layer edit buttons
  265. for (int i = 0; i < 8; i++) {
  266. final String nameStart = "Layer ";
  267. editButtons[i].setText(nameStart + (char)('A' + i));
  268. editButtons[i].setBounds(299, 32 + (35 * i), 102, 29);
  269. editButtons[i].setFont(font);
  270. cp.add(editButtons[i]);
  271. editButtons[i].addActionListener(new ActionListener() {
  272. public void actionPerformed(ActionEvent evt) {
  273. JButton tmp = (JButton)evt.getSource();
  274. String name = tmp.getText();
  275. name = name.substring(nameStart.length());
  276. // Name now is one of A - H
  277. char c = name.charAt(0);
  278. c -= 'A';
  279. showLayerEditFrame((int)c);
  280. }
  281. });
  282. }
  283. // Add Frame List
  284. frameListScrollPane.setBounds(339, 379, 187, 218);
  285. frameList.setModel(frameListModel);
  286. cp.add(frameListScrollPane);
  287. // Add Animation List
  288. animScrollPane.setBounds(18, 378, 187, 219);
  289. animList.setModel(animModel);
  290. cp.add(animScrollPane);
  291. // Add Move Frame Up Button
  292. frameUp.setBounds(532, 378, 110, 39);
  293. frameUp.setText("Move up");
  294. frameUp.setFont(font);
  295. cp.add(frameUp);
  296. frameUp.addActionListener(new ActionListener() {
  297. public void actionPerformed(ActionEvent evt) {
  298. int i = frameList.getSelectedIndex();
  299. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  300. Object tmp = frameListModel.get(i);
  301. frameListModel.set(i, frameListModel.get(i - 1));
  302. frameListModel.set(i - 1, tmp);
  303. frameList.setSelectedIndex(i - 1);
  304. worker.getAnimation(animList.getSelectedIndex()).moveFrameUp(i);
  305. }
  306. }
  307. });
  308. // Add Move Frame Down Button
  309. frameDown.setBounds(532, 558, 110, 39);
  310. frameDown.setText("Move down");
  311. frameDown.setFont(font);
  312. cp.add(frameDown);
  313. frameDown.addActionListener(new ActionListener() {
  314. public void actionPerformed(ActionEvent evt) {
  315. int i = frameList.getSelectedIndex();
  316. if ((i >= 0) && (frameListModel.getSize() >= 2) && (i < (frameListModel.getSize() - 1))) {
  317. Object tmp = frameListModel.get(i);
  318. frameListModel.set(i, frameListModel.get(i + 1));
  319. frameListModel.set(i + 1, tmp);
  320. frameList.setSelectedIndex(i + 1);
  321. worker.getAnimation(animList.getSelectedIndex()).moveFrameDown(i);
  322. }
  323. }
  324. });
  325. // Add 'Frame Add' Button
  326. frameAdd.setBounds(532, 423, 110, 39);
  327. frameAdd.setText("Add");
  328. frameAdd.setFont(font);
  329. cp.add(frameAdd);
  330. frameAdd.addActionListener(new ActionListener() {
  331. public void actionPerformed(ActionEvent evt) {
  332. if (animList.getSelectedIndex() == -1) {
  333. errorMessage("Please select an animation!");
  334. } else {
  335. int n = worker.getAnimation(animList.getSelectedIndex()).size();
  336. worker.getAnimation(animList.getSelectedIndex()).addFrame();
  337. frameRemaining.setText(Integer.toString(worker.memoryRemaining()));
  338. frameListModel.add(n, worker.getAnimation(animList.getSelectedIndex()).getFrame(n).getName());
  339. frameList.setModel(frameListModel);
  340. }
  341. }
  342. });
  343. // Add button for frame removal
  344. frameRemove.setBounds(532, 468, 110, 39);
  345. frameRemove.setText("Remove");
  346. frameRemove.setFont(font);
  347. cp.add(frameRemove);
  348. frameRemove.addActionListener(new ActionListener() {
  349. public void actionPerformed(ActionEvent evt) {
  350. if (animList.getSelectedIndex() == -1) {
  351. errorMessage("Select an animation.");
  352. } else if (frameList.getSelectedIndex() == -1) {
  353. errorMessage("Select a Frame.");
  354. } else {
  355. worker.getAnimation(animList.getSelectedIndex()).removeFrame(frameList.getSelectedIndex());
  356. frameRemaining.setText(Integer.toString(worker.memoryRemaining()));
  357. frameListModel.removeElementAt(frameList.getSelectedIndex());
  358. frameList.setModel(frameListModel);
  359. }
  360. }
  361. });
  362. // Add frame rename button
  363. frameRename.setBounds(532, 513, 110, 39);
  364. frameRename.setText("Rename");
  365. frameRename.setFont(font);
  366. cp.add(frameRename);
  367. frameRename.addActionListener(new ActionListener() {
  368. public void actionPerformed(ActionEvent evt) {
  369. int a = animList.getSelectedIndex();
  370. if (a < 0) {
  371. errorMessage("Select an animation!");
  372. return;
  373. }
  374. int f = frameList.getSelectedIndex();
  375. if (f < 0) {
  376. errorMessage("Select a Frame!");
  377. return;
  378. }
  379. worker.getAnimation(a).getFrame(f).setName(askString("Rename", "Rename " + frameList.getSelectedValue() + "?"));
  380. frameListModel.set(f, worker.getAnimation(a).getFrame(f).getName());
  381. frameList.setModel(frameListModel);
  382. }
  383. });
  384. // Add Move Animation Up Button
  385. animUp.setBounds(211, 378, 110, 39);
  386. animUp.setText("Move up");
  387. animUp.setFont(font);
  388. cp.add(animUp);
  389. animUp.addActionListener(new ActionListener() {
  390. public void actionPerformed(ActionEvent evt) {
  391. int i = animList.getSelectedIndex();
  392. if ((i > 0) && (animModel.getSize() >= 2)) {
  393. Object tmp = animModel.get(i);
  394. animModel.set(i, animModel.get(i - 1));
  395. animModel.set(i - 1, tmp);
  396. animList.setSelectedIndex(i - 1);
  397. worker.moveAnimationUp(i);
  398. }
  399. }
  400. });
  401. // Add Move Animation Down Button
  402. animDown.setBounds(211, 423, 110, 39);
  403. animDown.setText("Move down");
  404. animDown.setFont(font);
  405. cp.add(animDown);
  406. animDown.addActionListener(new ActionListener() {
  407. public void actionPerformed(ActionEvent evt) {
  408. int i = animList.getSelectedIndex();
  409. if ((i >= 0) && (animModel.getSize() >= 2) && (i < (animModel.getSize() - 1))) {
  410. Object tmp = animModel.get(i);
  411. animModel.set(i, animModel.get(i + 1));
  412. animModel.set(i + 1, tmp);
  413. animList.setSelectedIndex(i + 1);
  414. worker.moveAnimationDown(i);
  415. }
  416. }
  417. });
  418. // Add Rename Animation Button
  419. animRename.setBounds(211, 468, 110, 39);
  420. animRename.setText("Rename");
  421. animRename.setFont(font);
  422. cp.add(animRename);
  423. animRename.addActionListener(new ActionListener() {
  424. public void actionPerformed(ActionEvent evt) {
  425. int a = animList.getSelectedIndex();
  426. if (a < 0) {
  427. errorMessage("Select an animation!");
  428. return;
  429. }
  430. worker.getAnimation(a).setName(askString("Rename", "Rename " + animList.getSelectedValue() + "?"));
  431. animModel.set(a, worker.getAnimation(a).getName());
  432. animList.setModel(animModel);
  433. }
  434. });
  435. // Add button for adding animations
  436. animAdd.setBounds(211, 513, 110, 39);
  437. animAdd.setText("Add");
  438. animAdd.setFont(font);
  439. cp.add(animAdd);
  440. animAdd.addActionListener(new ActionListener() {
  441. public void actionPerformed(ActionEvent evt) {
  442. if (worker.addAnimation() == -1) {
  443. errorMessage("Could not add animation!");
  444. } else {
  445. animModel.clear();
  446. for (int i = 0; i < worker.size(); i++) {
  447. animModel.add(i, worker.getAnimation(i).getName());
  448. }
  449. animList.setModel(animModel);
  450. }
  451. }
  452. });
  453. // Add Animation remove button
  454. animRemove.setBounds(211, 558, 110, 39);
  455. animRemove.setText("Remove");
  456. animRemove.setFont(font);
  457. cp.add(animRemove);
  458. animRemove.addActionListener(new ActionListener() {
  459. public void actionPerformed(ActionEvent evt) {
  460. if (animList.getSelectedIndex() == -1) {
  461. errorMessage("Select an animation.");
  462. } else {
  463. worker.removeAnimation(animList.getSelectedIndex());
  464. animModel.removeElementAt(animList.getSelectedIndex());
  465. animList.setModel(animModel);
  466. }
  467. }
  468. });
  469. frameLengthLabel.setBounds(429, 118, 90, 13);
  470. frameLengthLabel.setText("Time (1/24 sec)");
  471. frameLengthLabel.setFont(font);
  472. cp.add(frameLengthLabel);
  473. frameLengthText.setBounds(429, 134, 31, 20);
  474. frameLengthText.setFont(font);
  475. cp.add(frameLengthText);
  476. fullScreenButton.setText("Fullscreen");
  477. fullScreenButton.setBounds(18, 312, 134, 35);
  478. fullScreenButton.setFont(font);
  479. cp.add(fullScreenButton);
  480. fullScreenButton.addActionListener(new ActionListener() {
  481. public void actionPerformed(ActionEvent evt) {
  482. ledView.enterFullscreen();
  483. setLocation(0,0);
  484. setSize(700, 700);
  485. int w = Toolkit.getDefaultToolkit().getScreenSize().width;
  486. int h = Toolkit.getDefaultToolkit().getScreenSize().height;
  487. setSize(w - 5, h - 30);
  488. playAnimationFullscreen.setVisible(true);
  489. cubeCanvas.setBounds(0, 0, w - 5, h - 80);
  490. }
  491. });
  492. exitButton = new JButton("Exit Fullscreen");
  493. exitButton.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-150, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  494. cp.add(exitButton);
  495. exitButton.addActionListener(new ActionListener() {
  496. public void actionPerformed(ActionEvent evt) {
  497. playAnimationFullscreen.setVisible(false);
  498. setLocation(0,0);
  499. setSize(672, 656);
  500. ledView.leaveFullscreen();
  501. cubeCanvas.setBounds(18, 31, 275, 275);
  502. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  503. int x = (d.width - getSize().width) / 2;
  504. int y = (d.height - getSize().height) / 2;
  505. setLocation(x, y);
  506. }
  507. });
  508. playAnimation.setText("Play");
  509. playAnimation.setBounds(159, 312, 134, 35);
  510. playAnimation.setFont(font);
  511. cp.add(playAnimation);
  512. playAnimation.addActionListener(new ActionListener() {
  513. public void actionPerformed(ActionEvent evt){
  514. if (animList.getSelectedIndex() == -1) {
  515. errorMessage("Please select an animation.");
  516. } else if (frameList.getSelectedIndex() == -1) {
  517. errorMessage("Please select a Frame.");
  518. } else {
  519. for (int i = 0; i < frameList.getModel().getSize(); i++){
  520. frameList.setSelectedIndex(i);
  521. short time1 = worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  522. long time = (long) (((time1+1) * 1/24) * 1000);
  523. try {
  524. Thread.sleep(time);
  525. } catch (Exception e) {
  526. System.out.println(e);
  527. }
  528. }
  529. }
  530. }
  531. });
  532. playAnimationFullscreen.setText("Play");
  533. playAnimationFullscreen.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-310, Toolkit.getDefaultToolkit().getScreenSize().height-80, 150, 25);
  534. playAnimationFullscreen.setFont(font);
  535. playAnimationFullscreen.setVisible(false);
  536. cp.add(playAnimationFullscreen);
  537. playAnimationFullscreen.addActionListener(new ActionListener() {
  538. public void actionPerformed(ActionEvent evt){
  539. if (animList.getSelectedIndex() == -1) {
  540. errorMessage("Please select an animation.");
  541. } else if (frameList.getSelectedIndex() == -1) {
  542. errorMessage("Please select a Frame.");
  543. } else {
  544. for (int i = 0; i < frameList.getModel().getSize(); i++){
  545. frameList.setSelectedIndex(i);
  546. short time1 = worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).getTime();
  547. long time = (long) (((time1+1) * 1/24) * 1000);
  548. try {
  549. Thread.sleep(time);
  550. } catch(Exception e) {
  551. System.out.println(e);
  552. }
  553. }
  554. }
  555. }
  556. });
  557. frameDuration.setBounds(462, 134, 55, 20);
  558. frameDuration.setText("OK");
  559. frameDuration.setFont(font);
  560. cp.add(frameDuration);
  561. frameDuration.addActionListener(new ActionListener() {
  562. public void actionPerformed(ActionEvent evt) {
  563. if (frameLengthText.getText().equals("0")) {
  564. errorMessage("0 is not a valid value!");
  565. frameLengthText.setText("1");
  566. } else if (Integer.parseInt(frameLengthText.getText()) > 256) {
  567. errorMessage("Value too high. Max: 256");
  568. frameLengthText.setText("256");
  569. return;
  570. } else {
  571. if (animList.getSelectedIndex() == -1) {
  572. errorMessage("Please select an animation!");
  573. return;
  574. }
  575. if (frameList.getSelectedIndex() == -1) {
  576. errorMessage("Please select a Frame!");
  577. return;
  578. }
  579. worker.getAnimation(animList.getSelectedIndex()).getFrame(frameList.getSelectedIndex()).setTime((byte)(Integer.parseInt(frameLengthText.getText()) - 1));
  580. }
  581. }
  582. });
  583. animPath.setBounds(417, 281, 228, 20);
  584. animPath.setEditable(false);
  585. animPath.setText("Load/Save an animation file...");
  586. animPath.setFont(font);
  587. cp.add(animPath);
  588. load.setBounds(417, 307, 72, 39);
  589. load.setText("Load");
  590. load.setFont(font);
  591. cp.add(load);
  592. load.addActionListener(new ActionListener() {
  593. public void actionPerformed(ActionEvent evt) {
  594. JFileChooser fc = new JFileChooser();
  595. int ret = fc.showOpenDialog(thisFrame);
  596. if (ret == JFileChooser.APPROVE_OPTION) {
  597. File file = fc.getSelectedFile();
  598. if (fileSelected == false) {
  599. fileSelected = true;
  600. }
  601. animPath.setText(file.getPath());
  602. worker.loadState(animPath.getText());
  603. animModel.clear();
  604. for (int i = 0; i < worker.size(); i++) {
  605. animModel.addElement(worker.getAnimation(i).getName());
  606. }
  607. animList.setModel(animModel);
  608. frameListModel.clear();
  609. frameList.setModel(frameListModel);
  610. }
  611. }
  612. });
  613. save.setBounds(491, 307, 72, 39);
  614. save.setText("Save");
  615. save.setFont(font);
  616. cp.add(save);
  617. save.addActionListener(new ActionListener() {
  618. public void actionPerformed(ActionEvent evt) {
  619. if (fileSelected == false) {
  620. JFileChooser fc = new JFileChooser();
  621. int ret = fc.showSaveDialog(thisFrame);
  622. if (ret == JFileChooser.APPROVE_OPTION) {
  623. File file = fc.getSelectedFile();
  624. fileSelected = true;
  625. animPath.setText(file.getPath());
  626. worker.saveState(animPath.getText());
  627. }
  628. } else {
  629. worker.saveState(animPath.getText());
  630. }
  631. }
  632. });
  633. saveAs.setBounds(565, 307, 82, 39);
  634. saveAs.setText("Save As");
  635. saveAs.setFont(font);
  636. cp.add(saveAs);
  637. saveAs.addActionListener(new ActionListener() {
  638. public void actionPerformed(ActionEvent evt) {
  639. JFileChooser fc;
  640. if (fileSelected == true) {
  641. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  642. } else {
  643. fc = new JFileChooser();
  644. }
  645. int ret = fc.showSaveDialog(thisFrame);
  646. if (ret == JFileChooser.APPROVE_OPTION) {
  647. File file = fc.getSelectedFile();
  648. if (fileSelected == false) {
  649. fileSelected = true;
  650. }
  651. animPath.setText(file.getPath());
  652. worker.saveState(animPath.getText());
  653. }
  654. }
  655. });
  656. serialPortSelector.setBounds(417, 186, 228, 21);
  657. serialPortSelector.setFont(font);
  658. cp.add(serialPortSelector);
  659. upload.setBounds(417, 212, 111, 39);
  660. upload.setText("Upload");
  661. upload.setFont(font);
  662. cp.add(upload);
  663. upload.addActionListener(new ActionListener() {
  664. public void actionPerformed(ActionEvent evt) {
  665. // Error messages are generated by worker
  666. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  667. worker.cubeSendState((String) serialPortSelector.getSelectedItem());
  668. }
  669. }
  670. });
  671. download.setBounds(534, 212, 111, 39);
  672. download.setText("Download");
  673. download.setFont(font);
  674. cp.add(download);
  675. download.addActionListener(new ActionListener() {
  676. public void actionPerformed(ActionEvent evt) {
  677. // Error messages are generated by worker
  678. if (worker.cubeProbeConnected((String) serialPortSelector.getSelectedItem())) {
  679. worker.cubeGetState((String) serialPortSelector.getSelectedItem());
  680. }
  681. }
  682. });
  683. remainingLabel.setBounds(530, 118, 90, 13);
  684. remainingLabel.setText("Remaining:");
  685. remainingLabel.setFont(font);
  686. cp.add(remainingLabel);
  687. frameRemaining.setBounds(530, 134, 49, 20);
  688. frameRemaining.setEditable(false);
  689. frameRemaining.setText(String.valueOf(worker.memoryRemaining()));
  690. frameRemaining.setFont(font);
  691. cp.add(frameRemaining);
  692. animList.setFont(font);
  693. frameList.setFont(font);
  694. previewPanel.setBounds(8, 12, 399, 342);
  695. previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
  696. cp.add(previewPanel);
  697. framePanel.setBounds(331, 360, 321, 246);
  698. framePanel.setBorder(BorderFactory.createTitledBorder("Frame"));
  699. cp.add(framePanel);
  700. animPanel.setBounds(8, 360, 321, 246);
  701. animPanel.setBorder(BorderFactory.createTitledBorder("Animation"));
  702. cp.add(animPanel);
  703. filePanel.setBounds(409, 262, 243, 92);
  704. filePanel.setBorder(BorderFactory.createTitledBorder("Load/Save"));
  705. cp.add(filePanel);
  706. serialPanel.setBounds(409, 167, 243, 92);
  707. serialPanel.setBorder(BorderFactory.createTitledBorder("Serial communication"));
  708. cp.add(serialPanel);
  709. settingsPanel.setBounds(409, 100, 243, 65);
  710. settingsPanel.setBorder(BorderFactory.createTitledBorder("Serial communication"));
  711. cp.add(settingsPanel);
  712. // Ende Komponenten
  713. animList.addListSelectionListener(this);
  714. frameList.addListSelectionListener(this);
  715. setResizable(false);
  716. setVisible(true);
  717. }
  718. public Led3D get3D() {
  719. return ledView;
  720. }
  721. public static void main(String[] args) {
  722. Frame f = new Frame("Cube Control");
  723. Led3D l = f.get3D();
  724. String lastCommand = null;
  725. java.util.Scanner sc = new java.util.Scanner(System.in);
  726. System.out.println("#### Cube Control Debug Console ####");
  727. System.out.println("## Enter a Command ('h' for help) ##");
  728. System.out.print("$> ");
  729. do {
  730. if (sc.hasNextLine()) {
  731. String s = sc.nextLine();
  732. // Up arrow key
  733. if (s.equals("\u001B[A")) {
  734. if (lastCommand != null) {
  735. System.out.println("Last command: " + lastCommand);
  736. s = new String(lastCommand);
  737. } else {
  738. System.out.println("No last command!");
  739. }
  740. }
  741. if (s.equals("p") || (s.equals("print")))
  742. l.printTranslationData();
  743. if (s.equals("q") || s.equals("quit"))
  744. System.exit(0);
  745. if (s.equals("r") || s.equals("reset"))
  746. l.resetView();
  747. if (s.equals("on") || s.equals("1")) {
  748. short[] d = new short[64];
  749. for (int i = 0; i < d.length; i++) {
  750. d[i] = 0xFF;
  751. }
  752. l.setData(d);
  753. System.out.println("All LEDs on now...");
  754. }
  755. if (s.equals("off") || s.equals("0")) {
  756. short[] d = new short[64];
  757. for (int i = 0; i < d.length; i++) {
  758. d[i] = 0x00;
  759. }
  760. l.setData(d);
  761. System.out.println("All LEDs off now...");
  762. }
  763. if (s.startsWith("port ")) {
  764. f.serialPortSelector.addItem(s.substring(5));
  765. f.serialPortSelector.setSelectedItem(s.substring(5));
  766. }
  767. if (s.startsWith("send ")) {
  768. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  769. short[] dat = toShortArray(s.substring(5));
  770. HelperUtility.writeData(dat, dat.length);
  771. HelperUtility.closePort();
  772. }
  773. if (s.startsWith("read ")) {
  774. int leng = Integer.parseInt(s.substring(5));
  775. HelperUtility.openPort((String) f.serialPortSelector.getSelectedItem());
  776. short[] data = HelperUtility.readData(leng);
  777. System.out.println(shortToString(data));
  778. HelperUtility.closePort();
  779. }
  780. if (s.startsWith("frames ")) {
  781. int anim = Integer.parseInt(s.substring(7));
  782. if (anim >= f.worker.size()) {
  783. System.out.println("Animation does not exist! Max: " + (f.worker.size() - 1));
  784. } else {
  785. System.out.println("Animation: " + f.worker.getAnimation(anim).getName());
  786. for (int i = 0; i < f.worker.getAnimation(anim).size(); i++) {
  787. AFrame frame = f.worker.getAnimation(anim).getFrame(i);
  788. System.out.println("\tFrame (" + frame.getTime() + "): " + frame.getName());
  789. }
  790. }
  791. }
  792. if (s.equals("a") || s.equals("anims")) {
  793. for (int i = 0; i < f.worker.size(); i++) {
  794. Animation anim = f.worker.getAnimation(i);
  795. System.out.println("\tAnimation: " + anim.getName());
  796. }
  797. }
  798. if (s.equals("s") || s.equals("scan")) {
  799. String[] sPorts = HelperUtility.getPorts();
  800. f.serialPortSelector.removeAllItems();
  801. for (int i = 0; i < sPorts.length; i++) {
  802. System.out.println("Port " + i + ": " + sPorts[i]);
  803. f.serialPortSelector.addItem(sPorts[i]);
  804. }
  805. }
  806. if (s.equals("h") || (s.equals("help"))) {
  807. System.out.println("Commands:");
  808. System.out.println("\t'port *name*'\t:\tSet serial port to this");
  809. System.out.println("\t'send *datas*'\t:\tSend data to serial port");
  810. System.out.println("\t'read *leng*'\t:\tRead data from serial port");
  811. System.out.println("\t'scan' / 's'\t:\tScan for serial ports");
  812. System.out.println("\t'on' / '1'\t:\tToggle all LEDs on");
  813. System.out.println("\t'off'/ '0'\t:\tToggle all LEDs off");
  814. System.out.println("\t'print'/ 'p'\t:\tPrint 3D Translation Matrix Data");
  815. System.out.println("\t'reset'/ 'r'\t:\tReset rotation of cube");
  816. System.out.println("\t'anims'/ 'a'\t:\tPrint animation tree");
  817. System.out.println("\t'frames *anim*\t:\tPrint frame tree");
  818. System.out.println("\t'help' / 'h'\t:\tShow this message");
  819. System.out.println("\t'quit' / 'q'\t:\tExit Cube Control");
  820. }
  821. lastCommand = new String(s);
  822. System.out.print("$> ");
  823. }
  824. } while (true);
  825. }
  826. private static short[] toShortArray(String s) {
  827. char[] d = s.toCharArray();
  828. System.out.println("Length: " + d.length);
  829. short[] r = new short[d.length];
  830. for (int i = 0; i < d.length; i++) {
  831. r[i] = (short) d[i];
  832. }
  833. return r;
  834. }
  835. private static String shortToString(short[] d) {
  836. String s = "";
  837. for (int i = 0; i < d.length; i++) {
  838. s += String.valueOf((char) d[i]);
  839. }
  840. return s;
  841. }
  842. private Point2d convertMousePositionToWorld (int iX, int iY)
  843. {
  844. Point3d eye_pos = new Point3d();
  845. Point3d mousePosn = new Point3d();
  846. //get the eye point and mouse click point
  847. this.cubeCanvas.getCenterEyeInImagePlate(eye_pos);
  848. this.cubeCanvas.getPixelLocationInImagePlate(iX, iY, mousePosn);
  849. //Transform from ImagePlate coordinates to Vworld coordinates
  850. Transform3D motion = new Transform3D();
  851. this.cubeCanvas.getImagePlateToVworld(motion);
  852. motion.transform(eye_pos);
  853. motion.transform(mousePosn);
  854. //calculate the intersection point on Z=0
  855. double dblX = (-eye_pos.z / (mousePosn.z - eye_pos.z)) * (mousePosn.x - eye_pos.x) + eye_pos.x;
  856. double dblY = (-eye_pos.z / (mousePosn.z - eye_pos.z)) * (mousePosn.y - eye_pos.y) + eye_pos.y;
  857. return new Point2d (dblX, dblY);
  858. }
  859. }