PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://i-weboffice.googlecode.com/
Java | 547 lines | 369 code | 69 blank | 109 comment | 59 complexity | 14e218e2a55aa4f9ec215a714327cf51 MD5 | raw file
  1. /*
  2. * $RCSfile: TextureByReference.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.3 $
  41. * $Date: 2007/02/09 17:21:55 $
  42. * $State: Exp $
  43. */
  44. package org.jdesktop.j3d.examples.texture_by_ref;
  45. import java.applet.Applet;
  46. import java.awt.*;
  47. import java.awt.event.*;
  48. import com.sun.j3d.utils.applet.MainFrame;
  49. import com.sun.j3d.utils.universe.*;
  50. import javax.media.j3d.*;
  51. import javax.vecmath.*;
  52. import java.awt.image.*;
  53. import com.sun.j3d.utils.image.TextureLoader;
  54. import javax.swing.*;
  55. import javax.swing.event.*;
  56. import org.jdesktop.j3d.examples.Resources;
  57. public class TextureByReference extends Applet
  58. implements ItemListener, ActionListener, ChangeListener {
  59. // need reference to animation behavior
  60. private AnimateTexturesBehavior animate;
  61. // need reference to tetrahedron
  62. private Tetrahedron tetra;
  63. // the gui buttons
  64. private JCheckBox flipB;
  65. private JRadioButton texByRef;
  66. private JRadioButton texByCopy;
  67. private JRadioButton geomByRef;
  68. private JRadioButton geomByCopy;
  69. private JRadioButton img4ByteABGR;
  70. private JRadioButton img3ByteBGR;
  71. private JRadioButton imgIntARGB;
  72. private JRadioButton imgCustomRGBA;
  73. private JRadioButton imgCustomRGB;
  74. private JRadioButton yUp;
  75. private JRadioButton yDown;
  76. private JButton animationB;
  77. private JSlider frameDelay;
  78. private SimpleUniverse universe = null;
  79. // image files used for the Texture animation for the applet,
  80. // or if no parameters are passed in for the application
  81. public static final String[] defaultFiles = {
  82. "resources/images/animation1.gif",
  83. "resources/images/animation2.gif",
  84. "resources/images/animation3.gif",
  85. "resources/images/animation4.gif",
  86. "resources/images/animation5.gif",
  87. "resources/images/animation6.gif",
  88. "resources/images/animation7.gif",
  89. "resources/images/animation8.gif",
  90. "resources/images/animation9.gif",
  91. "resources/images/animation10.gif"};
  92. private java.net.URL[] urls = null;
  93. public TextureByReference() {
  94. }
  95. public TextureByReference(java.net.URL[] fnamesP) {
  96. urls = fnamesP;
  97. }
  98. public void init() {
  99. if (urls == null) {
  100. urls = new java.net.URL[defaultFiles.length];
  101. for (int i = 0; i < defaultFiles.length; i++) {
  102. urls[i] = Resources.getResource(defaultFiles[i]);
  103. if (urls[i] == null) {
  104. System.err.println(defaultFiles[i] + " not found");
  105. System.exit(1);
  106. }
  107. /*
  108. try {
  109. urls[i] = new java.net.URL(getCodeBase().toString() +
  110. defaultFiles[i]);
  111. }
  112. catch (java.net.MalformedURLException ex) {
  113. System.out.println(ex.getMessage());
  114. System.exit(1);
  115. }
  116. */
  117. }
  118. }
  119. setLayout(new BorderLayout());
  120. GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
  121. Canvas3D canvas = new Canvas3D(config);
  122. add("Center", canvas);
  123. // create a simple scene graph and attach it to a simple universe
  124. BranchGroup scene = createSceneGraph();
  125. universe = new SimpleUniverse(canvas);
  126. universe.getViewingPlatform().setNominalViewingTransform();
  127. universe.addBranchGraph(scene);
  128. // create the gui
  129. JPanel gui = buildGui();
  130. this.add("South", gui);
  131. }
  132. public void destroy() {
  133. universe.cleanup();
  134. }
  135. public JPanel buildGui() {
  136. flipB = new JCheckBox("flip image", true);
  137. flipB.addItemListener(this);
  138. javax.swing.Box flipBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  139. flipBox.add(flipB);
  140. Component strut1 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  141. Component strut2 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  142. Component strut3 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  143. Component strut4 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  144. Component strut5 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  145. flipBox.add(strut1);
  146. flipBox.add(strut2);
  147. flipBox.add(strut3);
  148. flipBox.add(strut4);
  149. flipBox.add(strut5);
  150. yUp = new JRadioButton("y up");
  151. yUp.addActionListener(this);
  152. yUp.setSelected(true);
  153. yDown = new JRadioButton("y down");
  154. yDown.addActionListener(this);
  155. ButtonGroup yGroup = new ButtonGroup();
  156. yGroup.add(yUp);
  157. yGroup.add(yDown);
  158. JLabel yLabel = new JLabel("Image Orientation:");
  159. javax.swing.Box yBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  160. yBox.add(yLabel);
  161. yBox.add(yUp);
  162. yBox.add(yDown);
  163. strut1 = yBox.createVerticalStrut(yUp.getPreferredSize().height);
  164. strut2 = yBox.createVerticalStrut(yUp.getPreferredSize().height);
  165. strut3 = yBox.createVerticalStrut(yUp.getPreferredSize().height);
  166. yBox.add(strut1);
  167. yBox.add(strut2);
  168. yBox.add(strut3);
  169. texByRef = new JRadioButton("by reference");
  170. texByRef.addActionListener(this);
  171. texByRef.setSelected(true);
  172. texByCopy = new JRadioButton("by copy");
  173. texByCopy.addActionListener(this);
  174. ButtonGroup texGroup = new ButtonGroup();
  175. texGroup.add(texByRef);
  176. texGroup.add(texByCopy);
  177. JLabel texLabel = new JLabel("Texture:*");
  178. javax.swing.Box texBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  179. texBox.add(texLabel);
  180. texBox.add(texByRef);
  181. texBox.add(texByCopy);
  182. strut1 = texBox.createVerticalStrut(texByRef.getPreferredSize().height);
  183. strut2 = texBox.createVerticalStrut(texByRef.getPreferredSize().height);
  184. strut3 = texBox.createVerticalStrut(texByRef.getPreferredSize().height);
  185. texBox.add(strut1);
  186. texBox.add(strut2);
  187. texBox.add(strut3);
  188. geomByRef = new JRadioButton("by reference");
  189. geomByRef.addActionListener(this);
  190. geomByRef.setSelected(true);
  191. geomByCopy = new JRadioButton("by copy");
  192. geomByCopy.addActionListener(this);
  193. ButtonGroup geomGroup = new ButtonGroup();
  194. geomGroup.add(geomByRef);
  195. geomGroup.add(geomByCopy);
  196. JLabel geomLabel = new JLabel("Geometry:");
  197. javax.swing.Box geomBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  198. geomBox.add(geomLabel);
  199. geomBox.add(geomByRef);
  200. geomBox.add(geomByCopy);
  201. strut1 = geomBox.createVerticalStrut(geomByRef.getPreferredSize().height);
  202. strut2 = geomBox.createVerticalStrut(geomByRef.getPreferredSize().height);
  203. strut3 = geomBox.createVerticalStrut(geomByRef.getPreferredSize().height);
  204. geomBox.add(strut1);
  205. geomBox.add(strut2);
  206. geomBox.add(strut3);
  207. img4ByteABGR = new JRadioButton("TYPE_4BYTE_ABGR");
  208. img4ByteABGR.addActionListener(this);
  209. img4ByteABGR.setSelected(true);
  210. img3ByteBGR = new JRadioButton("TYPE_3BYTE_BGR");
  211. img3ByteBGR.addActionListener(this);
  212. imgIntARGB = new JRadioButton("TYPE_INT_ARGB");
  213. imgIntARGB.addActionListener(this);
  214. imgCustomRGBA = new JRadioButton("TYPE_CUSTOM RGBA");
  215. imgCustomRGBA.addActionListener(this);
  216. imgCustomRGB = new JRadioButton("TYPE_CUSTOM RGB");
  217. imgCustomRGB.addActionListener(this);
  218. ButtonGroup imgGroup = new ButtonGroup();
  219. imgGroup.add(img4ByteABGR);
  220. imgGroup.add(img3ByteBGR);
  221. imgGroup.add(imgIntARGB);
  222. imgGroup.add(imgCustomRGBA);
  223. imgGroup.add(imgCustomRGB);
  224. JLabel imgLabel = new JLabel("Image Type:*");
  225. javax.swing.Box imgBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  226. imgBox.add(imgLabel);
  227. imgBox.add(img4ByteABGR);
  228. imgBox.add(img3ByteBGR);
  229. imgBox.add(imgIntARGB);
  230. imgBox.add(imgCustomRGBA);
  231. imgBox.add(imgCustomRGB);
  232. javax.swing.Box topBox = new javax.swing.Box(BoxLayout.X_AXIS);
  233. topBox.add(flipBox);
  234. topBox.add(texBox);
  235. topBox.add(geomBox);
  236. topBox.add(yBox);
  237. Component strut = topBox.createRigidArea(new Dimension(10, 10));
  238. topBox.add(strut);
  239. topBox.add(imgBox);
  240. frameDelay = new JSlider(0, 50, 0);
  241. frameDelay.addChangeListener(this);
  242. frameDelay.setSnapToTicks(true);
  243. frameDelay.setPaintTicks(true);
  244. frameDelay.setPaintLabels(true);
  245. frameDelay.setMajorTickSpacing(10);
  246. frameDelay.setMinorTickSpacing(1);
  247. frameDelay.setValue(20);
  248. JLabel delayL = new JLabel("frame delay");
  249. javax.swing.Box delayBox = new javax.swing.Box(BoxLayout.X_AXIS);
  250. delayBox.add(delayL);
  251. delayBox.add(frameDelay);
  252. animationB = new JButton(" stop animation ");
  253. animationB.addActionListener(this);
  254. JLabel texInfo1 = new JLabel("*To use ImageComponent by reference feature, use TYPE_4BYTE_ABGR on Solaris");
  255. JLabel texInfo2 = new JLabel("and TYPE_3BYTE_BGR on Windows");
  256. JPanel buttonP = new JPanel();
  257. GridBagLayout gridbag = new GridBagLayout();
  258. GridBagConstraints c = new GridBagConstraints();
  259. buttonP.setLayout(gridbag);
  260. c.anchor = GridBagConstraints.CENTER;
  261. c.gridwidth = GridBagConstraints.REMAINDER;
  262. gridbag.setConstraints(topBox, c);
  263. buttonP.add(topBox);
  264. gridbag.setConstraints(delayBox, c);
  265. buttonP.add(delayBox);
  266. gridbag.setConstraints(animationB, c);
  267. buttonP.add(animationB);
  268. gridbag.setConstraints(texInfo1, c);
  269. buttonP.add(texInfo1);
  270. gridbag.setConstraints(texInfo2, c);
  271. buttonP.add(texInfo2);
  272. return buttonP;
  273. }
  274. public BranchGroup createSceneGraph() {
  275. // create the root of the branch group
  276. BranchGroup objRoot = new BranchGroup();
  277. // create the transform group node and initialize it
  278. // enable the TRANSFORM_WRITE capability so that it can be modified
  279. // at runtime. Add it to the root of the subgraph
  280. Transform3D rotate = new Transform3D();
  281. TransformGroup objTrans = new TransformGroup(rotate);
  282. objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  283. objRoot.addChild(objTrans);
  284. // bounds
  285. BoundingSphere bounds =
  286. new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  287. // set up some light
  288. Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
  289. Vector3f lDir1 = new Vector3f(-1.0f, -0.5f, -1.0f);
  290. Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
  291. AmbientLight aLgt = new AmbientLight(alColor);
  292. aLgt.setInfluencingBounds(bounds);
  293. DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
  294. lgt1.setInfluencingBounds(bounds);
  295. objRoot.addChild(aLgt);
  296. objRoot.addChild(lgt1);
  297. Appearance appearance = new Appearance();
  298. // enable the TEXTURE_WRITE so we can modify it at runtime
  299. appearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
  300. // load the first texture
  301. TextureLoader loader = new TextureLoader(urls[0],
  302. TextureLoader.BY_REFERENCE |
  303. TextureLoader.Y_UP,
  304. this);
  305. // get the texture from the loader
  306. Texture2D tex = (Texture2D)loader.getTexture();
  307. // get the BufferedImage to convert to TYPE_4BYTE_ABGR and flip
  308. // get the ImageComponent because we need it anyway
  309. ImageComponent2D imageComp = (ImageComponent2D)tex.getImage(0);
  310. BufferedImage bImage = imageComp.getImage();
  311. // convert the image
  312. bImage = ImageOps.convertImage(bImage, BufferedImage.TYPE_4BYTE_ABGR);
  313. // flip the image
  314. ImageOps.flipImage(bImage);
  315. imageComp.set(bImage);
  316. tex.setCapability(Texture.ALLOW_IMAGE_WRITE);
  317. tex.setBoundaryModeS(Texture.CLAMP);
  318. tex.setBoundaryModeT(Texture.CLAMP);
  319. tex.setBoundaryColor(1.0f, 1.0f, 1.0f, 1.0f);
  320. // set the image of the texture
  321. tex.setImage(0, imageComp);
  322. // set the texture on the appearance
  323. appearance.setTexture(tex);
  324. // set texture attributes
  325. TextureAttributes texAttr = new TextureAttributes();
  326. texAttr.setTextureMode(TextureAttributes.MODULATE);
  327. appearance.setTextureAttributes(texAttr);
  328. // set material properties
  329. Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
  330. Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
  331. appearance.setMaterial(new Material(white, black, white, black, 1.0f));
  332. // create a scale transform
  333. Transform3D scale = new Transform3D();
  334. scale.set(.6);
  335. TransformGroup objScale = new TransformGroup(scale);
  336. objTrans.addChild(objScale);
  337. tetra = new Tetrahedron(true);
  338. tetra.setAppearance(appearance);
  339. objScale.addChild(tetra);
  340. // create the behavior
  341. animate = new AnimateTexturesBehavior(tex,
  342. urls,
  343. appearance,
  344. this);
  345. animate.setSchedulingBounds(bounds);
  346. objTrans.addChild(animate);
  347. // add a rotation behavior so we can see all sides of the tetrahedron
  348. Transform3D yAxis = new Transform3D();
  349. Alpha rotorAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
  350. 0, 0,
  351. 4000, 0, 0,
  352. 0, 0, 0);
  353. RotationInterpolator rotator =
  354. new RotationInterpolator(rotorAlpha,
  355. objTrans,
  356. yAxis,
  357. 0.0f, (float) Math.PI*2.0f);
  358. rotator.setSchedulingBounds(bounds);
  359. objTrans.addChild(rotator);
  360. // have java3d perform optimizations on this scene graph
  361. objRoot.compile();
  362. return objRoot;
  363. }
  364. // callback for the animation button and delay text field
  365. public void actionPerformed(ActionEvent e) {
  366. Object o = e.getSource();
  367. // for the animation button
  368. if (o == animationB) {
  369. if (animate.getEnable()) {
  370. animate.setEnable(false);
  371. animationB.setText("start animation");
  372. }
  373. else {
  374. animate.setEnable(true);
  375. animationB.setText(" stop animation ");
  376. }
  377. }
  378. // for the texByRef button
  379. else if (o == texByRef && texByRef.isSelected()) {
  380. animate.setByReference(true);
  381. }
  382. // texByCopy button
  383. else if (o == texByCopy && texByCopy.isSelected()) {
  384. animate.setByReference(false);
  385. }
  386. // yUp button
  387. else if (o == yUp && yUp.isSelected()) {
  388. animate.setYUp(true);
  389. }
  390. // ydown button
  391. else if (o == yDown && yDown.isSelected()) {
  392. animate.setYUp(false);
  393. }
  394. //geomByRef button
  395. else if (o == geomByRef) {
  396. tetra.setByReference(true);
  397. }
  398. // geomByCopy button
  399. else if (o == geomByCopy) {
  400. tetra.setByReference(false);
  401. }
  402. // TYPE_INT_ARGB
  403. else if (o == imgIntARGB) {
  404. animate.setImageType(BufferedImage.TYPE_INT_ARGB);
  405. }
  406. // TYPE_4BYTE_ABGR
  407. else if (o == img4ByteABGR) {
  408. animate.setImageType(BufferedImage.TYPE_4BYTE_ABGR);
  409. }
  410. // TYPE_3BYTE_BGR
  411. else if (o == img3ByteBGR) {
  412. animate.setImageType(BufferedImage.TYPE_3BYTE_BGR);
  413. }
  414. // TYPE_CUSTOM RGBA
  415. else if (o == imgCustomRGBA) {
  416. animate.setImageTypeCustomRGBA();
  417. }
  418. // TYPE_CUSTOM RGB
  419. else if (o == imgCustomRGB) {
  420. animate.setImageTypeCustomRGB();
  421. }
  422. }
  423. // callback for the checkboxes
  424. public void itemStateChanged(ItemEvent e) {
  425. Object o = e.getSource();
  426. // for the flip checkbox
  427. if (o == flipB) {
  428. if (e.getStateChange() == ItemEvent.DESELECTED) {
  429. animate.setFlipImages(false);
  430. }
  431. else animate.setFlipImages(true);
  432. }
  433. }
  434. // callback for the slider
  435. public void stateChanged(ChangeEvent e) {
  436. Object o = e.getSource();
  437. // for the frame delay
  438. if (o == frameDelay) {
  439. animate.setFrameDelay(frameDelay.getValue());
  440. }
  441. }
  442. // allows TextureByReference to be run as an application as well as an applet
  443. public static void main(String[] args) {
  444. java.net.URL fnames[] = null;
  445. if (args.length > 1) {
  446. fnames = new java.net.URL[args.length];
  447. for (int i = 0; i < args.length; i++) {
  448. try {
  449. fnames[i] = new java.net.URL("file:" + args[i]);
  450. }
  451. catch (java.net.MalformedURLException ex) {
  452. System.out.println(ex.getMessage());
  453. }
  454. }
  455. }
  456. else {
  457. fnames = new java.net.URL[TextureByReference.defaultFiles.length];
  458. for (int i = 0; i < TextureByReference.defaultFiles.length; i++) {
  459. fnames[i] = Resources.getResource(defaultFiles[i]);
  460. if (fnames[i] == null) {
  461. System.err.println(TextureByReference.defaultFiles[i] + " not found");
  462. System.exit(1);
  463. }
  464. /*
  465. try {
  466. fnames[i] = new java.net.URL("file:" +
  467. TextureByReference.defaultFiles[i]);
  468. }
  469. catch (java.net.MalformedURLException ex) {
  470. System.out.println(ex.getMessage());
  471. System.exit(1);
  472. }
  473. */
  474. }
  475. }
  476. new MainFrame((new TextureByReference(fnames)), 650, 750);
  477. }
  478. }