按键旋转问题??!!
大家运行一下下面的程序(按键A为左旋,按键D为右旋):import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.AWTEvent;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;
import java.util.Enumeration;
public class behaviorTest extends Applet
{
public class MyBehavior extends Behavior
{
private TransformGroup targetTG;
private Transform3D rotation=new Transform3D();
private double angle=0.0;
public MyBehavior(TransformGroup targetTG)
{
this.targetTG=targetTG;
}
public void initialize()
{
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
public void processStimulus(Enumeration criteria)
{
WakeupCriterion wakeup=null;
AWTEvent[] event=null;
wakeup=(WakeupCriterion)criteria.nextElement();
if(wakeup instanceof WakeupOnAWTEvent)
{
event=((WakeupOnAWTEvent)wakeup).getAWTEvent();
KeyEvent keyevent=(KeyEvent)event[0];
if(keyevent.getKeyCode()==KeyEvent.VK_A)
{
angle-=0.1;
rotation.rotY(angle);
targetTG.setTransform(rotation);
}
if(keyevent.getKeyCode()==KeyEvent.VK_D)
{
angle+=0.1;
rotation.rotY(angle);
targetTG.setTransform(rotation);
}
}
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
}
public BranchGroup createSceneGraph()
{
BranchGroup objroot=new BranchGroup();
TransformGroup objRotate=new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objroot.addChild(objRotate);
objRotate.addChild(new ColorCube(.4));
MyBehavior b=new MyBehavior(objRotate);
b.setSchedulingBounds(new BoundingSphere());
objroot.addChild(b);
objroot.compile();
return objroot;
}
public behaviorTest(){
setLayout(new BorderLayout());
Canvas3D c=new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add("Center",c);
BranchGroup scene=createSceneGraph();
SimpleUniverse u=new SimpleUniverse(c);
u.addBranchGraph(scene);
u.getViewingPlatform().setNominalViewingTransform();
}
public static void main(String arg[])
{
new MainFrame(new behaviorTest(),400,300);
}
}
大家一定会发现一个问题,就是按住A或D的时候,物体会先旋转0.1个弧度,然后停一下,之后才能持续旋转,问题出来了,
大家有没有什么办法让我们按住A或D的时候,物体不用停一下就直接旋转??!!
这个程序是Java3D的,要安装java3d-1_4_0_01-windows-i586.exe