import javax.media.j3d.*; import javax.vecmath.*; /** * Adds a solid cube to the scene graph */ public class CubeBranch extends TransformGroup { Shape3D cubeShape; Appearance appear = new Appearance(); Material material = new Material(); public CubeBranch() { cubeShape = new Shape3D(); QuadArray cubeArray = new QuadArray(24, QuadArray.COORDINATES | QuadArray.NORMALS); Point3d[] cubeVertices = {new Point3d(0.0, 0.0, 0.0), new Point3d(1.0, 0.0, 0.0), new Point3d(1.0, 1.0, 0.0), new Point3d(0.0, 1.0, 0.0), new Point3d(0.0, 0.0, 1.0), new Point3d(1.0, 0.0, 1.0), new Point3d(1.0, 1.0, 1.0), new Point3d(0.0, 1.0, 1.0)}; Point3d[] cubeFaces = { cubeVertices[0], cubeVertices[3], cubeVertices[2], cubeVertices[1], cubeVertices[0], cubeVertices[4], cubeVertices[7], cubeVertices[3], cubeVertices[0], cubeVertices[1], cubeVertices[5], cubeVertices[4], cubeVertices[6], cubeVertices[2], cubeVertices[3], cubeVertices[7], cubeVertices[6], cubeVertices[5], cubeVertices[1], cubeVertices[2], cubeVertices[6], cubeVertices[7], cubeVertices[4], cubeVertices[5] }; cubeArray.setCoordinates(0, cubeFaces); Vector3f[] normalVector = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f) }; Vector3f[] normals = { normalVector[1], normalVector[1], normalVector[1], normalVector[1], normalVector[2], normalVector[2], normalVector[2], normalVector[2], normalVector[4], normalVector[4], normalVector[4], normalVector[4], normalVector[5], normalVector[5], normalVector[5], normalVector[5], normalVector[3], normalVector[3], normalVector[3], normalVector[3], normalVector[0], normalVector[0], normalVector[0], normalVector[0] }; cubeArray.setNormals(0, normals); cubeShape.setGeometry(cubeArray); cubeShape.setAppearance(createAppearance()); setCapability(TransformGroup.ALLOW_TRANSFORM_READ); setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); setCapability(TransformGroup.ENABLE_PICK_REPORTING); addChild(cubeShape); Transform3D translate = new Transform3D(); getTransform(translate); translate.setTranslation(new Vector3d(0.3, -0.5, 0.0)); setTransform(translate); } public Material getMaterial() { return material; } private Appearance createAppearance() { material.setCapability(Material.ALLOW_COMPONENT_WRITE); material.setCapability(Material.ALLOW_COMPONENT_READ); appear.setCapability(Appearance.ALLOW_MATERIAL_WRITE); appear.setCapability(Appearance.ALLOW_MATERIAL_READ); appear.setMaterial(material); ColoringAttributes colorAtt = new ColoringAttributes(); colorAtt.setColor(new Color3f(1.0f, 0.5f,0.2f)); colorAtt.setShadeModel(ColoringAttributes.SHADE_GOURAUD); appear.setColoringAttributes(colorAtt); return appear; } } // CubeBranch