Robot.mouseMove and Frame Decoration

Ok, I have a JOGL scene (and no, the problem isn’t about JOGL).

I was implementing a mouse look system where one holds the right mouse button and drags to move the direction of the camera around. I wanted to keep the mouse cursor anchored in one position while this is done so the camera doesn’t stop rotating when the mouse cursor hits the edge of the screen. Therefore, at the end of the drag calculations, I use Robot.mouseMove to move the mouse cursor back to where it originally was.

The problem is that I can’t get the exact coordinates because I can’t tell work out the offset generated by the frame’s decoration (title bar, minimise, maximise and close buttons). This is throwing my camera rotation way off and driving me nuts. I really don’t want to hard code it in as this will just cause problems in future and for other people/systems. How does one find out this offset. Turning off the decoration is out of the question.

If I am going in the totally wrong direction and theres a better way of sensing the mouse drag, don’t hesitate to correct me :slight_smile:

Thank in advance,
Jargon

JFrame, and indeed any component that extends java.awt.Container, has a getInsets() method that may be what you need.

It sounds like your MouseMotionListener is added to the top-level Frame or JFrame, if you add it instead to the GLCanvas you don’t need to worry about any inset shenanigans.

The MouseMotionListener is attached to the GLCanvas, the problem lies in the Robot class itself, its coordinates work from the top left of the actual JFrame, not the GLCanvas, which gives me the offset problem.

Anyway, despite that, the getInsets() method is EXACTLY what I was looking for :smiley: Thank you very much!

Is your window always maximised or in the top-left corner? Robot.mouseMove() coordinates are relative to the screen and not any particular component.

I’ve looked back to how I did this and found the perfectly suited java.awt.Component.getLocationOnScreen() method.