Java Reference

In-Depth Information

variable n corresponding to the number of points (vertices) for the curve, the variable dx

= r/n gives the distance along the x-axis between 2 adjacent points.

linearray

LineArray can be used to define an object based on drawing lines between pairs of points.

This is illustrated in the code segment in Figure 9, which results in a star shaped figure.

Note that the vertices must be provided in the form of pairs as Java 3D will render a

straight line, one pixel in thickness, between two vertices. Each of these vertices represents

the endpoints of a line. Different line width and style can be achieved by using shape ap-

pearance attributes.

Figure 9. Code segment and result of LineArrayStar.java

1.

public class Star

2.

{

3.

private BranchGroup axisBG;

4.

public Star()

5.

{

6.

axisBG = new BranchGroup();

7.

8. //define a LineArray of 10 vertices

9. LineArray lineArr = new LineArray(10, LineArray.COORDINATES);

10. axisBG.addChild(new Shape3D(lineArr));

11.

12. float r = 0.6f, x, y; //r is the distance from the origin to every vertex

13. Point3f coor[] = new Point3f[5];

14.

15. for (int i=0; i<=4; i++)

16. {

17. x = (float)Math.cos(Math.PI/180*(90+72*i))*r;

18. y = (float)Math.sin(Math.PI/180*(90+72*i))*r;

19. coor[i] = new Point3f(x, y, 0.0f);

20. }

21. for (int i=0; i<=4; i++)

22. {

23. lineArr.setCoordinate(i*2, coor[i]);

24. lineArr.setCoordinate(i*2+1, coor[(i+2) % 5]);

25. }

26. }

27. public BranchGroup getBG()

28. {

29.

return axisBG;

30. }

31. }

Search WWH ::

Custom Search