Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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