Java Reference
In-Depth Information
Finally, after setting the IndexedLineArray object array up, it is constituted as a Shape3D
node, which, in turn, is added as a child to the BranchGroup axisBG for the rendering of
the trapezoidal object.
As another example, the code segment in Figures 36 and 37 illustrates the use of Indexed-
LineArray to construct a sphere by using a series of circles. Specifically, the drawSphere
method in the code segment will construct a sphere of radius given by the parameter size
at the origin. It draws the sphere by iteratively invoking a drawCircle method, which builds
a circle at (x, y, z) using n vertices with a radius given by the parameter size.
The drawing of the circle in the drawCircle method is carried out by drawing lines be-
tween adjacent equally spaced points along the circumference of the circle. The resulting
shape will converge towards that for the desired circle if the number of points used is large
Figure 37. Second code segment and result of DemoSphere.java
1.
public IndexedLineArray drawCircle(int n, float size, double x, double y, double z, Color3f color)
2.
{
3.
IndexedLineArray lineArray =
4.
new IndexedLineArray(n+1,LineArray.COORDINATES|LineArray.COLOR_3,2*(n+1));
5.
6.
Point3f []linePts = new Point3f[n+1];
7.
for (int i=0; i<=n; i++)
8.
{
9.
float a = (float)(2*Math.PI/(n));
10.
linePts[i]= new
11.
Point3f((size*Math.cos(a*i)+x),(size*Math.sin(a*i)+y),(float)z);
12.
}
13.
14.
int []lineIndices = new int[2*n];
Z
15.
for (int i=1, j=1; i<n+1; i++, j=j+2)
16.
{
17.
lineIndices[j-1]=i-1;
18.
lineIndices[j]=i;
19.
}
20.
21.
lineArray.setCoordinates(0,linePts);
22.
lineArray.setCoordinateIndices(0, lineIndices);
23.
24.
Color3f []lineColor = new Color3f[1];
25.
lineColor[0] = color;
26.
lineArray.setColors(0,lineColor);
27.
}
28.
29.
public BranchGroup getBG()
30.
{
31.
return axisBG;
32.
}
33. }
34.
Search WWH ::

Custom Search