Java Reference
In-Depth Information
user-defIned mouse navIGatIon
The reason for implementing a user-defined mouse navigation tool and behavior is the same
as that in user-defined keyboard navigation. Specifically, while it takes more programming
Figure 10. First code segment for MouseNavigation.java
1.
public class MouseNavigation extends Applet
2.
{
3.
private static final int UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4;
4.
private float x, y, z, turningAngle = 0.0f; int direction = 1;
5.
private Matrix4d matrix = new Matrix4d(); private Matrix3f comMat = new Matrix3f();
6.
private static final float PAI = 3.14159f, ANGLEPAI = 2.0f*PAI/360.0f, SPAN = 0.02f,
7.
ANGLESTEP = 0.01f;
8.
private WakeupCriterion events[] = new WakeupCriterion[4]; private WakeupOr allEvents;
9.
SimpleUniverse universe = null;
10.
11.
public class MouseMovement extends Behavior
12.
{
13.
private TransformGroup targetTG;
14.
Point3f viewposi = new Point3f(0.0f,0.0f,2.5f);
15.
16.
MouseMovement (TransformGroup targetTG) {
17.
this.targetTG = targetTG; } // create behavior
18.
19.
public void initialize() {
20.
events[0]=new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
21.
events[1]=new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
22.
events[2]=new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED);
23.
events[3]=new WakeupOnAWTEvent(MouseEvent.MOUSE_MOVED);
24.
allEvents=new WakeupOr(events); wakeupOn(allEvents);
25.
comMat.m00 = 1.0f; comMat.m01 = 0.0f; comMat.m02 = 0.0f;
26.
comMat.m10 = 0.0f; comMat.m11 = 1.0f; comMat.m12 = 0.0f;
27.
comMat.m20 = 0.0f; comMat.m21 = 0.0f; comMat.m22 = 1.0f; }
28.
29.
public void MouseEvent(int direction) {
30.
switch(direction) {
31.
case UP:
viewposi.x = viewposi.x-3.0f*SPAN*(float)Math.sin(turningAngle);
32.
viewposi.z = viewposi.z-3.0f*SPAN*(float)Math.cos(turningAngle);
33.
setPosition3D(targetTG, viewposi); break;
34.
case DOWN:
viewposi.x = viewposi.x+1.0f*SPAN*(float)Math.sin(turningAngle);
35.
viewposi.z = viewposi.z+1.0f*SPAN*(float)Math.cos(turningAngle);
36.
setPosition3D(targetTG, viewposi); break;
37.
case LEFT:
turningAngle += ANGLESTEP;
38.
setRotation3D(targetTG, ANGLESTEP, comMat, 1); break;
39.
case RIGHT:
turningAngle -= ANGLESTEP;
40.
setRotation3D(targetTG, -ANGLESTEP, comMat, 1); break;
41.
default: } }
42.
Search WWH ::




Custom Search