View Javadoc
1 2 package camera3d; 3 4 /*** 5 * Title: Câmera Virtual - LIVES 6 * Description: Câmera Virtual para Controle via LabVIEW 7 * Company: Centro de Educação em Informática - SENAC - SP 8 * @author Fábio Roberto de Miranda 9 * @version 1.0 10 * 11 * This class is derived from Sun's com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior. 12 * Instead of using a KeyNavigator, this class uses a VcKeyNavigator, and instead of 13 * calling KeyNavigator.processKeyEvent(...) , it uses VcKeyNavigator.preProcessKeyEvent(...) 14 * 15 */ 16 17 18 19 //--------------- 20 /* 21 * @(#)KeyNavigatorBehavior.java 1.9 01/01/11 07:22:12 22 * 23 * Copyright (c) 1996-2001 Sun Microsystems, Inc. All Rights Reserved. 24 * 25 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, 26 * modify and redistribute this software in source and binary code form, 27 * provided that i) this copyright notice and license appear on all copies of 28 * the software; and ii) Licensee does not utilize the software in a manner 29 * which is disparaging to Sun. 30 * 31 * This software is provided "AS IS," without a warranty of any kind. ALL 32 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY 33 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR 34 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE 35 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING 36 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS 37 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 38 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 39 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF 40 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 41 * POSSIBILITY OF SUCH DAMAGES. 42 * 43 * This software is not designed or intended for use in on-line control of 44 * aircraft, air traffic, aircraft navigation or aircraft communications; or in 45 * the design, construction, operation or maintenance of any nuclear 46 * facility. Licensee represents and warrants that it will not use or 47 * redistribute the Software for such purposes. 48 */ 49 50 51 import java.awt.event.*; 52 import java.awt.AWTEvent; 53 import java.util.Enumeration; 54 import java.awt.Component; 55 import java.util.LinkedList; 56 import javax.vecmath.*; 57 import javax.media.j3d.*; 58 import com.sun.j3d.internal.J3dUtilsI18N; 59 import com.sun.j3d.utils.behaviors.keyboard.*; 60 61 /*** 62 * This class is a simple behavior that invokes the KeyNavigator 63 * to modify the view platform transform. 64 */ 65 class VcKeyNavigatorBehavior extends Behavior implements KeyListener { 66 private static int instanceCounter = 0; 67 private int instance = -1; 68 private WakeupCriterion w1 = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED); 69 private WakeupCriterion w2 = new WakeupOnAWTEvent(KeyEvent.KEY_RELEASED); 70 private WakeupOnElapsedFrames w3 = new WakeupOnElapsedFrames(0); 71 private WakeupCriterion[] warray = { w1, w2, w3 }; 72 private WakeupCondition w = new WakeupOr(warray); 73 74 private KeyEvent eventKey; 75 private VcKeyNavigator keyNavigator; 76 private boolean listener = false; 77 78 // tells whether we should update the VcView Transform; 79 // depends on mouse cursor position: in/outside the viewport frame 80 // This was removed because of a bug (in Java3D ?) which made the 81 // canvas3D disappear 82 //private boolean updating = false; 83 84 private LinkedList eventq; 85 Canvas3D[] sourcesC3D = new Canvas3D[5]; 86 87 /* 88 void setUpdating(boolean updating){ 89 this.updating = updating; 90 System.out.println("updating = " +updating); 91 } 92 */ 93 94 /*** 95 * Override Behavior's initialize method to setup wakeup criteria. 96 */ 97 public void initialize() { 98 // Establish initial wakeup criteria 99 if (listener) { 100 w1 = new WakeupOnBehaviorPost(this, KeyEvent.KEY_PRESSED); 101 w2 = new WakeupOnBehaviorPost(this, KeyEvent.KEY_RELEASED); 102 warray[0] = w1; 103 warray[1] = w2; 104 w = new WakeupOr(warray); 105 eventq = new LinkedList(); 106 } 107 wakeupOn(w); 108 } 109 110 /*** 111 * Override Behavior's stimulus method to handle the event. 112 */ 113 public void processStimulus(Enumeration criteria) { 114 WakeupOnAWTEvent ev; 115 WakeupCriterion genericEvt; 116 AWTEvent[] events; 117 boolean sawFrame = false; 118 119 while (criteria.hasMoreElements()) { 120 genericEvt = (WakeupCriterion) criteria.nextElement(); 121 if (genericEvt instanceof WakeupOnAWTEvent) { 122 ev = (WakeupOnAWTEvent) genericEvt; 123 events = ev.getAWTEvent(); 124 processAWTEvent(events); 125 } else if ((genericEvt instanceof WakeupOnElapsedFrames) && 126 eventKey != null) { 127 /* notice that this section will be executed every frame after the 128 first keystroke, since eventKey is never set to null again */ 129 sawFrame = true; 130 } else if ((genericEvt instanceof WakeupOnBehaviorPost)) { 131 while(true) { 132 // access to the queue must be synchronized 133 synchronized (eventq) { 134 if (eventq.isEmpty()) break; 135 eventKey = (KeyEvent)eventq.remove(0); 136 if ((isFromProperCanvas(eventKey))){ //Only lets certain events through 137 if (eventKey.getID() == KeyEvent.KEY_PRESSED || 138 eventKey.getID() == KeyEvent.KEY_RELEASED) { 139 keyNavigator.processKeyEvent(eventKey); 140 } 141 } 142 } 143 } 144 } 145 } 146 //if (sawFrame&&updating){ 147 if(sawFrame){ 148 keyNavigator.integrateTransformChanges(); 149 //keyNavigator.updateTime(); 150 sawFrame = false; 151 //eventKey = null; 152 } 153 154 // Set wakeup criteria for next time 155 wakeupOn(w); 156 } 157 158 /*** 159 * Process a keyboard event 160 */ 161 private void processAWTEvent(AWTEvent[] events) { 162 for (int loop = 0; loop < events.length; loop++) { 163 if (events[loop] instanceof KeyEvent) { 164 eventKey = (KeyEvent) events[loop]; 165 if(isFromProperCanvas(eventKey)){ 166 // change the transformation; for example to zoom 167 if (eventKey.getID() == KeyEvent.KEY_PRESSED || 168 eventKey.getID() == KeyEvent.KEY_RELEASED) { 169 keyNavigator.processKeyEvent(eventKey); 170 } 171 } 172 } 173 } 174 } 175 176 /*** 177 * Adds this behavior as a KeyListener to the specified component. 178 * This method can only be called if 179 * the behavior was created with one of the constructors that takes 180 * a Component as a parameter. 181 * @param c The component to add the KeyListener to. 182 * @exception IllegalStateException if the behavior was not created 183 * as a listener 184 * @since Java 3D 1.2.1 185 */ 186 public void addListener(Component c) { 187 if (!listener) { 188 throw new IllegalStateException(J3dUtilsI18N.getString("Behavior0")); 189 } 190 c.addKeyListener(this); 191 } 192 193 194 public void removeListener(Component c) { 195 if (!listener) { 196 throw new IllegalStateException(J3dUtilsI18N.getString("Behavior0")); 197 } 198 c.removeKeyListener(this); 199 } 200 201 /*** 202 * Constructs a new key navigator behavior node that operates 203 * on the specified VcView. 204 * @param targetView the target view 205 */ 206 public VcKeyNavigatorBehavior(VcView targetView) { 207 instance = instanceCounter++; 208 keyNavigator = new VcKeyNavigator(targetView); 209 } 210 211 /*** 212 * Constructs a key navigator behavior that uses AWT listeners 213 * and behavior posts rather than WakeupOnAWTEvent. The behavior 214 * is added to the specified Component and works on the given 215 * TransformGroup. A null component can be passed to specify 216 * the behavior should use listeners. Components can then be added 217 * to the behavior with the addListener(Component c) method. 218 * @param c The component to add the VcKeyListener to. 219 * @param targetView The target VcView. 220 * @since Java 3D 1.2.1 221 */ 222 public VcKeyNavigatorBehavior(Component c, VcView targetView) { 223 this(targetView); 224 if (c != null) { 225 c.addKeyListener(this); 226 } 227 listener = true; 228 } 229 230 public void keyPressed(KeyEvent evt) { 231 // add new event to the queue 232 // must be MT safe 233 synchronized (eventq) { 234 eventq.add(evt); 235 // only need to post if this is the only event in the queue 236 if (eventq.size() == 1) postId(KeyEvent.KEY_PRESSED); 237 } 238 } 239 240 public void keyReleased(KeyEvent evt) { 241 // add new event to the queue 242 // must be MT safe 243 synchronized (eventq) { 244 eventq.add(evt); 245 // only need to post if this is the only event in the queue 246 if (eventq.size() == 1) postId(KeyEvent.KEY_RELEASED); 247 } 248 } 249 250 public void keyTyped(KeyEvent evt) {} 251 252 /* New methods*/ 253 public void removeCanvas3D(Canvas3D canvas){ 254 for (int i=0; i< sourcesC3D.length; i++){ 255 if (sourcesC3D[i]==canvas){ 256 sourcesC3D[i] = null; 257 if(listener) removeListener(canvas); 258 } 259 } 260 } 261 262 public void removeAllCanvas3Ds(){ 263 for (int i=0; i< sourcesC3D.length; i++){ 264 if(listener) removeListener(sourcesC3D[i]); 265 sourcesC3D[i] = null; 266 } 267 } 268 269 public void addCanvas3D(Canvas3D canvas){ 270 for (int i=0; i< sourcesC3D.length; i++){ 271 if (sourcesC3D[i]==null){ 272 sourcesC3D[i] = canvas; 273 return; 274 } 275 } 276 } 277 278 boolean isFromProperCanvas(KeyEvent event){ 279 Object obj = event.getSource(); 280 /* 281 System.out.println("VcKeyNavBeh:"+ this.toString()+"/ sourcesC3D.length= "+sourcesC3D.length); 282 for (int i=0; i< sourcesC3D.length; i++){ 283 if(sourcesC3D[i]!=null)System.out.println("VcKey...: sourcesC3D["+i+"]= "+sourcesC3D[i]); 284 else System.out.println("VcKey...: sourcesC3D["+i+"]= null"); 285 } */ 286 for (int i=0; i< sourcesC3D.length; i++){ 287 if ((Object)sourcesC3D[i]==obj){ 288 //System.out.println("VcKey...:I found that "+sourcesC3D[i]+" and "+obj+" are alike"); 289 return true; 290 } 291 } 292 //System.out.println("VcKey...: "+obj+" is NOT a sourceC3D"); 293 return false; 294 } 295 296 }

This page was automatically generated by Maven