Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

layerEditFrame.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. public class layerEditFrame extends JFrame {
  29. // Anfang Attribute
  30. private JPanel panelLED1 = new JPanel(null, true);
  31. JButton[][] ledPanels = new JButton[8][8];
  32. ImageIcon on = new ImageIcon(getClass().getResource("LEDon.png"));
  33. ImageIcon off = new ImageIcon(getClass().getResource("LEDoff.png"));
  34. byte[][] ledStatus = new byte[8][8];
  35. boolean changedStateSinceSave = false;
  36. short[] frame;
  37. int li;
  38. boolean finish = false;
  39. cubeWorker worker = null;
  40. int animI;
  41. int frameI;
  42. // Ende Attribute
  43. public layerEditFrame(int animIndex, int frameIndex, int layerIndex, cubeWorker work) {
  44. // Frame-Initialisierung
  45. super("Layer Edit");
  46. worker = work;
  47. animI = animIndex;
  48. frameI = frameIndex;
  49. frame = byteToShortArray(worker.getFrame(animIndex, frameIndex));
  50. li = layerIndex;
  51. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  52. int frameWidth = 180;
  53. int frameHeight = 230;
  54. setSize(frameWidth, frameHeight);
  55. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  56. int x = (d.width - getSize().width) / 2;
  57. int y = (d.height - getSize().height) / 2;
  58. setLocation(x, y);
  59. setResizable(false);
  60. Container cp = getContentPane();
  61. cp.setLayout(null);
  62. for(int i = 0; i < 8; i++){
  63. for(int j = 0; j < 8; j++){
  64. final int finalI = i;
  65. final int finalJ = j;
  66. ledPanels[i][j] = new JButton(on);
  67. ledPanels[i][j].setBounds((i*20)+5, (j*20)+5, 15, 15);
  68. ledPanels[i][j].addActionListener(new ActionListener() {
  69. public void actionPerformed(ActionEvent evt) {
  70. btnClicked(finalI, finalJ);
  71. }
  72. });
  73. ledPanels[i][j].setVisible(true);
  74. cp.add(ledPanels[i][j]);
  75. }
  76. }
  77. loadData();
  78. JButton saveBtn = new JButton("Save");
  79. JButton cancelBtn = new JButton("Cancel");
  80. saveBtn.setBounds(5, 170, 70, 25);
  81. saveBtn.setFont(new Font("Dialog", Font.PLAIN, 13));
  82. cp.add(saveBtn);
  83. cancelBtn.setBounds(80, 170, 80, 25);
  84. cancelBtn.setFont(new Font("Dialog", Font.PLAIN, 13));
  85. cp.add(cancelBtn);
  86. setVisible(true);
  87. saveBtn.addActionListener(new ActionListener() {
  88. public void actionPerformed(ActionEvent evt) {
  89. save();
  90. }
  91. });
  92. cancelBtn.addActionListener(new ActionListener() {
  93. public void actionPerformed(ActionEvent evt) {
  94. cancel();
  95. }
  96. });
  97. addWindowListener(new WindowAdapter() {
  98. public void windowClosing(WindowEvent evt) {
  99. if(changedStateSinceSave){
  100. saveExitDialog();
  101. }
  102. dispose();
  103. }
  104. });
  105. }
  106. // Anfang Komponenten
  107. // Ende Komponenten
  108. private void loadData(){
  109. for(int i = 0; i < 8; i++){
  110. int div = frame[(8*(li+1)+i)-8];
  111. int[] rest = new int[8];
  112. int ctr = 0;
  113. while(div != 0){
  114. rest[ctr] = div%2;
  115. div = div/2;
  116. ctr++;
  117. }
  118. for(int j = 0; j < 8; j++){
  119. if(rest[j] == 0){
  120. ledPanels[j][i].setIcon(off);
  121. } else {
  122. ledPanels[j][i].setIcon(on);
  123. }
  124. ledStatus[j][i] = (byte) rest[j];
  125. }
  126. }
  127. }
  128. byte[] getFinalFrame(){
  129. if (finish == false) {
  130. return null;
  131. }
  132. return shortToByteArray(frame);
  133. }
  134. public void btnClicked(int i, int j){
  135. changedStateSinceSave = true;
  136. if (ledPanels[i][j].getIcon() == on){
  137. ledPanels[i][j].setIcon(off);
  138. ledStatus[i][j] = 0;
  139. } else {
  140. ledPanels[i][j].setIcon(on);
  141. ledStatus[i][j] = 1;
  142. }
  143. }
  144. public void cancel(){
  145. dispose();
  146. }
  147. public void save(){
  148. int ctr = 0;
  149. short[] tmpFrame = frame;
  150. changedStateSinceSave = false;
  151. short reihe = 0;
  152. for(int j = 0; j < 8; j++){
  153. for(int i = 0; i < 8; i++){
  154. reihe += ((short) Math.pow(2, i)) * ledStatus[i][j];
  155. ctr++;
  156. }
  157. tmpFrame[(8*(li+1)+j)-8] = reihe;
  158. reihe = 0;
  159. }
  160. frame = tmpFrame;
  161. worker.setFrame(shortToByteArray(frame), animI, frameI);
  162. dispose();
  163. }
  164. public byte[] shortToByteArray(short[] shrt){
  165. byte[] tmpByte = new byte[shrt.length];
  166. short min = 128;
  167. for(int i = 0; i < tmpByte.length; i++){
  168. tmpByte[i] = (byte) (shrt[i] - min);
  169. }
  170. return tmpByte;
  171. }
  172. public short[] byteToShortArray(byte[] tmpByte){
  173. short[] tmpShrt = new short[tmpByte.length];
  174. for(int i = 0; i < tmpByte.length; i++){
  175. tmpShrt[i] = (short) (tmpByte[i] + 128);
  176. }
  177. return tmpShrt;
  178. }
  179. private int saveExitDialog() {
  180. String[] Optionen = {"Yes", "No"};
  181. int Auswahl = JOptionPane.showOptionDialog(this, "Do you want to save your changes?", "Save?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Optionen, Optionen[0]);
  182. if (Auswahl == JOptionPane.YES_OPTION) {
  183. save();
  184. return 1;
  185. } else {
  186. return 0;
  187. }
  188. }
  189. }