Java Reference
In-Depth Information
Obviously, utility classes such as PickRotationBehavior, PickTranslateBehavior and
PickZoomBehavior, which have been discussed in the beginning of this chapter, have rather
restricted performance and will not have enough flexibility in this application. Instead, we
need to develop a more dedicated and sophisticated picking behavior class to support the
development of different types of 3D instrument controls in the virtual laboratory.
Figure 26 shows the important code segment for implementing the picking behavior,
where the control being picked and the action to be taken is obtained from the type of mouse
events as well as the identity number of the picked objects or controls. The following gives
a more detailed description on the subtleties in the implementation.
Figure 26. First code segment for Oscilloscope.java
1.
public void processStimulus (Enumeration criteria) {
2.
...
3.
if (eventId == MouseEvent.MOUSE_MOVED) {
4.
Point3d []ptw = pi.getPrimitiveCoordinatesVW();
5.
Point3d []pt = pi.getPrimitiveCoordinates();
6.
if (pt.length() == 3) if (pt[0].z>0&&pt[1].z>0&&pt[2].z>0) {
7.
Point3f RPt = new Point3f(ptw[0]);
8.
obj = whichObject(RPt.x, RPt.y, RPt.z); }
9.
else {
10.
if (pt[0].z>=0&&pt[1].z>=0&&pt[2].z>=0&&pt[3].z>=0)
11.
if (pt[0].x>=0&&pt[1].x>=0&&pt[2].x>=0&&pt[3].x>=0) {
12.
Point3f RPt = new Point3f((ptw[2].x+ptw[3].x)/2.0f, (ptw[2].y+ptw[3].y)/2.0f, ptw[0].z);
13.
obj = whichObject(RPt.x, RPt.y, RPt.z); }
14.
else {
15.
Point3f RPt =new Point3f((ptw[0].x+ptw[2].x)/2.0f, (ptw[0].y+ptw[2].y)/2.0f, ptw[0].z);
16.
obj = whichObject(RPt.x, RPt.y, RPt.z); }}
17.
if (eventId == MouseEvent.MOUSE_PRESSED) {
18.
if (obj==id) … // activate or deactivate a button; … }
19.
if (eventId == MouseEvent.MOUSE_RELEASED) {
20.
if (obj==id) … // deactivate a button or send out a network command; …}
21.
if (eventId == MouseEvent.MOUSE_DRAGGED) {
22.
if (obj==id) … // rotate a knob, or drag a slider or connector; …}
23. …
24. public int whichObject(float x, float y, float z) {
25.
if (isObject(posi[i], x, y, z)) return id;
26.
else return 0;
27. }
28.
29. public boolean isObject(Point3f object, float x, float y, float z) {
30.
float d;
31.
d = (object.x-x)*(object.x-x)+(object.y-y)*(object.y-y)+ (object.z-z)*(object.z-z);
32.
if (d < DIFFERENCE) return true;
33.
else return false;
34. }
Search WWH ::




Custom Search