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

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