Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.alejandrorg.cube3d;
- import java.applet.Applet;
- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.Frame;
- import java.awt.GraphicsConfiguration;
- import java.net.URL;
- import javax.media.j3d.Appearance;
- import javax.media.j3d.BoundingSphere;
- import javax.media.j3d.BranchGroup;
- import javax.media.j3d.Canvas3D;
- import javax.media.j3d.ImageComponent2D;
- import javax.media.j3d.Material;
- import javax.media.j3d.Texture;
- import javax.media.j3d.Texture2D;
- import javax.media.j3d.TextureAttributes;
- import javax.media.j3d.Transform3D;
- import javax.media.j3d.TransformGroup;
- import javax.vecmath.Vector3f;
- import com.sun.j3d.utils.applet.MainFrame;
- import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
- import com.sun.j3d.utils.geometry.Box;
- import com.sun.j3d.utils.geometry.ColorCube;
- import com.sun.j3d.utils.image.TextureLoader;
- import com.sun.j3d.utils.universe.SimpleUniverse;
- // MouseRotate2App renders a single, interactively rotatable cube.
- public class CubeGenerator extends Applet {
- private Canvas3D canvas3D;
- public BranchGroup createSceneGraph() throws Exception {
- // Create the root of the branch graph
- BranchGroup objRoot = new BranchGroup();
- TransformGroup objRotate = null;
- MouseRotate myMouseRotate = null;
- Transform3D transform = new Transform3D();
- // create ColorCube and MouseRotate behvaior objects
- transform.setTranslation(new Vector3f(-0.0f, 0.0f, -0.0f));
- objRotate = new TransformGroup(transform);
- objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
- objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
- objRoot.addChild(objRotate);
- Appearance app = new Appearance();
- URL texImage = new java.net.URL("file:laumono.jpg");
- Texture tex = new TextureLoader(texImage, this).getTexture();
- app.setTexture(tex);
- TextureAttributes texAttr = new TextureAttributes();
- texAttr.setTextureMode(TextureAttributes.MODULATE);
- app.setTextureAttributes(texAttr);
- // Create textured cube and add it to the scene graph.
- Box textureCube = new Box(0.4f, 0.4f, 0.4f,
- Box.GENERATE_TEXTURE_COORDS, app);
- objRotate.addChild(textureCube);
- // objRotate.addChild(new ColorCube(0.4));
- myMouseRotate = new MouseRotate();
- myMouseRotate.setTransformGroup(objRotate);
- myMouseRotate.setSchedulingBounds(new BoundingSphere());
- objRoot.addChild(myMouseRotate);
- // Let Java 3D perform optimizations on this scene graph.
- objRoot.compile();
- return objRoot;
- } // end of CreateSceneGraph method of MouseRotate2App
- // Create a simple scene and attach it to the virtual universe
- public CubeGenerator() throws Exception {
- setLayout(new BorderLayout());
- GraphicsConfiguration config = SimpleUniverse
- .getPreferredConfiguration();
- canvas3D = new Canvas3D(config);
- add("Center", canvas3D);
- BranchGroup scene = createSceneGraph();
- // SimpleUniverse is a Convenience Utility class
- SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
- // This will move the ViewPlatform back a bit so the
- // objects in the scene can be viewed.
- simpleU.getViewingPlatform().setNominalViewingTransform();
- simpleU.addBranchGraph(scene);
- } // end of MouseRotate2App (constructor)
- // The following allows this to be run as an application
- // as well as an applet
- public Component getCanvas() {
- return canvas3D;
- }
- /**
- * This defines the appearance with a texture. The texture is loaded from an
- * external file.
- *
- * @return Appearance that uses the texture.
- */
- protected Appearance DefineAppearance() {
- //Load the texture from the external image file
- TextureLoader textLoad = new TextureLoader("laumono.jpg", this);
- //Access the image from the loaded texture
- ImageComponent2D textImage = textLoad.getImage();
- //Create a two dimensional texture
- Texture2D texture = new Texture2D(Texture2D.BASE_LEVEL, Texture.RGB,
- textImage.getWidth(), textImage.getHeight());
- //Set the texture from the image loaded
- texture.setImage(0, textImage);
- //Create the appearance that will use the texture
- Appearance app = new Appearance();
- app.setTexture(texture);
- //Define how the texture will be mapped onto the surface
- //by creating the appropriate texture attributes
- TextureAttributes textAttr = new TextureAttributes();
- textAttr.setTextureMode(TextureAttributes.REPLACE);
- app.setTextureAttributes(textAttr);
- app.setMaterial(new Material());
- return app;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement