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

Animation.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Animation.java
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Collections;
  24. /**
  25. * A collection of frames that represent an entire animation.
  26. *
  27. * @author Thomas Buck
  28. * @version 1.0
  29. */
  30. public class Animation {
  31. AFrame frames[] = new AFrame[1];
  32. private String name = "Animation";
  33. private static int lastIndex = 1;
  34. /**
  35. * Create an empty frame in this new animation.
  36. */
  37. public Animation() {
  38. frames[0] = new AFrame();
  39. name = "Animation " + lastIndex++;
  40. }
  41. /**
  42. * Gets the specified frame in this animation
  43. *
  44. * @param i Index of frame you want
  45. * @return The selected frame
  46. */
  47. public AFrame getFrame(int i) {
  48. if (i >= frames.length) {
  49. return null;
  50. } else {
  51. return frames[i];
  52. }
  53. }
  54. /**
  55. * Move an frame up.
  56. * @param i the animation you want to move
  57. */
  58. public void moveFrameUp(int i) {
  59. if (i > 0) {
  60. AFrame tmp = frames[i];
  61. frames[i] = frames[i - 1];
  62. frames[i - 1] = tmp;
  63. }
  64. }
  65. /**
  66. * Move an frame down.
  67. * @param i the animation you want to move
  68. */
  69. public void moveFrameDown(int i) {
  70. if (i < (frames.length - 1)) {
  71. AFrame tmp = frames[i];
  72. frames[i] = frames[i + 1];
  73. frames[i + 1] = tmp;
  74. }
  75. }
  76. /**
  77. * Sets the selected Frame
  78. *
  79. * @param f the frame you want to place in this animation
  80. * @param i Index of the frame you want to override
  81. */
  82. public void setFrame(AFrame f, int i) {
  83. if (i < frames.length) {
  84. frames[i] = f;
  85. } else {
  86. addFrame(f);
  87. }
  88. }
  89. /**
  90. * Add a new (empty) frame at the end
  91. */
  92. public void addFrame() {
  93. extendArray();
  94. frames[frames.length - 1] = new AFrame();
  95. }
  96. /**
  97. * Add a specified frame at the end
  98. *
  99. * @param f data for new frame
  100. */
  101. public void addFrame(AFrame f) {
  102. addFrame();
  103. frames[frames.length - 1] = f;
  104. }
  105. /**
  106. * Return size of this animation, in number of frames.
  107. *
  108. * @return number of frames
  109. */
  110. public int size() {
  111. return frames.length;
  112. }
  113. /**
  114. * Removes a frame. Subsequent frames shift to the left.
  115. *
  116. * @param i Index of frame you want to remove
  117. */
  118. public void removeFrame(int i) {
  119. shiftOver(i);
  120. shrinkArray();
  121. }
  122. /**
  123. * Gets the name of this animation
  124. *
  125. * @return name of this animation
  126. */
  127. public String getName() {
  128. return name;
  129. }
  130. /**
  131. * Sets the name of this animation
  132. *
  133. * @param s
  134. * new name
  135. */
  136. public void setName(String s) {
  137. name = s;
  138. }
  139. private void extendArray() {
  140. AFrame newArray[] = new AFrame[frames.length + 1];
  141. for (int i = 0; i < frames.length; i++) {
  142. newArray[i] = frames[i];
  143. }
  144. frames = newArray;
  145. }
  146. private void shrinkArray() {
  147. AFrame newArray[] = new AFrame[frames.length - 1];
  148. for (int i = 0; i < newArray.length; i++) {
  149. newArray[i] = frames[i];
  150. }
  151. frames = newArray;
  152. }
  153. private void shiftOver(int toForget) {
  154. for (int i = (toForget + 1); i < frames.length; i++) {
  155. frames[i - 1] = frames[i];
  156. }
  157. }
  158. }