开发者

Java3D and Behaviours : KeyNavigatorBehaviour works fine, but not MouseRotate

I don't manage to give user mouse interaction to a ColorCube by using a MouseRotate. However, when i use a KeyNavigatorBehaviour, i can control the cube with keyboard as needed.

Here the code i used to test MouseRotate :

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.GraphicsConfigTemplate3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.vecmath.Point3d;

import com.sun.j3d.exp.swing.JCanvas3D;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;


public class MovingAroundCube extends JFrame {

    private static final long serialVersionUID = 1L;

    public MovingAroundCube(){
        setTitle("Moving around cube");
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JCanvas3D jCanvas3D = new JCanvas3D(new GraphicsConfigTemplate3D());
        jCanvas3D.setSize(300, 300);
        add(jCanvas3D);
        SimpleUniverse universe = new SimpleUniverse(jCanvas3D.getOffscreenCanvas3D());
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(createSceneGraph());
    }

    public BranchGroup createSceneGraph() {
        BranchGroup objRoot = new BranchGroup();

        TransformGroup listenerGroup = new TransformGroup();
        listenerGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        listenerGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        objRoot.addChild(listenerGroup);

        //KeyNavigatorBehavior behaviour = new KeyNavigatorBehavior(listenerGroup);
        MouseRotate behaviour = new MouseRotate(listenerGroup);
        behaviour.setSchedulingBounds(new BoundingSphere(new Point3d(), 100));

        listenerGroup.addChild(behaviour);
        listenerGroup.addChild(new ColorCube(0.4));

        return objRoot;
    }

    public static void main(String[] args) {
        new MovingAroundCube().setVisible(true);
    }


}

If I uncomment the line creating the KeyNavigatorBehaviour and comment the line creating the MouseRotate, user interaction this time is possible .

So, why can't the cube react to the mouse (when i use MouseRotate behaviour instance) ?

Any hel开发者_JS百科p will be appreciated.

System : Xubuntu 11.04 Java3D version : 1.5.2


There are two ways to solve this dilemma:

  • Use this constructor:

    MouseRotate behaviour = new MouseRotate(jCanvas3D, listenerGroup);

or

  • Enable mouse events as long as no MouseListeners are added:

    import java.awt.AWTEvent;

    JCanvas3D jCanvas3D = new JCanvas3D(new GraphicsConfigTemplate3D()) {
        {
            this.enableEvents(AWTEvent.MOUSE_EVENT_MASK | 
                              AWTEvent.MOUSE_MOTION_EVENT_MASK |
                              AWTEvent.MOUSE_WHEEL_EVENT_MASK);
        }
    };

Key events are enabled because 'setFocusable(true)' is set in JCanvas3D.

August, InteractiveMesh

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜