Mouse Re-centering in java

933 views Asked by At

I'm writing a very basic first person game in java, and one problem I've been running into for a while was the mouse motion(eg, the cursor going out of the frame), so I decided to try and figure out how to use the awt.Robot class to recenter the mouse every time after it was moved. However, I ran into a problem that I'm getting really annoyed with and I feel like it should be really easy to fix: the mouse cursor either doesn't move or doesn't move long enough for the program to register it. This is my last attempt at it which is based on an example from a tutorial

public void mouseMoved(MouseEvent e){ 
            MouseX = e.getX(); 
            MouseY = e.getY();
            try{
                Robot robot = new Robot();
                robot.mouseMove((Frame.newX - Frame.oldX) + Frame.WIDTH / 2, (Frame.newY - Frame.oldY) + Frame.HEIGHT / 2);

            }catch(Exception ex){
                ex.printStackTrace();
            }
        } 

If it helps any, a good example of what I want to do is like in Minecraft, or actually almost any other first person game, where you can keep moving the mouse infinitely and the cursor stays in the middle of the frame.

Edit:

just realized this might be a bit confusing if I didn't tell you, Frame is the name of a separate class in the project, NOT awt.Frame, although some of you probably already figured that out from the way I used it.

0

There are 0 answers