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

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