Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

layerEditFrame.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * layerEditFrame.java
  3. *
  4. *
  5. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  6. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  7. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  8. *
  9. * This file is part of LED-Cube.
  10. *
  11. * LED-Cube is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * LED-Cube is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import javax.swing.*;
  27. import javax.swing.event.*;
  28. /**
  29. * Shows a windows that allows the user to toggle the state of 64 LEDs.
  30. *
  31. * @author Max Nuding
  32. * @author Thomas Buck
  33. * @author Felix Bäder
  34. * @version 1.0
  35. */
  36. public class layerEditFrame extends JFrame {
  37. // Anfang Attribute
  38. private static final long serialVersionUID = 13374223L;
  39. JButton[][] ledPanels = new JButton[8][8];
  40. ImageIcon on = new ImageIcon(getClass().getResource("LEDon.png"));
  41. ImageIcon off = new ImageIcon(getClass().getResource("LEDoff.png"));
  42. byte[][] ledStatus = new byte[8][8];
  43. boolean changedStateSinceSave = false;
  44. short[] frame;
  45. int li;
  46. boolean finish = false;
  47. cubeWorker worker = null;
  48. int animI;
  49. int frameI;
  50. Frame LedFrame;
  51. // Ende Attribute
  52. /**
  53. * Create a new layer editor.
  54. *
  55. * @param animIndex Current animation
  56. * @param frameIndex Current frame
  57. * @param layerIndex Layer to edit
  58. * @param work the cubeWorker containing the data
  59. * @param LEDframe Used to call valueChanged, to trigger 3D View update
  60. */
  61. public layerEditFrame(int animIndex, int frameIndex, int layerIndex,
  62. cubeWorker work, Frame LEDframe) {
  63. // Frame-Initialisierung
  64. super("Layer Edit");
  65. worker = work;
  66. LedFrame = LEDframe;
  67. animI = animIndex;
  68. frameI = frameIndex;
  69. // frame = byteToShortArray(worker.getFrame(animIndex, frameIndex));
  70. frame = worker.getFrame(animIndex, frameIndex);
  71. li = layerIndex;
  72. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  73. int frameWidth = 180;
  74. int frameHeight = 230;
  75. setSize(frameWidth, frameHeight);
  76. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  77. int x = (d.width - getSize().width) / 2;
  78. int y = (d.height - getSize().height) / 2;
  79. setLocation(x, y);
  80. setResizable(false);
  81. Container cp = getContentPane();
  82. cp.setLayout(null);
  83. for (int i = 0; i < 8; i++) {
  84. for (int j = 0; j < 8; j++) {
  85. final int finalI = i;
  86. final int finalJ = j;
  87. ledPanels[i][j] = new JButton(on);
  88. ledPanels[i][j].setBounds((i * 20) + 5, (j * 20) + 5, 15, 15);
  89. ledPanels[i][j].addActionListener(new ActionListener() {
  90. public void actionPerformed(ActionEvent evt) {
  91. btnClicked(finalI, finalJ);
  92. }
  93. });
  94. ledPanels[i][j].setVisible(true);
  95. cp.add(ledPanels[i][j]);
  96. }
  97. }
  98. loadData();
  99. JButton saveBtn = new JButton("Save");
  100. JButton cancelBtn = new JButton("Cancel");
  101. saveBtn.setBounds(5, 170, 70, 25);
  102. saveBtn.setFont(new Font("Dialog", Font.PLAIN, 13));
  103. cp.add(saveBtn);
  104. cancelBtn.setBounds(80, 170, 80, 25);
  105. cancelBtn.setFont(new Font("Dialog", Font.PLAIN, 13));
  106. cp.add(cancelBtn);
  107. setVisible(true);
  108. saveBtn.addActionListener(new ActionListener() {
  109. public void actionPerformed(ActionEvent evt) {
  110. save();
  111. }
  112. });
  113. cancelBtn.addActionListener(new ActionListener() {
  114. public void actionPerformed(ActionEvent evt) {
  115. cancel();
  116. }
  117. });
  118. addWindowListener(new WindowAdapter() {
  119. public void windowClosing(WindowEvent evt) {
  120. if (changedStateSinceSave) {
  121. saveExitDialog();
  122. }
  123. dispose();
  124. }
  125. });
  126. }
  127. // Anfang Komponenten
  128. // Ende Komponenten
  129. private void loadData() {
  130. for (int i = 0; i < 8; i++) {
  131. int div = frame[(8 * (li + 1) + i) - 8];
  132. int[] rest = new int[8];
  133. int ctr = 0;
  134. while (div != 0) {
  135. rest[ctr] = div % 2;
  136. div = div / 2;
  137. ctr++;
  138. }
  139. for (int j = 0; j < 8; j++) {
  140. if (rest[j] == 0) {
  141. ledPanels[j][i].setIcon(off);
  142. } else {
  143. ledPanels[j][i].setIcon(on);
  144. }
  145. ledStatus[j][i] = (byte) rest[j];
  146. }
  147. }
  148. }
  149. /**
  150. * Get the edited data back. NOTE: The worker is updated automagically!
  151. *
  152. * @return Now changed 64 byte array.
  153. */
  154. public short[] getFinalFrame() {
  155. if (finish == false) {
  156. return null;
  157. }
  158. return frame;
  159. }
  160. /**
  161. * Gets called when the user clicks on a Toggle Button.
  162. *
  163. * @param i X-Coordinate of Button
  164. * @param j Y-Coordinate of Button
  165. */
  166. public void btnClicked(int i, int j) {
  167. changedStateSinceSave = true;
  168. if (ledPanels[i][j].getIcon() == on) {
  169. ledPanels[i][j].setIcon(off);
  170. ledStatus[i][j] = 0;
  171. } else {
  172. ledPanels[i][j].setIcon(on);
  173. ledStatus[i][j] = 1;
  174. }
  175. }
  176. /**
  177. * Action of Cancel Button. Removes this window...
  178. */
  179. public void cancel() {
  180. dispose();
  181. }
  182. /**
  183. * Gets called when clicking the save button. Puts data back into Worker and
  184. * fires ListSelectionEvent in Worker, so that the 3D View is updated.
  185. */
  186. public void save() {
  187. short[] tmpFrame = frame;
  188. changedStateSinceSave = false;
  189. short reihe = 0;
  190. for (int j = 0; j < 8; j++) {
  191. for (int i = 0; i < 8; i++) {
  192. reihe += ((short) Math.pow(2, i)) * ledStatus[i][j];
  193. }
  194. tmpFrame[(8 * (li + 1) + j) - 8] = reihe;
  195. reihe = 0;
  196. }
  197. frame = tmpFrame;
  198. worker.setFrame(frame, animI, frameI);
  199. ListSelectionEvent layerChanged = new ListSelectionEvent(
  200. LedFrame.frameList, LedFrame.frameList.getSelectedIndex(),
  201. LedFrame.frameList.getSelectedIndex(), false);
  202. LedFrame.valueChanged(layerChanged);
  203. dispose();
  204. }
  205. private int saveExitDialog() {
  206. String[] Optionen = { "Yes", "No" };
  207. int Auswahl = JOptionPane.showOptionDialog(this,
  208. "Do you want to save your changes?", "Save?",
  209. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
  210. Optionen, Optionen[0]);
  211. if (Auswahl == JOptionPane.YES_OPTION) {
  212. save();
  213. return 1;
  214. } else {
  215. return 0;
  216. }
  217. }
  218. }