Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] set Mouse/cursor Position

Started by atarifly_zero Apr 28, 2001 at 4:10 AM 1 replies 500+ views
Original Post
atarifly_zero
atarifly_zero
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
AIRmichael
AIRmichael
At http://nehe.gamedev.net theres some tutorial about the function point mpos; for mouse positioning. ( Only the basics). Mess with it
TraxOp
TraxOp
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






Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.