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
|
import java.util.Enumeration;
import java.awt.event.KeyEvent;
import java.awt.AWTEvent;
import javax.vecmath.Point3d;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Behavior;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.WakeupOr;
import javax.media.j3d.WakeupOnElapsedTime;
import javax.media.j3d.WakeupOnAWTEvent;
/** Tache de fond applicable a un objet KeyPressedControlable. */
public class KeyBehavior extends Behavior
{
private WakeupCriterion[] wakeup;
private WakeupCondition wakeupCombi;
private Controlable ctrl;
private boolean fixed = true;
public KeyBehavior (Controlable ctrl)
{
wakeup = new WakeupCriterion[2];
wakeup[0] = new WakeupOnAWTEvent (KeyEvent.KEY_PRESSED );
wakeup[1] = new WakeupOnElapsedTime (50L); // minuterie
wakeupCombi = new WakeupOr (wakeup);
this.ctrl = ctrl;
setSchedulingBounds (new BoundingSphere (new Point3d (), 300.0));
}
public void initialize()
{
this.wakeupOn (wakeup[0]);
}
public void processStimulus(Enumeration Criteria)
{
AWTEvent[] tabEvent = ( (WakeupOnAWTEvent) wakeup[0]).getAWTEvent();
AWTEvent event;
for (int i = 0; i < tabEvent.length; i++)
{
event = tabEvent[i];
switch (((KeyEvent) event).getKeyCode())
{
case KeyEvent.VK_UP :
System.out.println("UP");
if (fixed = !fixed) this.wakeupOn (wakeup[0]);
else this.wakeupOn (wakeupCombi);
if (ctrl.incrementer ()) this.wakeupOn (wakeupCombi);
else
{
this.wakeupOn (wakeup[0]);
fixed = true;
}
break;
case KeyEvent.VK_DOWN :
System.out.println("DOWN");
if (fixed = !fixed) this.wakeupOn (wakeup[0]);
else this.wakeupOn (wakeupCombi);
if (ctrl.incrementer ()) this.wakeupOn (wakeupCombi);
else
{
this.wakeupOn (wakeup[0]);
fixed = true;
}
break;
case KeyEvent.VK_LEFT :
System.out.println("LEFT");
if (fixed = !fixed) this.wakeupOn (wakeup[0]);
else this.wakeupOn (wakeupCombi);
if (ctrl.incrementer ()) this.wakeupOn (wakeupCombi);
else
{
this.wakeupOn (wakeup[0]);
fixed = true;
}
break;
case KeyEvent.VK_RIGHT :
System.out.println("RIGHT");
if (fixed = !fixed) this.wakeupOn (wakeup[0]);
else this.wakeupOn (wakeupCombi);
if (ctrl.incrementer ()) this.wakeupOn (wakeupCombi);
else
{
this.wakeupOn (wakeup[0]);
fixed = true;
}
break;
}
// System.out.println(" fin switch");
}wakeupOn(wakeupCombi);
}
} |
Partager