Simple single-color 8x8x8 LED Cube with AVRs
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

layerEditFrame.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. /**
  6. *
  7. * Beschreibung
  8. *
  9. * @version 1.0 vom 11/16/2011
  10. * @author
  11. */
  12. public class layerEditFrame extends JFrame {
  13. // Anfang Attribute
  14. private JPanel panelLED1 = new JPanel(null, true);
  15. JButton[][] ledPanels = new JButton[8][8];
  16. ImageIcon on = new ImageIcon(getClass().getResource("LEDon.png"));
  17. ImageIcon off = new ImageIcon(getClass().getResource("LEDoff.png"));
  18. byte[][] ledStatus = new byte[8][8];
  19. boolean changedStateSinceSave = false;
  20. byte[] frame;
  21. int li;
  22. // Ende Attribute
  23. public layerEditFrame(byte[] f, int layerIndex) {
  24. // Frame-Initialisierung
  25. super("Layer Edit");
  26. frame = f;
  27. li = layerIndex;
  28. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  29. int frameWidth = 180;
  30. int frameHeight = 230;
  31. setSize(frameWidth, frameHeight);
  32. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  33. int x = (d.width - getSize().width) / 2;
  34. int y = (d.height - getSize().height) / 2;
  35. setLocation(x, y);
  36. setResizable(false);
  37. Container cp = getContentPane();
  38. cp.setLayout(null);
  39. for(int i = 0; i < 8; i++){
  40. for(int j = 0; j < 8; j++){
  41. final int finalI = i;
  42. final int finalJ = j;
  43. ledPanels[i][j] = new JButton(on);
  44. ledPanels[i][j].setBounds((i*20)+5, (j*20)+5, 15, 15);
  45. ledPanels[i][j].addActionListener(new ActionListener() {
  46. public void actionPerformed(ActionEvent evt) {
  47. btnClicked(finalI, finalJ);
  48. }
  49. });
  50. ledPanels[i][j].setVisible(true);
  51. cp.add(ledPanels[i][j]);
  52. }
  53. }
  54. loadData();
  55. JButton saveBtn = new JButton("Save");
  56. JButton cancelBtn = new JButton("Cancel");
  57. saveBtn.setBounds(5, 170, 70, 25);
  58. saveBtn.setFont(new Font("Dialog", Font.PLAIN, 13));
  59. cp.add(saveBtn);
  60. cancelBtn.setBounds(80, 170, 80, 25);
  61. cancelBtn.setFont(new Font("Dialog", Font.PLAIN, 13));
  62. cp.add(cancelBtn);
  63. setVisible(true);
  64. saveBtn.addActionListener(new ActionListener() {
  65. public void actionPerformed(ActionEvent evt) {
  66. save();
  67. }
  68. });
  69. cancelBtn.addActionListener(new ActionListener() {
  70. public void actionPerformed(ActionEvent evt) {
  71. cancel();
  72. }
  73. });
  74. addWindowListener(new WindowAdapter() {
  75. public void windowClosing(WindowEvent evt) {
  76. if(changedStateSinceSave){
  77. saveExitDialog();
  78. }
  79. dispose();
  80. }
  81. });
  82. }
  83. // Anfang Komponenten
  84. // Ende Komponenten
  85. private void loadData(){
  86. for(int i = 0; i < 8; i++){
  87. int div = frame[li + i];
  88. int[] rest = new int[8];
  89. int ctr = 0;
  90. while(div != 0){
  91. rest[ctr] = div%2;
  92. div = div/2;
  93. ctr++;
  94. }
  95. for(int j = 0; j < 8; j++){
  96. if(rest[j] == 0){
  97. ledPanels[li + j][i].setIcon(off);
  98. } else {
  99. ledPanels[li + j][i].setIcon(on);
  100. }
  101. ledStatus[li + j][i] = (byte) rest[j];
  102. }
  103. }
  104. }
  105. byte[] getFinalFrame(){
  106. return frame;
  107. }
  108. public void btnClicked(int i, int j){
  109. changedStateSinceSave = true;
  110. if (ledPanels[i][j].getIcon() == on){
  111. ledPanels[i][j].setIcon(off);
  112. ledStatus[i][j] = 0;
  113. } else {
  114. ledPanels[i][j].setIcon(on);
  115. ledStatus[i][j] = 1;
  116. }
  117. }
  118. public void cancel(){
  119. dispose();
  120. }
  121. public void save(){
  122. int ctr = 0;
  123. byte[] tmpFrame = new byte[64];
  124. changedStateSinceSave = false;
  125. int reihe = 0;
  126. for(int j = 0; j < 8; j++){
  127. for(int i = 0; i < 8; i++){
  128. reihe += ((int) Math.pow(2, i)) * ledStatus[i][j];
  129. System.out.println("LED-Status: " + ledStatus[i][j]);
  130. System.out.println("Reihe: " + i);
  131. System.out.println("Spalte: " + j);
  132. System.out.println("Wertigkeit: " + ((int) Math.pow(2, i)));
  133. System.out.println("Zusammen: " + ((int) Math.pow(2, j)) * ledStatus[i][j]);
  134. System.out.println("Reihe nacher: " + reihe);
  135. System.out.println();
  136. ctr++;
  137. }
  138. tmpFrame[j] = (byte)reihe;
  139. reihe = 0;
  140. System.out.println("----");
  141. System.out.println("Frame-Array, Position " + j + " = " + tmpFrame[j]);
  142. System.out.println("----");
  143. }
  144. frame = tmpFrame;
  145. }
  146. private int saveExitDialog() {
  147. String[] Optionen = {"Yes", "No"};
  148. int Auswahl = JOptionPane.showOptionDialog(this, "Do you want to save your changes?", "Save?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Optionen, Optionen[0]);
  149. if (Auswahl == JOptionPane.YES_OPTION) {
  150. save();
  151. return 1;
  152. } else {
  153. return 0;
  154. }
  155. }
  156. }