Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

layerEditFrame.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. * @author Max Nuding
  31. * @author Thomas Buck
  32. * @author Felix Bäder
  33. * @version 1.0
  34. */
  35. public class layerEditFrame extends JFrame {
  36. // Anfang Attribute
  37. private JPanel panelLED1 = new JPanel(null, true);
  38. JButton[][] ledPanels = new JButton[8][8];
  39. ImageIcon on = new ImageIcon(getClass().getResource("LEDon.png"));
  40. ImageIcon off = new ImageIcon(getClass().getResource("LEDoff.png"));
  41. byte[][] ledStatus = new byte[8][8];
  42. boolean changedStateSinceSave = false;
  43. short[] frame;
  44. int li;
  45. boolean finish = false;
  46. cubeWorker worker = null;
  47. int animI;
  48. int frameI;
  49. Frame LedFrame;
  50. // Ende Attribute
  51. /**
  52. * Create a new layer editor.
  53. * @param animIndex Current animation
  54. * @param frameIndex Current frame
  55. * @param layerIndex Layer to edit
  56. * @param work the cubeWorker containing the data
  57. * @param LEDframe Used to call valueChanged, to trigger 3D View update
  58. */
  59. public layerEditFrame(int animIndex, int frameIndex, int layerIndex, cubeWorker work, Frame LEDframe) {
  60. // Frame-Initialisierung
  61. super("Layer Edit");
  62. worker = work;
  63. LedFrame = LEDframe;
  64. animI = animIndex;
  65. frameI = frameIndex;
  66. //frame = byteToShortArray(worker.getFrame(animIndex, frameIndex));
  67. frame = worker.getFrame(animIndex, frameIndex);
  68. li = layerIndex;
  69. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  70. int frameWidth = 180;
  71. int frameHeight = 230;
  72. setSize(frameWidth, frameHeight);
  73. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  74. int x = (d.width - getSize().width) / 2;
  75. int y = (d.height - getSize().height) / 2;
  76. setLocation(x, y);
  77. setResizable(false);
  78. Container cp = getContentPane();
  79. cp.setLayout(null);
  80. for(int i = 0; i < 8; i++){
  81. for(int j = 0; j < 8; j++){
  82. final int finalI = i;
  83. final int finalJ = j;
  84. ledPanels[i][j] = new JButton(on);
  85. ledPanels[i][j].setBounds((i*20)+5, (j*20)+5, 15, 15);
  86. ledPanels[i][j].addActionListener(new ActionListener() {
  87. public void actionPerformed(ActionEvent evt) {
  88. btnClicked(finalI, finalJ);
  89. }
  90. });
  91. ledPanels[i][j].setVisible(true);
  92. cp.add(ledPanels[i][j]);
  93. }
  94. }
  95. loadData();
  96. JButton saveBtn = new JButton("Save");
  97. JButton cancelBtn = new JButton("Cancel");
  98. saveBtn.setBounds(5, 170, 70, 25);
  99. saveBtn.setFont(new Font("Dialog", Font.PLAIN, 13));
  100. cp.add(saveBtn);
  101. cancelBtn.setBounds(80, 170, 80, 25);
  102. cancelBtn.setFont(new Font("Dialog", Font.PLAIN, 13));
  103. cp.add(cancelBtn);
  104. setVisible(true);
  105. saveBtn.addActionListener(new ActionListener() {
  106. public void actionPerformed(ActionEvent evt) {
  107. save();
  108. }
  109. });
  110. cancelBtn.addActionListener(new ActionListener() {
  111. public void actionPerformed(ActionEvent evt) {
  112. cancel();
  113. }
  114. });
  115. addWindowListener(new WindowAdapter() {
  116. public void windowClosing(WindowEvent evt) {
  117. if(changedStateSinceSave){
  118. saveExitDialog();
  119. }
  120. dispose();
  121. }
  122. });
  123. }
  124. // Anfang Komponenten
  125. // Ende Komponenten
  126. private void loadData(){
  127. for(int i = 0; i < 8; i++){
  128. int div = frame[(8*(li+1)+i)-8];
  129. int[] rest = new int[8];
  130. int ctr = 0;
  131. while(div != 0){
  132. rest[ctr] = div%2;
  133. div = div/2;
  134. ctr++;
  135. }
  136. for(int j = 0; j < 8; j++){
  137. if(rest[j] == 0){
  138. ledPanels[j][i].setIcon(off);
  139. } else {
  140. ledPanels[j][i].setIcon(on);
  141. }
  142. ledStatus[j][i] = (byte) rest[j];
  143. }
  144. }
  145. }
  146. /**
  147. * Get the edited data back. NOTE: The worker is updated automagically!
  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. * @param i X-Coordinate of Button
  159. * @param j Y-Coordinate of Button
  160. */
  161. public void btnClicked(int i, int j){
  162. changedStateSinceSave = true;
  163. if (ledPanels[i][j].getIcon() == on){
  164. ledPanels[i][j].setIcon(off);
  165. ledStatus[i][j] = 0;
  166. } else {
  167. ledPanels[i][j].setIcon(on);
  168. ledStatus[i][j] = 1;
  169. }
  170. }
  171. /**
  172. * Action of Cancel Button. Removes this window...
  173. */
  174. public void cancel(){
  175. dispose();
  176. }
  177. /**
  178. * Gets called when clicking the save button. Puts data back into Worker and fires ListSelectionEvent in Worker, so that the 3D View is updated.
  179. */
  180. public void save(){
  181. int ctr = 0;
  182. short[] tmpFrame = frame;
  183. changedStateSinceSave = false;
  184. short reihe = 0;
  185. for(int j = 0; j < 8; j++){
  186. for(int i = 0; i < 8; i++){
  187. reihe += ((short) Math.pow(2, i)) * ledStatus[i][j];
  188. ctr++;
  189. }
  190. tmpFrame[(8*(li+1)+j)-8] = reihe;
  191. reihe = 0;
  192. }
  193. frame = tmpFrame;
  194. worker.setFrame(frame, animI, frameI);
  195. ListSelectionEvent layerChanged = new ListSelectionEvent(LedFrame.frameList, LedFrame.frameList.getSelectedIndex(), LedFrame.frameList.getSelectedIndex(), false);
  196. LedFrame.valueChanged(layerChanged);
  197. dispose();
  198. }
  199. private int saveExitDialog() {
  200. String[] Optionen = {"Yes", "No"};
  201. int Auswahl = JOptionPane.showOptionDialog(this, "Do you want to save your changes?", "Save?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Optionen, Optionen[0]);
  202. if (Auswahl == JOptionPane.YES_OPTION) {
  203. save();
  204. return 1;
  205. } else {
  206. return 0;
  207. }
  208. }
  209. }