Simple single-color 8x8x8 LED Cube with AVRs
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

frame.java 18KB

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