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 package edu.uci.ics.jung.visualization3d.control;
46
47 import java.awt.AWTEvent;
48 import java.awt.Event;
49 import java.awt.event.MouseEvent;
50 import java.util.Enumeration;
51
52 import javax.media.j3d.Behavior;
53 import javax.media.j3d.Bounds;
54 import javax.media.j3d.BranchGroup;
55 import javax.media.j3d.Canvas3D;
56 import javax.media.j3d.TransformGroup;
57 import javax.media.j3d.WakeupCriterion;
58 import javax.media.j3d.WakeupOnAWTEvent;
59 import javax.media.j3d.WakeupOr;
60
61 import com.sun.j3d.utils.picking.PickCanvas;
62 import com.sun.j3d.utils.picking.PickTool;
63
64
65
66
67
68
69
70
71 public abstract class PickMouseBehavior extends Behavior {
72
73 protected PickCanvas pickCanvas;
74
75 protected WakeupCriterion[] conditions;
76 protected WakeupOr wakeupCondition;
77 protected boolean buttonPress = false;
78
79 protected TransformGroup currGrp;
80 protected static final boolean debug = false;
81 protected MouseEvent mevent;
82
83
84
85
86
87 public PickMouseBehavior(Canvas3D canvas, BranchGroup root, Bounds bounds){
88 super();
89 currGrp = new TransformGroup();
90 currGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
91 currGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
92 root.addChild(currGrp);
93 pickCanvas = new PickCanvas(canvas, root);
94 }
95
96
97
98
99
100 public void setMode(int pickMode) {
101 pickCanvas.setMode(pickMode);
102 }
103
104
105
106
107
108
109 public int getMode() {
110 return pickCanvas.getMode();
111 }
112
113
114
115
116
117 public void setTolerance(float tolerance) {
118 pickCanvas.setTolerance(tolerance);
119 }
120
121
122
123
124
125 public float getTolerance() {
126 return pickCanvas.getTolerance();
127 }
128
129 public void initialize() {
130
131 conditions = new WakeupCriterion[2];
132 conditions[0] = new WakeupOnAWTEvent(Event.MOUSE_MOVE);
133 conditions[1] = new WakeupOnAWTEvent(Event.MOUSE_DOWN);
134 wakeupCondition = new WakeupOr(conditions);
135
136 wakeupOn(wakeupCondition);
137 }
138
139 private void processMouseEvent(MouseEvent evt) {
140 buttonPress = false;
141
142 if (evt.getID()==MouseEvent.MOUSE_PRESSED |
143 evt.getID()==MouseEvent.MOUSE_CLICKED) {
144 buttonPress = true;
145 return;
146 }
147 else if (evt.getID() == MouseEvent.MOUSE_MOVED) {
148
149 }
150 }
151
152 public void processStimulus (Enumeration criteria) {
153 WakeupCriterion wakeup;
154 AWTEvent[] evt = null;
155 int xpos = 0, ypos = 0;
156
157 while(criteria.hasMoreElements()) {
158 wakeup = (WakeupCriterion)criteria.nextElement();
159 if (wakeup instanceof WakeupOnAWTEvent)
160 evt = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
161 }
162
163 if (evt[0] instanceof MouseEvent){
164 mevent = (MouseEvent) evt[0];
165
166 if (debug)
167 System.out.println("got mouse event");
168 processMouseEvent((MouseEvent)evt[0]);
169 xpos = mevent.getPoint().x;
170 ypos = mevent.getPoint().y;
171 }
172
173 if (debug)
174 System.out.println("mouse position " + xpos + " " + ypos);
175
176 if (buttonPress){
177 updateScene(xpos, ypos);
178 }
179 wakeupOn (wakeupCondition);
180 }
181
182
183
184 public abstract void updateScene(int xpos, int ypos);
185
186 }
187