| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1521 人关注过本帖
标题:为什么我上下移动鼠标,物体还是右移,困了我半个月的难题??!!
只看楼主 加入收藏
999hits
Rank: 1
等 级:新手上路
威 望:1
帖 子:227
专家分:0
注 册:2005-10-2
收藏
 问题点数:0 回复次数:7 
为什么我上下移动鼠标,物体还是右移,困了我半个月的难题??!!
//各位一定 给我个答复,困了我半个月了
//先谢过各位了!!
// 程序如下:
//第一个是一个自定义的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));
}
}
搜索更多相关主题的帖子: 鼠标 物体 难题 
2006-10-31 11:44
999hits
Rank: 1
等 级:新手上路
威 望:1
帖 子:227
专家分:0
注 册:2005-10-2
收藏
得分:0 
//第二个是主类,里面有一个控制鼠标位置的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();
}
}

J3D的摸瞎人
2006-10-31 11:45
999hits
Rank: 1
等 级:新手上路
威 望:1
帖 子:227
专家分:0
注 册:2005-10-2
收藏
得分:0 
里面可能有一些用不到的东西,是我修改时候添上去的,大家一定帮我好好看看

J3D的摸瞎人
2006-10-31 11:47
999hits
Rank: 1
等 级:新手上路
威 望:1
帖 子:227
专家分:0
注 册:2005-10-2
收藏
得分:0 
忘了跟大家说一下我的目的了,我的主要目的是物体运动的方向总是根鼠标的运动方向相反,(例:鼠标左上运动,物体右下运动),可是我现在把x+=-dy;变成了x=-dy;也就是说物体不可以上下运动了按理说当我上下移动鼠标的时候物体不会左右移动,可是现在变成了我不管是上,下,左的移动都变成了物体的右移,大家看一下,怎么解决??!!

J3D的摸瞎人
2006-10-31 15:04
999hits
Rank: 1
等 级:新手上路
威 望:1
帖 子:227
专家分:0
注 册:2005-10-2
收藏
得分:0 
就没有人知道吗,郁闷

J3D的摸瞎人
2006-11-01 08:18
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
兄弟,搞JAVA3D的人不多啊

可惜不是你,陪我到最后
2006-11-01 09:47
999hits
Rank: 1
等 级:新手上路
威 望:1
帖 子:227
专家分:0
注 册:2005-10-2
收藏
得分:0 
那谁能帮我改一下啊,我很急 啊

J3D的摸瞎人
2006-11-01 19:48
999hits
Rank: 1
等 级:新手上路
威 望:1
帖 子:227
专家分:0
注 册:2005-10-2
收藏
得分:0 
版主都不会,那我起不是费了

J3D的摸瞎人
2006-11-03 19:34
快速回复:为什么我上下移动鼠标,物体还是右移,困了我半个月的难题??!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016962 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved