Java Reference
In-Depth Information
Figure 10. First code segment of MouseBehaviorUserDefine.java
1.
public class MouseBehaviourUserDefine extends Applet
2.
{
3.
private static final int UP = 1;
4.
private static final int DOWN = 2;
5.
private static final int LEFT = 3;
6.
private static final int RIGHT = 4;
7.
8.
private WakeupCriterion events[] = new WakeupCriterion[4];
9.
private WakeupOr allEvents;
10.
11.
public class MouseMovement extends Behavior
12.
{
13.
private TransformGroup targetTG;
14.
private Transform3D trans = new Transform3D();
15.
private double TransX = 0.0;
16.
private double TransY = 0.0;
17.
18.
MouseMovement (TransformGroup targetTG) this.targetTG = targetTG;
19.
20.
public void initialize()
21.
{
22.
//set initial wakeup condition
23.
events[0]=new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
24.
events[1]=new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
25.
events[2]=new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED);
26.
events[3]=new WakeupOnAWTEvent(MouseEvent.MOUSE_MOVED);
27.
28.
allEvents=new WakeupOr(events);
29.
wakeupOn(allEvents);
30.
}
31.
this shift right operation has finished, an appropriate postID will be posted to wake up a
partner MoveLeft class in line 51.
The MoveLeft class is presented from lines 57 onwards. As shown in line 81, it is
initialized by WakeupOnBehaviorPost instead of WakeupOnAWTEvent. Line 86 checks
if the postId for the wakeup is proper. If it is, lines 88 to 90 will be executed, setting the
next WakeupCriterion to be AWTEvent or the pressing of a key. When the next key stroke
is detected, the object will thus shift left, and once this operation has been accomplished,
the next WakeupCriterion will be set to WakeupOnBehaviorPost and a new postID will
be posted to its partner MoveRight.
Search WWH ::




Custom Search