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 Tetrahedron extends JFrame { public Tetrahedron() { initComponents (); setSize(600, 600); GraphicsConfiguration graphicsConfig = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(graphicsConfig); getContentPane().add("Center", canvas); BasicUniverse universe = new BasicUniverse(canvas, 8.0f); /* Generate a branch graph with a Tetrahedron and attach it to the scene graph */ BranchGroup scene = createTetrahedronGraph(); universe.addBranchGraph(scene); } public BranchGroup createTetrahedronGraph() { BranchGroup objRoot = new BranchGroup(); // Create the geometry arrays - // - a 4 element co-ordinate array of vertices // - a 12 element index array (4 faces by 3 vertices each) IndexedTriangleArray TetrahedronGeometry = new IndexedTriangleArray(4, GeometryArray.COORDINATES|GeometryArray.COLOR_3, 12); // Generate the co-ordinate array for the 4 vertices Point3d vertices[] = new Point3d[4]; vertices[0]= new Point3d(-1.0,-1.0,0.0); vertices[1]= new Point3d(1.0,-1.0,0.0); vertices[2]= new Point3d(0.0,1.0,0.0); vertices[3]= new Point3d(0.0,0.0,2.0); TetrahedronGeometry.setCoordinates(0,vertices); // Generate an indexed array which defines the co-ordinates of the 4 faces int faceIndices[] = { 0,2,1, // Base 0,1,3, // Side 1 2,0,3, // Side 2 1,2,3 // Side 3 }; TetrahedronGeometry.setCoordinateIndices(0,faceIndices); // Generate the colour array for the 4 vertices Color3f colours[] = new Color3f[4]; colours[0]= new Color3f(1.0f,1.0f,1.0f); colours[1]= new Color3f(1.0f,0.0f,0.0f); colours[2]= new Color3f(0.0f,1.0f,0.0f); colours[3]= new Color3f(0.0f,0.0f,1.0f); TetrahedronGeometry.setColors(0,colours); // Generate an indexed array which defines the colours of the 4 faces int colourIndices[] = { 0,2,1, // Base 0,1,3, // Side 1 2,0,3, // Side 2 1,2,3 // Side 3 }; TetrahedronGeometry.setColorIndices(0,colourIndices); // Create the Tetrahedron shape object Shape3D Tetrahedron = new Shape3D(TetrahedronGeometry); // Set the appearance of the Tetrahedron /* PolygonAttributes TetrahedronAttributes = new PolygonAttributes(); TetrahedronAttributes.setPolygonMode(PolygonAttributes.POLYGON_LINE); Appearance appear = new Appearance(); appear.setPolygonAttributes(TetrahedronAttributes); Tetrahedron.setAppearance(appear);*/ // Add the Tetrahedron into the branch graph objRoot.addChild(Tetrahedron); 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) { Tetrahedron myShape = new Tetrahedron(); myShape.setVisible(true); } }