当前位置: 代码迷 >> Java相关 >> 为什么我上下移动鼠标,物体还是右移,困了我半个月的难题??!!
  详细解决方案

为什么我上下移动鼠标,物体还是右移,困了我半个月的难题??!!

热度:256   发布时间:2006-10-31 11:44:45.0
为什么我上下移动鼠标,物体还是右移,困了我半个月的难题??!!
//各位一定 给我个答复,困了我半个月了
//先谢过各位了!!
// 程序如下:
//第一个是一个自定义的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));
}
}
搜索更多相关的解决方案: 鼠标  物体  难题  

----------------解决方案--------------------------------------------------------
//第二个是主类,里面有一个控制鼠标位置的Robot:
import java.awt.*;
import java.awt.event.*;
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 javax.swing.JFrame;
import java.util.Enumeration;
public class behaviorTest extends JFrame implements Runnable
{
private SimpleUniverse su;
private BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,0.,.0),100.0);
private BranchGroup sceneBG;
private Transform3D t3d;
private Canvas3D canvas3D;

private Robot robot;
private Point centerLocation;
private Point mouseLocation;

private boolean isRecenter=true;

public void createSceneGraph()
{
sceneBG=new BranchGroup();

Background bg=new Background(new Color3f(0.f,1.f,1.f));
bg.setApplicationBounds(bounds);
sceneBG.addChild(bg);

TransformGroup objRotate=new TransformGroup();
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.addChild(new ColorCube(.2));
sceneBG.addChild(objRotate);

addViewControl();

sceneBG.compile();
}
private void addViewControl()
{
Transform3D t3d=new Transform3D();
t3d.setTranslation(new Vector3f(0.f,0.f,2.41f));

ViewingPlatform vp=su.getViewingPlatform();
TransformGroup viewerTG=vp.getViewPlatformTransform();
viewerTG.setTransform(t3d);

MyBehavior be=new MyBehavior(this,viewerTG,t3d);
be.setSchedulingBounds(new BoundingSphere());
sceneBG.addChild(be);
}
private void recenterLocation()
{
centerLocation.x=this.getWidth()/2;
centerLocation.y=this.getHeight()/2;
robot.mouseMove(centerLocation.x,centerLocation.y);
}
public void init()
{
mouseLocation=new Point();
centerLocation=new Point();
try
{
robot=new Robot();
recenterLocation();
}
catch(Exception ex)
{
System.out.println("no");
}
}
public behaviorTest()
{
setSize(400,300);
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
canvas3D = new Canvas3D(config);
add("Center", canvas3D);

canvas3D.setFocusable(true);
canvas3D.requestFocus();
canvas3D.addKeyListener(
new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
{
isRecenter=false;
System.exit(1);
}
}
});
su=new SimpleUniverse(canvas3D);
createSceneGraph();
su.addBranchGraph(sceneBG);
init();
new Thread(this).start();
}
public void run()
{
while(isRecenter)
{
recenterLocation();
try
{
Thread.sleep(10);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
public static void main(String arg[])
{
behaviorTest bt=new behaviorTest();
bt.setVisible(true);
bt.init();
}
}

----------------解决方案--------------------------------------------------------
里面可能有一些用不到的东西,是我修改时候添上去的,大家一定帮我好好看看
----------------解决方案--------------------------------------------------------
忘了跟大家说一下我的目的了,我的主要目的是物体运动的方向总是根鼠标的运动方向相反,(例:鼠标左上运动,物体右下运动),可是我现在把x+=-dy;变成了x=-dy;也就是说物体不可以上下运动了按理说当我上下移动鼠标的时候物体不会左右移动,可是现在变成了我不管是上,下,左的移动都变成了物体的右移,大家看一下,怎么解决??!!
----------------解决方案--------------------------------------------------------
就没有人知道吗,郁闷
----------------解决方案--------------------------------------------------------
兄弟,搞JAVA3D的人不多啊
----------------解决方案--------------------------------------------------------
那谁能帮我改一下啊,我很急 啊

----------------解决方案--------------------------------------------------------
版主都不会,那我起不是费了
----------------解决方案--------------------------------------------------------
  相关解决方案