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 5.8KB

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