Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Frame.java 31KB

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