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

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