Java Reference
In-Depth Information
We will now discuss the last four most basic classes in the next few sections, noting that
the first two classes, GeometryStripArray and IndexedGeometryArray, are again abstract
classes which may also be useful and will be discussed in later sections.
pointarray
In the PointArray class, a series of vertices are drawn as individual points. Figure 8 shows
the code segment and result for using PointArray to declare a set of points on a curve based
on the equation y = x² and z=0. In line 13, the PointArray class is invoked and n vertices
with coordinate masks are created. Note that since z=0, the 3D curve degenerates to be-
come a 2D one. The variable r gives the x-axis range for the curve. This is set to be smaller
than or equal to 2.0f as the maximum x-axis display width on the screen is 2.0f. With the
Figure 8. Code segment and result of PointArrayCurve.java
1.
public class Curve
2.
{
3.
private BranchGroup axisBG;
4.
public Curve()
5.
{
//this curve is draw based on this function y = square(x)
6.
axisBG = new BranchGroup();
7.
float r = 1.8f, x, y, dx;
8.
9.
int n = 100; //n is number of points
10.
dx = r/n; //dx is the x distance between 2 nearest point
11.
12. //PointArray declaration of n vertices
13. PointArray pA = new PointArray(n,
GeometryArray.COORDINATES);
14.
15. / / add the PointArray object to the BranchGraph
16. axisBG.addChild(new Shape3D(pA));
17. for (int i=0; i<n; i++)
18. {
19. x = -0.9f + dx*i; //set the coordinate of x
20. y = x*x; //equivalent value of coordinate y
21. pA.setCoordinate(i, new Point3f(x, y, 0.0f));
22. }
23. }
24. public BranchGroup getBG()
25. {
26.
return axisBG;
27. }
28. }
Search WWH ::




Custom Search