import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.Triangulator;
import com.sun.j3d.utils.geometry.NormalGenerator;

public class HoleTest extends Applet {

    private SimpleUniverse u = null;
private double[]        m_VertexArray = {1,1,0, //0
                                                                0,3,0, //1
                                                                1,5,0, //2
                                                                2,4,0, //3
                                                                4,5,0, //4
                                                                3,3,0, //5
                                                                4,2,0, //6
                                                                4,0,0, //7
                                                                3,0,0, //8
                                                                2,1,0, //9
//these are vertices for the hole
                                                                1,3,0, //10
                                                                2,3,0, //11
                                                                3,2,0, //12
                                                                3,1,0, //13
                                                                2,2,0};//14
   public BranchGroup createSceneGraph() {
	// Create the root of the branch graph
	BranchGroup objRoot = new BranchGroup();

// triangulate the polygon
GeometryInfo gi = new GeometryInfo( GeometryInfo.POLYGON_ARRAY );

gi.setCoordinates( m_VertexArray );

//the first 10 points make up the outer edge of the polygon, the next five
//make up the hole
int[] stripCountArray = {10,5};
int[] countourCountArray = {stripCountArray.length};

gi.setContourCounts( countourCountArray );
gi.setStripCounts( stripCountArray );

Triangulator triangulator = new Triangulator();
triangulator.triangulate( gi );

//also generate normal vectors so that the surface can be light
NormalGenerator normalGenerator = new NormalGenerator();
normalGenerator.generateNormals( gi );

//create an appearance
Appearance ap = new Appearance();

//render as a wireframe
PolygonAttributes polyAttrbutes = new PolygonAttributes();
polyAttrbutes.setPolygonMode( PolygonAttributes.POLYGON_LINE );
polyAttrbutes.setCullFace( PolygonAttributes.CULL_NONE ) ;
ap.setPolygonAttributes( polyAttrbutes );

//add both a wireframe and a solid version of the triangulated surface
Shape3D shape1 = new Shape3D( gi.getGeometryArray(), ap );
Shape3D shape2 = new Shape3D( gi.getGeometryArray() );
       objRoot.addChild(shape1);

 /*
        GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
        double[] coordData = new double[] {
          0.0, 0.0, 0.0,
          0.4, 0.0, 0.0,
          0.4, 0.4, 0.0,
          0.0, 0.4, 0.0,

          0.2, 0.2, 0.0,
          0.2, 0.4, 0.0,
          0.4, 0.4, 0.0,
          0.4, 0.2, 0.0,
        };
        gi.setCoordinates(coordData);

       int[] stripCount = new int[] {4, 4};
       gi.setStripCounts(stripCount);

       int[] contourCount = new int[] {1,1};
       gi.setContourCounts(contourCount);

       Geometry geometry3d = gi.getGeometryArray();

       Shape3D shape3d = new Shape3D(geometry3d);

       objRoot.addChild(shape3d);
*/
      return objRoot;
    }

    public HoleTest() {
    }

    public void init() {
	setLayout(new BorderLayout());
        GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();

	Canvas3D c = new Canvas3D(config);
	add("Center", c);

	// Create a simple scene and attach it to the virtual universe
	BranchGroup scene = createSceneGraph();
	u = new SimpleUniverse(c);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        Transform3D viewTransform = new Transform3D();
        viewTransform.set(new Vector3d(0.0, 0.0, 30)); // View at 30 meters.
  //      u.getViewingPlatform().setNominalViewingTransform();
        u.getViewingPlatform().getViewPlatformTransform().setTransform(viewTransform);

	u.addBranchGraph(scene);
    }

    public void destroy() {
	u.removeAllLocales();
    }

    public static void main(String[] args) {
	new MainFrame(new HoleTest(), 256, 256);
    }
}
