import java.awt.*; import java.awt.event.*; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.universe.*; import javax.media.j3d.*; import javax.vecmath.*; import javax.swing.*; public class Gasket extends JFrame { public Gasket() { initComponents (); setSize(600, 600); GraphicsConfiguration graphicsConfig = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(graphicsConfig); getContentPane().add("Center", canvas); BasicUniverse universe = new BasicUniverse(canvas, 8.0f); // Rotate the view platform by PI/4 radians about X & Y TransformGroup cubeTransform = universe.getViewPlatformTransform(); Transform3D transform = new Transform3D(); transform.rotX(Math.PI/4.0); Transform3D yRotate = new Transform3D(); yRotate.rotY(Math.PI/4.0); Transform3D currentTransform = new Transform3D(); transform.mul(yRotate); cubeTransform.getTransform(currentTransform); transform.mul(currentTransform); cubeTransform.setTransform(transform); /* Generate a branch graph for a cube with holes and attach it to the scene graph */ BranchGroup scene = createGasketGraph(); universe.addBranchGraph(scene); } public BranchGroup createGasketGraph() { BranchGroup objRoot = new BranchGroup(); GeometryInfo gasket = new GeometryInfo(GeometryInfo.POLYGON_ARRAY); // Generate the co-ordinate array for the 6 faces and 3 holes double[] gasketCoordinates = { // Bottom face of cube -1,-1,-1, 1,-1,-1, 1,-1, 1, -1,-1, 1, // Hole in bottom face -0.5,-1,-0.5, 0.5,-1,-0.5, 0.5,-1, 0.5, -0.5,-1, 0.5, // Front face of cube 1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1, // Hole in front face 0.5, 0.5, 1, -0.5, 0.5, 1, -0.5,-0.5, 1, 0.5,-0.5, 1, // Right face of cube 1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1, // Hole in right face 1, 0.5, 0.5, 1,-0.5, 0.5, 1,-0.5,-0.5, 1, 0.5,-0.5, // Left face of cube -1,-1,-1, -1,-1, 1, -1, 1, 1, -1, 1,-1, // Top face of cube 1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1, // Back face of cube -1,-1,-1, -1, 1,-1, 1, 1,-1, 1,-1,-1 }; gasket.setCoordinates(gasketCoordinates); // Generate a strip count array which associates co-ordinates with polygons int gasketStripCount[] = { 4,4, // Bottom face and its hole 4,4, // Front face and its hole 4,4, // Right face and its hole 4, // Left face 4, // Top face 4 // Back face }; gasket.setStripCounts(gasketStripCount); // Generate a contour count array which speficies polygons in the // strip count array to be containers and holes int gasketContourCount[] = { 2, // The first 2 element are the bottom and its hole 2, // The next 2 are the front face and its hole 2, // Then the right face and its hole 1, // The left face has no hole 1, // Nor does the top face 1, // Finally the back face brings up the rear }; gasket.setContourCounts(gasketContourCount); // Perform an additional random triangulation // Triangulator triang = new Triangulator(1); // triang.triangulate(gasket); // Generate normals for each vertex NormalGenerator norma = new NormalGenerator(); norma.generateNormals(gasket); // Turn into a triangle strip array Stripifier stripper = new Stripifier(); stripper.stripify(gasket); // Create the gasket shape object Shape3D gasketShape = new Shape3D(); gasketShape.setGeometry(gasket.getGeometryArray()); // Set the appearance of the gasket Appearance gasketAppearance = new Appearance(); // Wire frame (comment this out for a polygon fill) PolygonAttributes gasketPolyAtts = new PolygonAttributes(); gasketPolyAtts.setPolygonMode(PolygonAttributes.POLYGON_LINE); gasketAppearance.setPolygonAttributes(gasketPolyAtts); // Make it blue ColoringAttributes gasketColAtts = new ColoringAttributes(); gasketColAtts.setColor(new Color3f(0.0f,0.0f,1.0f)); gasketAppearance.setColoringAttributes(gasketColAtts); gasketShape.setAppearance(gasketAppearance); // Add the gasket to the branch group objRoot.addChild(gasketShape); return objRoot; } 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) { Gasket myShape = new Gasket(); myShape.setVisible(true); } }