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 15KB

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