[java] set Mouse/cursor Position
hi there,
well, i translated a part of my C++ 3D game engine to java with a OpenGL lib
called by the JNI, but now i stuck, cause i got i problem with the mouse
navigation.
cause it''s some kind of an ego-shooter i need to spin left by mouving the
mouse to the left - of cause the same with right =) - but therefore i need
to position the cursor at the center of my Canvas/Frame...
but i can''t find a function doing this =(
a also posted it a newsgroup, where someone told me about the Robot class and its mouseMove() method to reposition the cursor, but this probably won''t work under X-Window...
greets zero
At http://nehe.gamedev.net theres some tutorial about the function point mpos; for mouse positioning. ( Only the basics). Mess with it
![](wink.gif)
Since you are rendering in a Canva/Frame, meaningly a component object, all you need to do to get the mouse position is to add a MouseMotionListener to your class to pick up mouse motions
import java.awt.event.*;
public class xyz {
public xyz() {};
// using inner classes and an adapter
private void setMouseListenerFunction() {
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent ev) {
//do your stuff here, look at the MouseEvent
//class for more info.
System.out.println("posX is " + ev.getX() + " posY " +
ev.getY());
//other things you can do... etc ...
Point xyPoint = ev.getPoint();
// etc ....
int clickNumTime = ev.getClickCount();
//etc ...
}
});
}
public static void main(String[] args) {
//blah blah
xyz newXYZ = new xyz();
newXYZ.setMouseListenerFunction();
//blah blah
}
}
Note that I''m using inner classes and mouseAdapter to make this more compact, if you want more information, go to Sun''s Java site and find their tutorials, it''s complete and free.
hope this helps
import java.awt.event.*;
public class xyz {
public xyz() {};
// using inner classes and an adapter
private void setMouseListenerFunction() {
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent ev) {
//do your stuff here, look at the MouseEvent
//class for more info.
System.out.println("posX is " + ev.getX() + " posY " +
ev.getY());
//other things you can do... etc ...
Point xyPoint = ev.getPoint();
// etc ....
int clickNumTime = ev.getClickCount();
//etc ...
}
});
}
public static void main(String[] args) {
//blah blah
xyz newXYZ = new xyz();
newXYZ.setMouseListenerFunction();
//blah blah
}
}
Note that I''m using inner classes and mouseAdapter to make this more compact, if you want more information, go to Sun''s Java site and find their tutorials, it''s complete and free.
hope this helps
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement