PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/i-weboffice/project/secishow/sandbox/src/org/jdesktop/j3d/examples/texture_by_ref/AnimateTexturesBehavior.java

http://i-weboffice.googlecode.com/
Java | 317 lines | 184 code | 35 blank | 98 comment | 48 complexity | 9dd6cf8367af5dce1f36a86f338bbefc MD5 | raw file
  1. /*
  2. * $RCSfile: AnimateTexturesBehavior.java,v $
  3. *
  4. * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistribution of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * - Redistribution in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * Neither the name of Sun Microsystems, Inc. or the names of
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * This software is provided "AS IS," without a warranty of any
  23. * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26. * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
  27. * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
  28. * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
  29. * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
  30. * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
  31. * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
  32. * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
  33. * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGES.
  35. *
  36. * You acknowledge that this software is not designed, licensed or
  37. * intended for use in the design, construction, operation or
  38. * maintenance of any nuclear facility.
  39. *
  40. * $Revision: 1.2 $
  41. * $Date: 2007/02/09 17:21:54 $
  42. * $State: Exp $
  43. */
  44. package org.jdesktop.j3d.examples.texture_by_ref;
  45. import javax.media.j3d.*;
  46. import java.awt.image.BufferedImage;
  47. import java.awt.*;
  48. import com.sun.j3d.utils.image.TextureLoader;
  49. import java.util.Enumeration;
  50. public class AnimateTexturesBehavior extends Behavior {
  51. // what image are we on
  52. private int current;
  53. private int max;
  54. // the images
  55. private ImageComponent2D[] images;
  56. // the target
  57. private Texture2D texture;
  58. private Appearance appearance;
  59. // the wakeup criterion
  60. private WakeupCriterion wakeupC;
  61. // are the images flipped?
  62. private boolean flip;
  63. // need the current type because by copy changes all images
  64. // to TYPE_INT_ARGB
  65. private int currentType;
  66. // for custom types
  67. public static final int TYPE_CUSTOM_RGBA = 0x01;
  68. public static final int TYPE_CUSTOM_RGB = 0x02;
  69. private int customType;
  70. // create a new AnimateTextureBehavior
  71. // initialize the images
  72. public AnimateTexturesBehavior(Texture2D texP,
  73. java.net.URL[] fnames,
  74. Appearance appP,
  75. TextureByReference applet) {
  76. int size = fnames.length;
  77. images = new ImageComponent2D[size];
  78. BufferedImage bImage;
  79. TextureLoader loader;
  80. for (int i = 0; i < size; i++) {
  81. loader = new TextureLoader(fnames[i],
  82. TextureLoader.BY_REFERENCE |
  83. TextureLoader.Y_UP, applet);
  84. images[i] = loader.getImage();
  85. bImage = images[i].getImage();
  86. // convert the image to TYPE_4BYTE_ABGR
  87. currentType = BufferedImage.TYPE_4BYTE_ABGR;
  88. bImage = ImageOps.convertImage(bImage, currentType);
  89. // flip the image
  90. flip = true;
  91. ImageOps.flipImage(bImage);
  92. // set the image on the ImageComponent to the new one
  93. images[i].set(bImage);
  94. images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
  95. images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
  96. }
  97. texture = texP;
  98. current = 0;
  99. max = size;
  100. wakeupC = new WakeupOnElapsedFrames(20);
  101. appearance = appP;
  102. }
  103. // initialize to the first image
  104. public void initialize() {
  105. texture.setImage(0, images[current]);
  106. if (current < max-1) current++;
  107. else current = 0;
  108. wakeupOn(wakeupC);
  109. }
  110. // procesStimulus changes the ImageComponent of the texture
  111. public void processStimulus(Enumeration criteria) {
  112. // ImageOps.printType(images[current].getImage());
  113. texture.setImage(0, images[current]);
  114. appearance.setTexture(texture);
  115. if (current < max-1) current++;
  116. else current = 0;
  117. wakeupOn(wakeupC);
  118. }
  119. // flip the image -- useful depending on yUp
  120. public void setFlipImages(boolean b) {
  121. // double check that flipping is necessary
  122. if (b != flip) {
  123. BufferedImage bImage;
  124. // these are the same for all images so get info once
  125. int format = images[0].getFormat();
  126. boolean byRef = images[0].isByReference();
  127. boolean yUp = images[0].isYUp();
  128. // flip all the images
  129. // have to new ImageComponents because can't set the image at runtime
  130. for (int i = 0; i < images.length; i++) {
  131. bImage = images[i].getImage();
  132. ImageOps.flipImage(bImage);
  133. // if we are byRef and the bImage type does not match currentType
  134. // we need to convert it. If we are not byRef we will
  135. // save converting until it is changed to byRef
  136. if (byRef && bImage.getType() != currentType) {
  137. if (currentType != BufferedImage.TYPE_CUSTOM) {
  138. bImage = ImageOps.convertImage(bImage, currentType);
  139. }
  140. else if (customType == this.TYPE_CUSTOM_RGBA) {
  141. bImage = ImageOps.convertToCustomRGBA(bImage);
  142. }
  143. else {
  144. bImage = ImageOps.convertToCustomRGB(bImage);
  145. }
  146. }
  147. images[i] = new ImageComponent2D(format, bImage, byRef, yUp);
  148. images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
  149. images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
  150. }
  151. // set flip to new value
  152. flip = b;
  153. }
  154. }
  155. // create new ImageComponents with yUp set to the parameter. yUp on
  156. // an ImageComponent cannot be changed at runtim
  157. public void setYUp(boolean b) {
  158. // double check that changing yUp is necessary
  159. if (b != images[0].isYUp()) {
  160. // these are the same for all images so get info once
  161. int format = images[0].getFormat();
  162. boolean byRef = images[0].isByReference();
  163. // reset yUp on all the images -- have to new ImageComponents because
  164. // cannot change the value at runtime
  165. for (int i = 0; i < images.length; i++) {
  166. // if we are byRef and the bImage type does not match currentType
  167. // we need to convert it. If we are not byRef we will
  168. // save converting until it is changed to byRef
  169. BufferedImage bImage = images[i].getImage();
  170. if (byRef && bImage.getType() != currentType) {
  171. // bImage = ImageOps.convertImage(bImage, currentType);
  172. if (currentType != BufferedImage.TYPE_CUSTOM) {
  173. bImage = ImageOps.convertImage(bImage, currentType);
  174. }
  175. else if (customType == this.TYPE_CUSTOM_RGBA) {
  176. bImage = ImageOps.convertToCustomRGBA(bImage);
  177. }
  178. else {
  179. bImage = ImageOps.convertToCustomRGB(bImage);
  180. }
  181. }
  182. images[i] = new ImageComponent2D(format, bImage,
  183. byRef, b);
  184. images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
  185. images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
  186. }
  187. }
  188. }
  189. // create new ImageComponents with ByReference set by parameter.
  190. // by reference cannot be changed on an image component at runtime
  191. public void setByReference(boolean b) {
  192. // double check that changing is necessary
  193. if (b != images[0].isByReference()) {
  194. // these are the same for all images so get info once
  195. int format = images[0].getFormat();
  196. boolean yUp = images[0].isYUp();
  197. // reset yUp on all the images
  198. // have to new ImageComponents because cannot set value
  199. for (int i = 0; i < images.length; i++) {
  200. // if the bImage type does not match currentType and we are setting
  201. // to byRef we need to convert it
  202. BufferedImage bImage = images[i].getImage();
  203. if (bImage.getType() != currentType && b) {
  204. // bImage = ImageOps.convertImage(bImage, currentType);
  205. if (currentType != BufferedImage.TYPE_CUSTOM) {
  206. bImage = ImageOps.convertImage(bImage, currentType);
  207. }
  208. else if (customType == this.TYPE_CUSTOM_RGBA) {
  209. bImage = ImageOps.convertToCustomRGBA(bImage);
  210. }
  211. else {
  212. bImage = ImageOps.convertToCustomRGB(bImage);
  213. }
  214. }
  215. images[i] = new ImageComponent2D(format, bImage, b, yUp);
  216. images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
  217. images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
  218. }
  219. }
  220. }
  221. // make a new wakeup criterion object based on the new delay time
  222. public void setFrameDelay(int delay) {
  223. wakeupC = new WakeupOnElapsedFrames(delay);
  224. }
  225. //change the type of image
  226. public void setImageType(int newType) {
  227. currentType = newType;
  228. // only need to change the images if we are byRef otherwise will change
  229. // them when we chnage to byRef
  230. if (images[0].isByReference() == true) {
  231. // this information is the same for all
  232. int format = images[0].getFormat();
  233. boolean yUp = images[0].isYUp();
  234. boolean byRef = true;
  235. for (int i = 0; i < images.length; i++) {
  236. BufferedImage bImage = images[i].getImage();
  237. bImage = ImageOps.convertImage(bImage, currentType);
  238. images[i] = new ImageComponent2D(format, bImage, byRef, yUp);
  239. images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
  240. images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
  241. }
  242. }
  243. }
  244. public void setImageTypeCustomRGBA() {
  245. currentType = BufferedImage.TYPE_CUSTOM;
  246. customType = this.TYPE_CUSTOM_RGBA;
  247. // only need to change images if we are byRef otherwise will change
  248. // them when we change to byRef
  249. if (images[0].isByReference()) {
  250. // this information is the same for all
  251. int format = images[0].getFormat();
  252. boolean yUp = images[0].isYUp();
  253. boolean byRef = true;
  254. for (int i = 0; i < images.length; i++) {
  255. BufferedImage bImage = images[i].getImage();
  256. bImage = ImageOps.convertToCustomRGBA(bImage);
  257. images[i] = new ImageComponent2D(format, bImage, byRef, yUp);
  258. images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
  259. images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
  260. }
  261. }
  262. }
  263. public void setImageTypeCustomRGB() {
  264. currentType = BufferedImage.TYPE_CUSTOM;
  265. customType = this.TYPE_CUSTOM_RGB;
  266. // only need to change images if we are byRef otherwise will change
  267. // them when we change to byRef
  268. if (images[0].isByReference()) {
  269. // this information is the same for all
  270. int format = images[0].getFormat();
  271. boolean yUp = images[0].isYUp();
  272. boolean byRef = true;
  273. for (int i = 0; i < images.length; i++) {
  274. BufferedImage bImage = images[i].getImage();
  275. bImage = ImageOps.convertToCustomRGB(bImage);
  276. images[i] = new ImageComponent2D(format, bImage, byRef, yUp);
  277. images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
  278. images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
  279. }
  280. }
  281. }
  282. }