import javax.media.j3d.*;
import javax.vecmath.*;


public class Java3dLine  extends Shape3D
{
   protected LineStripArray  lineArray      = null;

   protected int             stripVertexCounts[] = new int[1];

   protected int     lastVertex           = -1;

   protected int     maxVertexCount       = 1000;

   protected Color3f currentColor     = new Color3f( 1.0f, 1.0f, 1.0f );


   public  Java3dLine()
   {
      createGeometry();
      createAppearance();
   }



   private void  createGeometry()
   {
      stripVertexCounts[0] = maxVertexCount;

      int numVertices = maxVertexCount;

      int vertexFormat = LineStripArray.COORDINATES |
                         LineStripArray.COLOR_3;

      lineArray = new LineStripArray( numVertices,
                                      vertexFormat,
                                      stripVertexCounts );

      lineArray.setCapability( LineStripArray.ALLOW_COORDINATE_WRITE );
      lineArray.setCapability( LineStripArray.ALLOW_COLOR_WRITE );
      lineArray.setCapability( LineStripArray.ALLOW_COUNT_WRITE );

      setGeometry( lineArray );
   }



   private void  createAppearance()
   {
      Appearance lineAppearance = new Appearance();

      lineAppearance.setTransparencyAttributes( new TransparencyAttributes(
                                    TransparencyAttributes.BLENDED, 0.025f) );
      setAppearance( lineAppearance );
   }


   public void  addPoint( Point3d trackPoint )
   {
      // increment the last vertex
      lastVertex++;

      // when lastVertex reaches the end of the array, start over again
      if (lastVertex == maxVertexCount)
      {
         lastVertex = 0;
      }

      lineArray.setCoordinate( lastVertex, trackPoint );
      lineArray.setColor( lastVertex, currentColor );

      int actualHistoryLength = lastVertex + 1;

      // Make sure the length doesn't fall below 2 or it will throw an exception
      if ( actualHistoryLength < 2 )
      {
         actualHistoryLength = 2;
      }

      stripVertexCounts[0] = actualHistoryLength;

      // Set the length of the line
      lineArray.setStripVertexCounts( stripVertexCounts );
   }
}
