Advertisement
Guest User

Java3D Cube with same image for each side

a guest
Nov 26th, 2014
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. package com.alejandrorg.cube3d;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.BorderLayout;
  5. import java.awt.Component;
  6. import java.awt.Frame;
  7. import java.awt.GraphicsConfiguration;
  8. import java.net.URL;
  9.  
  10. import javax.media.j3d.Appearance;
  11. import javax.media.j3d.BoundingSphere;
  12. import javax.media.j3d.BranchGroup;
  13. import javax.media.j3d.Canvas3D;
  14. import javax.media.j3d.ImageComponent2D;
  15. import javax.media.j3d.Material;
  16. import javax.media.j3d.Texture;
  17. import javax.media.j3d.Texture2D;
  18. import javax.media.j3d.TextureAttributes;
  19. import javax.media.j3d.Transform3D;
  20. import javax.media.j3d.TransformGroup;
  21. import javax.vecmath.Vector3f;
  22.  
  23. import com.sun.j3d.utils.applet.MainFrame;
  24. import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
  25. import com.sun.j3d.utils.geometry.Box;
  26. import com.sun.j3d.utils.geometry.ColorCube;
  27. import com.sun.j3d.utils.image.TextureLoader;
  28. import com.sun.j3d.utils.universe.SimpleUniverse;
  29.  
  30. //   MouseRotate2App renders a single, interactively rotatable cube.
  31.  
  32. public class CubeGenerator extends Applet {
  33.  
  34.     private Canvas3D canvas3D;
  35.  
  36.     public BranchGroup createSceneGraph() throws Exception {
  37.         // Create the root of the branch graph
  38.         BranchGroup objRoot = new BranchGroup();
  39.  
  40.         TransformGroup objRotate = null;
  41.         MouseRotate myMouseRotate = null;
  42.         Transform3D transform = new Transform3D();
  43.  
  44.         // create ColorCube and MouseRotate behvaior objects
  45.         transform.setTranslation(new Vector3f(-0.0f, 0.0f, -0.0f));
  46.         objRotate = new TransformGroup(transform);
  47.         objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  48.         objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  49.  
  50.         objRoot.addChild(objRotate);
  51.  
  52.         Appearance app = new Appearance();
  53.         URL texImage = new java.net.URL("file:laumono.jpg");
  54.         Texture tex = new TextureLoader(texImage, this).getTexture();
  55.         app.setTexture(tex);
  56.         TextureAttributes texAttr = new TextureAttributes();
  57.         texAttr.setTextureMode(TextureAttributes.MODULATE);
  58.         app.setTextureAttributes(texAttr);
  59.  
  60.         // Create textured cube and add it to the scene graph.
  61.         Box textureCube = new Box(0.4f, 0.4f, 0.4f,
  62.                 Box.GENERATE_TEXTURE_COORDS, app);
  63.  
  64.         objRotate.addChild(textureCube);
  65.         // objRotate.addChild(new ColorCube(0.4));
  66.  
  67.         myMouseRotate = new MouseRotate();
  68.         myMouseRotate.setTransformGroup(objRotate);
  69.         myMouseRotate.setSchedulingBounds(new BoundingSphere());
  70.         objRoot.addChild(myMouseRotate);
  71.  
  72.         // Let Java 3D perform optimizations on this scene graph.
  73.         objRoot.compile();
  74.  
  75.         return objRoot;
  76.     } // end of CreateSceneGraph method of MouseRotate2App
  77.  
  78.     // Create a simple scene and attach it to the virtual universe
  79.  
  80.     public CubeGenerator() throws Exception {
  81.         setLayout(new BorderLayout());
  82.         GraphicsConfiguration config = SimpleUniverse
  83.                 .getPreferredConfiguration();
  84.         canvas3D = new Canvas3D(config);
  85.         add("Center", canvas3D);
  86.  
  87.         BranchGroup scene = createSceneGraph();
  88.  
  89.         // SimpleUniverse is a Convenience Utility class
  90.         SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
  91.  
  92.         // This will move the ViewPlatform back a bit so the
  93.         // objects in the scene can be viewed.
  94.         simpleU.getViewingPlatform().setNominalViewingTransform();
  95.  
  96.         simpleU.addBranchGraph(scene);
  97.     } // end of MouseRotate2App (constructor)
  98.  
  99.     // The following allows this to be run as an application
  100.     // as well as an applet
  101.  
  102.     public Component getCanvas() {
  103.         return canvas3D;
  104.     }
  105.    
  106.       /**
  107.        * This defines the appearance with a texture. The texture is loaded from an
  108.        * external file.
  109.        *
  110.        * @return Appearance that uses the texture.
  111.        */
  112.       protected Appearance DefineAppearance() {
  113.         //Load the texture from the external image file
  114.         TextureLoader textLoad = new TextureLoader("laumono.jpg", this);
  115.         //Access the image from the loaded texture
  116.         ImageComponent2D textImage = textLoad.getImage();
  117.         //Create a two dimensional texture
  118.         Texture2D texture = new Texture2D(Texture2D.BASE_LEVEL, Texture.RGB,
  119.             textImage.getWidth(), textImage.getHeight());
  120.         //Set the texture from the image loaded
  121.         texture.setImage(0, textImage);
  122.         //Create the appearance that will use the texture
  123.         Appearance app = new Appearance();
  124.         app.setTexture(texture);
  125.         //Define how the texture will be mapped onto the surface
  126.         //by creating the appropriate texture attributes
  127.         TextureAttributes textAttr = new TextureAttributes();
  128.         textAttr.setTextureMode(TextureAttributes.REPLACE);
  129.         app.setTextureAttributes(textAttr);
  130.         app.setMaterial(new Material());
  131.         return app;
  132.       }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement