Simple single-color 8x8x8 LED Cube with AVRs
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

frame.java 16KB

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