-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyMouseRotate.java
120 lines (90 loc) · 3.64 KB
/
MyMouseRotate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.AWTEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.Enumeration;
import javax.media.j3d.Transform3D;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOr;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
class MyMouseRotate extends MouseRotate {
private int id;
private AWTEvent[] event;
private WakeupCriterion wakeup;
private WakeupCriterion[] mouseEvents;
private WakeupOr mouseCriterion;
private int mouseX;
private int mouseY;
private double alfa = 0.0;
private double beta = 0.0;
private double r = 2.5;
public MyMouseRotate() {
super();
}
public void initialize() {
mouseEvents = new WakeupCriterion[5];
mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED);
mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
mouseEvents[2] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
mouseEvents[3] = new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED);
mouseEvents[4] = new WakeupOnAWTEvent(MouseEvent.MOUSE_WHEEL);
mouseCriterion = new WakeupOr(mouseEvents);
wakeupOn(mouseCriterion);
}
public void processStimulus(Enumeration criteria) {
id = 0;
while (criteria.hasMoreElements()) {
wakeup = (WakeupCriterion) criteria.nextElement();
if (wakeup instanceof WakeupOnAWTEvent) {
event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
for (int i = 0; i < event.length; i++) {
id = event[i].getID();
if (id == MouseEvent.MOUSE_DRAGGED) {
if (((MouseEvent) event[i]).getModifiers() == MouseEvent.BUTTON1_MASK) {
int dX, dY;
dX = ((MouseEvent) event[i]).getX() - mouseX;
dY = ((MouseEvent) event[i]).getY() - mouseY;
alfa -= dX/300.0;
beta += dY/300.0;
Transform3D locator = new Transform3D();
Point3d eye = new Point3d(r*Math.sin(alfa)*Math.cos(beta), r*Math.sin(beta), r*Math.cos(alfa)*Math.cos(beta));
Point3d center = new Point3d(0.0, 0.0, 0.0);
Vector3d up = new Vector3d(0.0, Math.cos(beta), 0.0);
locator.lookAt(eye, center, up);
locator.invert();
transformGroup.setTransform(locator);
mouseX = ((MouseEvent) event[i]).getX();
mouseY = ((MouseEvent) event[i]).getY();
}
}
else if (id == MouseEvent.MOUSE_PRESSED) {
mouseX = ((MouseEvent) event[i]).getX();
mouseY = ((MouseEvent) event[i]).getY();
if ((((MouseEvent) event[i]).getModifiers() & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK) {
if (JavaSample.running) {
JavaSample.running = false;
} else {
JavaSample.running = true;
}
}
} else if (id == MouseEvent.MOUSE_WHEEL) {
MouseWheelEvent e = (MouseWheelEvent)event[i];
r += 0.005f*e.getUnitsToScroll();
Transform3D locator = new Transform3D();
Point3d eye = new Point3d(r*Math.sin(alfa)*Math.cos(beta), r*Math.sin(beta), r*Math.cos(alfa)*Math.cos(beta));
Point3d center = new Point3d(0.0, 0.0, 0.0);
Vector3d up = new Vector3d(0.0, Math.cos(beta), 0.0);
locator.lookAt(eye, center, up);
locator.invert();
transformGroup.setTransform(locator);
mouseX = ((MouseEvent) event[i]).getX();
mouseY = ((MouseEvent) event[i]).getY();
}
this.wakeupOn(new WakeupOr(mouseEvents));
}
}
}
}
}