为什么我上下移动鼠标,物体还是右移,困了我半个月的难题??!!
//各位一定 给我个答复,困了我半个月了//先谢过各位了!!
// 程序如下:
//第一个是一个自定义的Behavoir,用来控制鼠标:
import java.awt.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.JFrame;
import java.awt.event.*;
import java.util.Enumeration;
public class MyBehavior extends Behavior
{
private TransformGroup targetTG;
private Transform3D rotation;
private Robot robot;
private Point mouseLocation,centerLocation;
private JFrame canvas;
private double x=.0;
private double y=.0;
private double angle=.0;
private double z=1000.0;
int i=0;
public MyBehavior(JFrame canvas,TransformGroup targetTG,Transform3D rotation)
{
super();
this.targetTG=targetTG;
this.rotation=rotation;
this.canvas=canvas;
}
public void initialize()
{
this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_MOVED));
}
public void processStimulus(Enumeration criteria)
{
WakeupCriterion wakeupMouse=null;
AWTEvent[] eventMouse=null;
wakeupMouse=(WakeupCriterion)criteria.nextElement();
if(wakeupMouse instanceof WakeupOnAWTEvent)
{
eventMouse=((WakeupOnAWTEvent)wakeupMouse).getAWTEvent();
MouseEvent mouseevent=(MouseEvent)eventMouse[0];
// if(mouseevent.getX()<canvas.getWidth()&&mouseevent.getY()<canvas.getHeight())
// {
double ddx=((double)mouseevent.getX()-(double)canvas.getWidth()/2);
double dy=((double)mouseevent.getY()-(double)canvas.getHeight()/2);
x=-dy;
y+=-ddx;
angle=Math.atan((Math.sqrt(x*x+y*y))/z);
rotation.setRotation(new AxisAngle4d(x,y,0.0,angle));
targetTG.setTransform(rotation);
i+=1;
System.out.println(x+" "+y+" "+angle+" "+i);
// }
}
this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_MOVED));
}
}