# Beispielprogram funktioniert nicht



## Dominicana (11. Jun 2006)

Schaue mir zur Zeit das Java3D-Tutorial von Michael Pfeiffer an. Stecke jetzt bei einem Beispiel mit einem KeyEvent fest. Scheinabr reagiert es nicht auf Eingaben. Normalerweise sollte man sich mit den Pfeiltasten drehen und bewegen können.
Der Code ist Original vom Autor des Tutorials , Package habe ich bei mir eingebaut.

```
/*
 * Copyright (c) 2004 Virtual Worlds Productions / Michael Pfeiffer
 * Email: [email]virtual_worlds@gmx.de[/email]
 *
 * This program is free software; you can redistribute it and/or modify  it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 *
 * Please contact [email]virtual_worlds@gmx.de[/email] if you are interested in a source
 * package which is not licensed under the GPL
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. VIRTUAL WORLDS PRODUCTIONS AND ITS
 * LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
 * RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES.
 * IN NO EVENT WILL VIRTUAL WORLDS PRODUCTIONS OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS
 * OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE
 * SOFTWARE, EVEN IF VIRTUAL WORLDS PRODUCTIONS HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Universe extends Applet
   {
   private SimpleUniverse u = null;

   public Universe()
      {
      }

   public BranchGroup createSceneGraph()
      {
      BranchGroup          RootBG=new BranchGroup();
      Appearance           ConeAppearance=new Appearance();
      DirectionalLight     DLgt=new DirectionalLight(new Color3f(0.8f,0.8f,0.8f),new Vector3f(-0.5f,-1f,-0.5f));
      AmbientLight         ALgt=new AmbientLight(new Color3f(0.8f,0.8f,0.8f));
      BoundingSphere       BigBounds=new BoundingSphere(new Point3d(),100000);

      ALgt.setInfluencingBounds(BigBounds);
      DLgt.setInfluencingBounds(BigBounds);
      RootBG.addChild(ALgt);
      RootBG.addChild(DLgt);
      ConeAppearance.setMaterial(new Material(new Color3f(0.9f,0.5f,0.5f),new Color3f(0f,0f,0f),new Color3f
                                             (0.3f,0.7f,0.7f),new Color3f(0.8f,0.8f,0.8f),1f));
      RootBG.addChild(new Cone(0.5f,1.5f,Cone.GENERATE_NORMALS,40,1,ConeAppearance));

      KeyBehavior KeyCtrl=new KeyBehavior(u.getViewingPlatform().getViewPlatformTransform());
      KeyCtrl.setSchedulingBounds(BigBounds);
      RootBG.addChild(KeyCtrl);

      RootBG.compile();
      return RootBG;
      }



   void setViewPosition()
      {
      TransformGroup ViewTG;
      Transform3D    ViewT3D=new Transform3D();

      ViewTG=u.getViewingPlatform().getViewPlatformTransform();
      ViewTG.getTransform(ViewT3D);
      ViewT3D.setTranslation(new Vector3f(0f,0f,4f));
      ViewTG.setTransform(ViewT3D);
      }



   public void init()
      {
      setLayout(new BorderLayout());
      GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
      Canvas3D c = new Canvas3D(config);
      add("Center", c);
      u = new SimpleUniverse(c);

      setViewPosition();
      u.addBranchGraph(createSceneGraph());
      }



   public void destroy()
      {
      u.cleanup();
      }

   public static void main(String[] args)
      {
      new MainFrame(new Universe(), 400, 400);
      }





}
```


```
/*
 * Copyright (c) 2004 Virtual Worlds Productions / Michael Pfeiffer
 * Email: [email]virtual_worlds@gmx.de[/email]
 *
 * This program is free software; you can redistribute it and/or modify  it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 *
 * Please contact [email]virtual_worlds@gmx.de[/email] if you are interested in a source
 * package which is not licensed under the GPL
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. VIRTUAL WORLDS PRODUCTIONS AND ITS
 * LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
 * RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES.
 * IN NO EVENT WILL VIRTUAL WORLDS PRODUCTIONS OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS
 * OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE
 * SOFTWARE, EVEN IF VIRTUAL WORLDS PRODUCTIONS HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.*;

public class KeyBehavior extends Behavior
   {
   private TransformGroup transformGroup;
   private Transform3D trans = new Transform3D(),tempTrans=new Transform3D();
   private WakeupCriterion criterion;

   public KeyBehavior(TransformGroup tg)
      {
      transformGroup=tg;
      }

   public void initialize()
      {
      criterion=new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
      wakeupOn(criterion);
      }

   public void processStimulus(Enumeration criteria)
      {
      WakeupOnAWTEvent ev;
      AWTEvent         AWTEv[];
      KeyEvent         KeyEv;

      while (criteria.hasMoreElements())
         {
         ev=(WakeupOnAWTEvent)criteria.nextElement();
         AWTEv=ev.getAWTEvent();
         for (int i=0; i<AWTEv.length; i++)
            {
            KeyEv=(KeyEvent)AWTEv[i];
            transformGroup.getTransform(trans);
            tempTrans.setIdentity();
            if (KeyEv.getKeyCode()==KeyEvent.VK_UP) tempTrans.setTranslation(new Vector3f(0f,0f,-0.2f));
            else if (KeyEv.getKeyCode()==KeyEvent.VK_DOWN) tempTrans.setTranslation(new Vector3f(0f,0f,0.2f));
            else if (KeyEv.getKeyCode()==KeyEvent.VK_LEFT) tempTrans.rotY(Math.toRadians(2));
            else if (KeyEv.getKeyCode()==KeyEvent.VK_RIGHT) tempTrans.rotY(Math.toRadians(-2));
            trans.mul(tempTrans);
            transformGroup.setTransform(trans);
            }
         }
      wakeupOn(criterion);
      }

}
```
Kann mir jemand sagen, ob´s an mir liegt (Tastatur), oder am obigen Code?

Dom.


----------



## Illuvatar (11. Jun 2006)

Bei mir funktioniert der Code gut, ich kann auch keinen Fehler darin entdecken.


----------



## Dominicana (11. Jun 2006)

Thx @ Illuvatar für´s testen.
Dann ist diese sch...  Microsoft-Tastatur dran Schuld! Ich hab´s doch gewußt! Hätte ich mir doch lieber eine von Logitech geholt!


----------



## Illuvatar (11. Jun 2006)

Also ich hab Logitech 

Aber eigentlich kann das nicht sein... testest du wirklich exakt den Code da oben? Startest du das über die main, im Appletviewer oder im Browser? Hast du schonmal gedebuggt und in der processStimulus einen Breakpoint gesetzt? Funktionieren irgendwelche auf DirectX (bzw. OpenGL, je nach dem was du für J3D verwendest) basierenden Spiele mit der Tastatur?


----------



## Dominicana (11. Jun 2006)

Also ich nutze obigen Code , bis auf wenige Änderungen (z.B. Variablenname in kleinschrift). Das Applet an sich kann ich sehen - weißer Kegel auf schwarzem Hintergrund - aber ich kann Tasten(Pfeil hoch, runter rechts links) drücken, wie ich will , es tut sich nichts.


----------



## Dominicana (11. Jun 2006)

Hab den Fehler jetzt gefunden : ich muß erst mit der Maus ins Applet klicken. Ist das Normal?


----------



## Wildcard (11. Jun 2006)

Versuch mal ein requestFocus() auf der Komponente die die Events erhalten soll.


----------



## Dominicana (12. Jun 2006)

Macht keinen Unterschied.  ???:L Was solls, irgendwann werd ich noch eine Lösung dafür finden.


----------



## The_S (12. Jun 2006)

dann teste ein requestFocusInWindow().

Funktioniert beim IE, beim Firefox aber nicht. Opera weiß ich nicht mehr auswendig.


----------

