import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.universe.*; import javax.media.j3d.*; import javax.vecmath.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; // import com.sun.j3d.utils.image.TextureLoader; import com.sun.j3d.utils.image.*; import com.sun.j3d.utils.behaviors.mouse.*; public class BackgroundDemo extends JFrame { BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); public BackgroundDemo() { initComponents (); setSize(600, 600); setTitle("Textured background demo"); GraphicsConfiguration graphicsConfig = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(graphicsConfig); getContentPane().add("Center", canvas); BasicUniverse universe = new BasicUniverse(canvas, 8.0f); BranchGroup scene = createCubeGraph(); TransformGroup viewTrans = universe.getViewPlatformTransform(); // Create the rotate behavior node MouseRotate behavior1 = new MouseRotate(viewTrans); scene.addChild(behavior1); behavior1.setSchedulingBounds(bounds); // Create the zoom behavior node MouseZoom behavior2 = new MouseZoom(viewTrans); scene.addChild(behavior2); behavior2.setSchedulingBounds(bounds); // Create the translate behavior node MouseTranslate behavior3 = new MouseTranslate(viewTrans); scene.addChild(behavior3); behavior3.setSchedulingBounds(bounds); universe.addBranchGraph(scene); universe.addBranchGraph(addBackground()); } public BranchGroup createCubeGraph() { BranchGroup cubeBranch = new BranchGroup(); Transform3D cubeTrans = new Transform3D(); cubeTrans.rotX(Math.PI/6.0); TransformGroup cubeGroup = new TransformGroup(cubeTrans); cubeGroup.addChild(new ColorCube()); cubeBranch.addChild(cubeGroup); return cubeBranch; } private BranchGroup addBackground() { BranchGroup backgroundRoot = new BranchGroup(); // Create the background object, the sphere to be textured, // and connect the sphere to the background as its geometry Background background = new Background(); background.setApplicationBounds(bounds); BranchGroup backGeoBranch = new BranchGroup(); Sphere backSphere = new Sphere(1.0f, Sphere.GENERATE_NORMALS_INWARD | Sphere.GENERATE_TEXTURE_COORDS, 20); backGeoBranch.addChild(backSphere); background.setGeometry(backGeoBranch); // Create a texture from the image file TextureLoader tex = new TextureLoader("carpet.jpg",this); // Scale the texture so it's about the same size as // in the original file (factor of 20 seems to do this) TextureAttributes texAttr = new TextureAttributes(); Transform3D textureTrans = new Transform3D(); textureTrans.setScale(20); texAttr.setTextureTransform(textureTrans); Appearance backgroundApp = backSphere.getAppearance(); backgroundApp.setTextureAttributes(texAttr); // Attach the texture to the background's appearance if (tex != null) backgroundApp.setTexture(tex.getTexture()); backgroundRoot.addChild(background); return backgroundRoot; } private void initComponents() { addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent evt) {exitForm (evt);} } ); } /*** Exit Application ***/ private void exitForm(WindowEvent evt) {System.exit (0);} public static void main(String[] args) { BackgroundDemo thisDemo = new BackgroundDemo(); thisDemo.setVisible(true); } }