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.0KB

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