Simple single-color 8x8x8 LED Cube with AVRs
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import java.io.File;
  6. import com.sun.j3d.utils.universe.*;
  7. import com.sun.j3d.utils.geometry.*;
  8. import javax.media.j3d.*;
  9. import javax.vecmath.*;
  10. /*
  11. * frame.java
  12. *
  13. *
  14. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  15. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  16. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  17. *
  18. * This file is part of LED-Cube.
  19. *
  20. * LED-Cube is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation, either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * LED-Cube is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  32. */
  33. public class frame extends JFrame {
  34. // Anfang Variablen
  35. private GraphicsConfiguration gConfig = SimpleUniverse.getPreferredConfiguration();
  36. private Canvas3D cubeCanvas = new Canvas3D(gConfig);
  37. SimpleUniverse universe;
  38. Transform3D transform3d;
  39. TransformGroup transroot;
  40. BranchGroup branchgroup;
  41. // Anfang Attribute
  42. private JButton editA = new JButton();
  43. private JButton editB = new JButton();
  44. private JButton editC = new JButton();
  45. private JButton editD = new JButton();
  46. private JButton editE = new JButton();
  47. private JButton editF = new JButton();
  48. private JButton editG = new JButton();
  49. private JButton editH = new JButton();
  50. private DefaultListModel frameListModel = new DefaultListModel();
  51. private 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 frame = 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 JTextField animPath = new JTextField();
  66. private JButton load = new JButton();
  67. private JButton save = new JButton();
  68. private JButton saveAs = new JButton();
  69. private 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. // Ende Attribute
  75. private cubeWorker worker = new cubeWorker();
  76. private boolean fileSelected = false;
  77. // Ende Variablen
  78. private int saveExitDialog() {
  79. String[] Optionen = {"Yes", "No"};
  80. int Auswahl = JOptionPane.showOptionDialog(this, "Do you want to save your changes?", "Save?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Optionen, Optionen[0]);
  81. if (Auswahl == JOptionPane.YES_OPTION) {
  82. worker.saveState(animPath.getText());
  83. return 1;
  84. } else {
  85. return 0;
  86. }
  87. }
  88. private void errorMessage(String s) {
  89. String[] Optionen = {"OK"};
  90. JOptionPane.showOptionDialog(this, s, "Error!", JOptionPane.YES_OPTION, JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
  91. }
  92. public frame(String title) {
  93. // Frame-Initialisierung
  94. super(title);
  95. String[] sPorts = worker.getSerialPorts();
  96. for(int i = 0; i < sPorts.length; i++){
  97. jComboBox1.addItem(sPorts[i]);
  98. }
  99. for(int i = 0; i < worker.numOfAnimations(); i++){
  100. animModel.addElement(worker.getAnimationName());
  101. }
  102. for(int i = 0; i < worker.numOfFrames(); i++){
  103. frameListModel.add(i, worker.getFrameName());
  104. }
  105. addWindowListener(new WindowAdapter() {
  106. public void windowClosing(WindowEvent evt) {
  107. if (worker.changedStateSinceSave()) {
  108. saveExitDialog();
  109. }
  110. System.exit(0);
  111. }
  112. });
  113. int frameWidth = 661;
  114. int frameHeight = 417;
  115. setSize(frameWidth, frameHeight);
  116. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  117. int x = (d.width - getSize().width) / 2;
  118. int y = (d.height - getSize().height) / 2 ;
  119. setLocation(x, y);
  120. Container cp = getContentPane();
  121. cp.setLayout(null);
  122. // Anfang Komponenten
  123. //----- 3D-----
  124. //-------------
  125. cubeCanvas.setBounds(8, 8, 250, 250);
  126. cp.add(cubeCanvas);
  127. ColorCube cube1 = new ColorCube(0.3);
  128. universe = new SimpleUniverse(cubeCanvas);
  129. universe.getViewingPlatform().setNominalViewingTransform();
  130. transform3d = new Transform3D();
  131. transroot = new TransformGroup(transform3d);
  132. transform3d.rotZ (Math.toRadians(30));
  133. transroot.addChild(cube1);
  134. transform3d.setTranslation(new Vector3d(2,2,2));
  135. branchgroup = new BranchGroup();
  136. branchgroup.addChild(transroot);
  137. universe.addBranchGraph(branchgroup);
  138. //-------------
  139. editA.setBounds(264, 8, 107, 25);
  140. editA.setText("Layer A");
  141. editA.setFont(new Font("Dialog", Font.PLAIN, 13));
  142. cp.add(editA);
  143. editA.addActionListener(new ActionListener() {
  144. public void actionPerformed(ActionEvent evt) {
  145. editA_ActionPerformed(evt);
  146. }
  147. });
  148. editB.setBounds(264, 40, 107, 25);
  149. editB.setText("Layer B");
  150. editB.setFont(new Font("Dialog", Font.PLAIN, 13));
  151. cp.add(editB);
  152. editB.addActionListener(new ActionListener() {
  153. public void actionPerformed(ActionEvent evt) {
  154. editB_ActionPerformed(evt);
  155. }
  156. });
  157. editC.setBounds(264, 72, 107, 25);
  158. editC.setText("Layer C");
  159. editC.setFont(new Font("Dialog", Font.PLAIN, 13));
  160. cp.add(editC);
  161. editC.addActionListener(new ActionListener() {
  162. public void actionPerformed(ActionEvent evt) {
  163. editC_ActionPerformed(evt);
  164. }
  165. });
  166. editD.setBounds(264, 104, 107, 25);
  167. editD.setText("Layer D");
  168. editD.setFont(new Font("Dialog", Font.PLAIN, 13));
  169. cp.add(editD);
  170. editD.addActionListener(new ActionListener() {
  171. public void actionPerformed(ActionEvent evt) {
  172. editD_ActionPerformed(evt);
  173. }
  174. });
  175. editE.setBounds(264, 136, 107, 25);
  176. editE.setText("Layer E");
  177. editE.setFont(new Font("Dialog", Font.PLAIN, 13));
  178. cp.add(editE);
  179. editE.addActionListener(new ActionListener() {
  180. public void actionPerformed(ActionEvent evt) {
  181. editE_ActionPerformed(evt);
  182. }
  183. });
  184. editF.setBounds(264, 168, 107, 25);
  185. editF.setText("Layer F");
  186. editF.setFont(new Font("Dialog", Font.PLAIN, 13));
  187. cp.add(editF);
  188. editF.addActionListener(new ActionListener() {
  189. public void actionPerformed(ActionEvent evt) {
  190. editF_ActionPerformed(evt);
  191. }
  192. });
  193. editG.setBounds(264, 200, 107, 25);
  194. editG.setText("Layer G");
  195. editG.setFont(new Font("Dialog", Font.PLAIN, 13));
  196. cp.add(editG);
  197. editG.addActionListener(new ActionListener() {
  198. public void actionPerformed(ActionEvent evt) {
  199. editG_ActionPerformed(evt);
  200. }
  201. });
  202. editH.setBounds(264, 232, 107, 25);
  203. editH.setText("Layer H");
  204. editH.setFont(new Font("Dialog", Font.PLAIN, 13));
  205. cp.add(editH);
  206. editH.addActionListener(new ActionListener() {
  207. public void actionPerformed(ActionEvent evt) {
  208. editH_ActionPerformed(evt);
  209. }
  210. });
  211. frameListScrollPane.setBounds(384, 8, 145, 249);
  212. frameList.setModel(frameListModel);
  213. //frameListModel.addElement();
  214. cp.add(frameListScrollPane);
  215. frameUp.setBounds(544, 8, 107, 33);
  216. frameUp.setText("Move up");
  217. frameUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  218. cp.add(frameUp);
  219. frameUp.addActionListener(new ActionListener() {
  220. public void actionPerformed(ActionEvent evt) {
  221. frameUp_ActionPerformed(evt);
  222. }
  223. });
  224. frameDown.setBounds(544, 152, 107, 33);
  225. frameDown.setText("Move down");
  226. frameDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  227. cp.add(frameDown);
  228. frameDown.addActionListener(new ActionListener() {
  229. public void actionPerformed(ActionEvent evt) {
  230. frameDown_ActionPerformed(evt);
  231. }
  232. });
  233. frameAdd.setBounds(544, 56, 107, 33);
  234. frameAdd.setText("Add");
  235. frameAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  236. cp.add(frameAdd);
  237. frameAdd.addActionListener(new ActionListener() {
  238. public void actionPerformed(ActionEvent evt) {
  239. frameAdd_ActionPerformed(evt);
  240. }
  241. });
  242. frameRemove.setBounds(544, 104, 107, 33);
  243. frameRemove.setText("Remove");
  244. frameRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  245. cp.add(frameRemove);
  246. frameRemove.addActionListener(new ActionListener() {
  247. public void actionPerformed(ActionEvent evt) {
  248. frameRemove_ActionPerformed(evt);
  249. }
  250. });
  251. animScrollPane.setBounds(8, 264, 209, 121);
  252. animList.setModel(animModel);
  253. //jList2Model.addElement("");
  254. cp.add(animScrollPane);
  255. animUp.setBounds(224, 264, 99, 25);
  256. animUp.setText("Move up");
  257. animUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  258. cp.add(animUp);
  259. animUp.addActionListener(new ActionListener() {
  260. public void actionPerformed(ActionEvent evt) {
  261. animUp_ActionPerformed(evt);
  262. }
  263. });
  264. animDown.setBounds(224, 360, 99, 25);
  265. animDown.setText("Move down");
  266. animDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  267. cp.add(animDown);
  268. animDown.addActionListener(new ActionListener() {
  269. public void actionPerformed(ActionEvent evt) {
  270. animDown_ActionPerformed(evt);
  271. }
  272. });
  273. animAdd.setBounds(224, 296, 99, 25);
  274. animAdd.setText("Add");
  275. animAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  276. cp.add(animAdd);
  277. animAdd.addActionListener(new ActionListener() {
  278. public void actionPerformed(ActionEvent evt) {
  279. animAdd_ActionPerformed(evt);
  280. }
  281. });
  282. animRemove.setBounds(224, 328, 99, 25);
  283. animRemove.setText("Remove");
  284. animRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  285. cp.add(animRemove);
  286. animRemove.addActionListener(new ActionListener() {
  287. public void actionPerformed(ActionEvent evt) {
  288. animRemove_ActionPerformed(evt);
  289. }
  290. });
  291. animPath.setBounds(344, 264, 305, 24);
  292. animPath.setEditable(false);
  293. animPath.setText("Load/Save an animation file...");
  294. animPath.setFont(new Font("Dialog", Font.PLAIN, 13));
  295. cp.add(animPath);
  296. load.setBounds(344, 296, 100, 25);
  297. load.setText("Load");
  298. load.setFont(new Font("Dialog", Font.PLAIN, 13));
  299. cp.add(load);
  300. load.addActionListener(new ActionListener() {
  301. public void actionPerformed(ActionEvent evt) {
  302. load_ActionPerformed(evt);
  303. }
  304. });
  305. save.setBounds(454, 296, 100, 25);
  306. save.setText("Save");
  307. save.setFont(new Font("Dialog", Font.PLAIN, 13));
  308. cp.add(save);
  309. save.addActionListener(new ActionListener() {
  310. public void actionPerformed(ActionEvent evt) {
  311. save_ActionPerformed(evt);
  312. }
  313. });
  314. saveAs.setBounds(564, 296, 90, 25);
  315. saveAs.setText("Save As");
  316. saveAs.setFont(new Font("Dialog", Font.PLAIN, 13));
  317. cp.add(saveAs);
  318. saveAs.addActionListener(new ActionListener() {
  319. public void actionPerformed(ActionEvent evt) {
  320. saveAs_ActionPerformed(evt);
  321. }
  322. });
  323. jComboBox1.setBounds(344, 328, 305, 24);
  324. jComboBox1.setFont(new Font("Dialog", Font.PLAIN, 13));
  325. cp.add(jComboBox1);
  326. upload.setBounds(344, 360, 147, 25);
  327. upload.setText("Upload");
  328. upload.setFont(new Font("Dialog", Font.PLAIN, 13));
  329. cp.add(upload);
  330. upload.addActionListener(new ActionListener() {
  331. public void actionPerformed(ActionEvent evt) {
  332. upload_ActionPerformed(evt);
  333. }
  334. });
  335. download.setBounds(504, 360, 147, 25);
  336. download.setText("Download");
  337. download.setFont(new Font("Dialog", Font.PLAIN, 13));
  338. cp.add(download);
  339. download.addActionListener(new ActionListener() {
  340. public void actionPerformed(ActionEvent evt) {
  341. download_ActionPerformed(evt);
  342. }
  343. });
  344. jLabel4.setBounds(536, 208, 112, 20);
  345. jLabel4.setText("Frames remaining:");
  346. jLabel4.setFont(new Font("Dialog", Font.PLAIN, 13));
  347. cp.add(jLabel4);
  348. frameRemaining.setBounds(536, 232, 113, 24);
  349. frameRemaining.setEditable(false);
  350. frameRemaining.setText("2048");
  351. frameRemaining.setFont(new Font("Dialog", Font.PLAIN, 13));
  352. cp.add(frameRemaining);
  353. animList.setFont(new Font("Dialog", Font.PLAIN, 13));
  354. frameList.setFont(new Font("Dialog", Font.PLAIN, 13));
  355. // Ende Komponenten
  356. animList.addListSelectionListener(new MyListSelectionListener(animList, worker));
  357. setResizable(false);
  358. setVisible(true);
  359. }
  360. // Anfang Methoden
  361. // Anfang Ereignisprozeduren
  362. public void editA_ActionPerformed(ActionEvent evt) {
  363. layerEditFrame layerFrame1 = new layerEditFrame(worker.getLayer(0));
  364. }
  365. public void editB_ActionPerformed(ActionEvent evt) {
  366. layerEditFrame layerFrame1 = new layerEditFrame(worker.getLayer(1));
  367. }
  368. public void editC_ActionPerformed(ActionEvent evt) {
  369. layerEditFrame layerFrame1 = new layerEditFrame(worker.getLayer(2));
  370. }
  371. public void editD_ActionPerformed(ActionEvent evt) {
  372. layerEditFrame layerFrame1 = new layerEditFrame(worker.getLayer(3));
  373. }
  374. public void editE_ActionPerformed(ActionEvent evt) {
  375. layerEditFrame layerFrame1 = new layerEditFrame(worker.getLayer(4));
  376. }
  377. public void editF_ActionPerformed(ActionEvent evt) {
  378. layerEditFrame layerFrame1 = new layerEditFrame(worker.getLayer(5));
  379. }
  380. public void editG_ActionPerformed(ActionEvent evt) {
  381. layerEditFrame layerFrame1 = new layerEditFrame(worker.getLayer(6));
  382. }
  383. public void editH_ActionPerformed(ActionEvent evt) {
  384. layerEditFrame layerFrame1 = new layerEditFrame(worker.getLayer(7));
  385. }
  386. public void frameUp_ActionPerformed(ActionEvent evt) {
  387. int i = frameList.getSelectedIndex();
  388. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  389. Object tmp = frameListModel.get(i);
  390. frameListModel.set(i, frameListModel.get(i - 1));
  391. frameListModel.set(i - 1, tmp);
  392. frameList.setSelectedIndex(i - 1);
  393. worker.moveFrame(worker.UP);
  394. }
  395. }
  396. public void frameDown_ActionPerformed(ActionEvent evt) {
  397. int i = frameList.getSelectedIndex();
  398. if ((i >= 0) && (frameListModel.getSize() >= 2) && (i < (frameListModel.getSize() - 1))) {
  399. Object tmp = frameListModel.get(i);
  400. frameListModel.set(i, frameListModel.get(i + 1));
  401. frameListModel.set(i + 1, tmp);
  402. frameList.setSelectedIndex(i + 1);
  403. worker.moveFrame(worker.DOWN);
  404. }
  405. }
  406. public void frameAdd_ActionPerformed(ActionEvent evt) {
  407. worker.addFrame();
  408. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  409. }
  410. public void frameRemove_ActionPerformed(ActionEvent evt) {
  411. worker.removeFrame();
  412. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  413. }
  414. public void animUp_ActionPerformed(ActionEvent evt) {
  415. int i = animList.getSelectedIndex();
  416. if ((i > 0) && (animModel.getSize() >= 2)) {
  417. Object tmp = animModel.get(i);
  418. animModel.set(i, animModel.get(i - 1));
  419. animModel.set(i - 1, tmp);
  420. animList.setSelectedIndex(i - 1);
  421. worker.moveAnimation(worker.UP);
  422. }
  423. }
  424. public void animDown_ActionPerformed(ActionEvent evt) {
  425. int i = animList.getSelectedIndex();
  426. if ((i >= 0) && (animModel.getSize() >= 2) && (i < (animModel.getSize() - 1))) {
  427. Object tmp = animModel.get(i);
  428. animModel.set(i, animModel.get(i + 1));
  429. animModel.set(i + 1, tmp);
  430. animList.setSelectedIndex(i + 1);
  431. worker.moveAnimation(worker.DOWN);
  432. }
  433. }
  434. public void animAdd_ActionPerformed(ActionEvent evt) {
  435. if(worker.addAnimation() == -1){
  436. //show error Dialog
  437. }
  438. }
  439. public void animRemove_ActionPerformed(ActionEvent evt) {
  440. worker.removeAnimation();
  441. }
  442. public void load_ActionPerformed(ActionEvent evt) {
  443. JFileChooser fc = new JFileChooser();
  444. int ret = fc.showOpenDialog(this);
  445. if (ret == JFileChooser.APPROVE_OPTION) {
  446. File file = fc.getSelectedFile();
  447. if (fileSelected == false) {
  448. fileSelected = true;
  449. }
  450. animPath.setText(file.getPath());
  451. worker.loadState(animPath.getText());
  452. }
  453. }
  454. public void save_ActionPerformed(ActionEvent evt) {
  455. if (fileSelected == false) {
  456. JFileChooser fc = new JFileChooser();
  457. int ret = fc.showSaveDialog(this);
  458. if (ret == JFileChooser.APPROVE_OPTION) {
  459. File file = fc.getSelectedFile();
  460. fileSelected = true;
  461. animPath.setText(file.getPath());
  462. worker.saveState(animPath.getText());
  463. }
  464. } else {
  465. worker.saveState(animPath.getText());
  466. }
  467. }
  468. public void saveAs_ActionPerformed(ActionEvent evt) {
  469. JFileChooser fc;
  470. if (fileSelected == true) {
  471. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  472. } else {
  473. fc = new JFileChooser();
  474. }
  475. int ret = fc.showSaveDialog(this);
  476. if (ret == JFileChooser.APPROVE_OPTION) {
  477. File file = fc.getSelectedFile();
  478. if (fileSelected == false) {
  479. fileSelected = true;
  480. }
  481. animPath.setText(file.getPath());
  482. worker.saveState(animPath.getText());
  483. }
  484. }
  485. public void upload_ActionPerformed(ActionEvent evt) {
  486. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  487. errorMessage("No serial port selected...");
  488. } else {
  489. worker.uploadState((String)jComboBox1.getSelectedItem());
  490. }
  491. }
  492. public void download_ActionPerformed(ActionEvent evt) {
  493. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  494. errorMessage("No serial port selected...");
  495. } else {
  496. worker.downloadState((String)jComboBox1.getSelectedItem());
  497. }
  498. }
  499. // Ende Ereignisprozeduren
  500. public static void main(String[] args) {
  501. new frame("Cube Control");
  502. }
  503. // Ende Methoden
  504. }
  505. class MyListSelectionListener implements ListSelectionListener {
  506. JList alist;
  507. cubeWorker worker;
  508. MyListSelectionListener(JList animList, cubeWorker w){
  509. alist = animList;
  510. worker = w;
  511. }
  512. public void valueChanged(ListSelectionEvent evt) {
  513. if (!evt.getValueIsAdjusting()) {
  514. worker.selectAnimation(alist.getSelectedIndex());
  515. }
  516. }
  517. }