getting mouse location without a listener - java

In some situations I need to place a JFrame right where the mouse cursor is located. Do I really need a mouse listener to track mouse move events, or I can just read current mouse position somehow?

this could help:
MouseInfo.getPointerInfo().getLocation()

Related

How to "Unclick" a JButton without releasing the left mouse button?

I have set up a mouse dragged listener. I trying to set it up where you can click one button then drag your mouse over others to click the other ones. The problem I am having is when you click the first button it turns grey like its waiting for you to release the mouse button. When you move your mouse off the button (still holding the left mouse button) it returns back to its normal color but you cant highlight anything until you let go. Is there anyway to simulated letting the mouse go and "unclicking" the button so you can highlight other things?
What you observe is the typical behavior of the ButtonModel used by Swing buttons. A complete example is examined here, but note how the effect depends on the chosen Look & Feel's ButtonUI delegate.
To get the effect you want, you would have to create buttons using your own variation of BasicButtonUI and a custom ButtonModel that uses isRollover() to add buttons to your program's notion of a selection model.
As an alternative, consider JList, which contains a ListSelectionModel that allows MULTIPLE_INTERVAL_SELECTION. A compete example is shown here.

JavaFX User-drawn lines

How would I let a user draw lines on a canvas in JavaFX? I define a line as where the mouse went over between a click and a release. I'm thinking of using a mouse event handler and a Path, but I was wondering if there's anything built in that I could use. Thanks!
I have a small program that allows the user to draw lines from a tree on the left to a tree on tree on the right.
If the user clicks on an item in the left tree, a line is drawn and the endpoint is bound to the mouse position (a custom handler is registered to monitor for mouse movement and updates two properties that allow you to bind to it). This works great.

Find the mouse position in a JFrame

I'm stuck with the following.
I'm programming a game and I need to get the mouse pointer location inside the JFrame, I don't need to get the pointer location on the screen, but just in the JFrame. When you use the MouseClick event you could get the position in the frame while the mouse button is pressed, but I want to get it's location when no button is pressed.
I hope you understood my question.
Sounds like you need a MouseMotionListener.
The MouseEvent contains the current mouse position.
void mouseMoved(MouseEvent e)
Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.

How would I allow the user to move the view when they move the mouse off screen?

I would implement the view moving, I just need to know if the mouse is offscreen (offwindow) and adjust the offset variables accordingly.
Maybe you can use a MouseListener to listen for the mouseExited event. Then you can use the MouseInfo class to get the current location of the mouse and then reset the location of the window accordingly.
If the mouse is moving too fast, the mouse may still be outside the window after you reset the location so maybe you will need to start a Timer to continually check the MouseInfo to get the current mouse location and then continually adjust the window location. If at any time a mouseEntered event is generated then you can stop the Timer.

How do I lock the mouse inside a JFrame

I would like to lock the mouse inside a JFrame. That is, the mouse can not leave the contents of the JFrame (unless the user hits escape, alt-tab, or the window otherwise looses focus). Ideas?
Thanks!
I'm not sure if there's a more automatic way of doing that, but you could use the Robot class to set the mouse position. So in the event handler for when the JFrame gains focus you can start watching the mouse move event, and when the mouse moves just make sure it stays within the JFrame. If it leaves the JFrame you can use the Robot class to set the mouse's position to go back.
Then when the window loses focus, you can unregister from the mouse move event.
The Robot class is ideal for this type of thing, but I would suggest another approach.
Perhaps making the game full screen (maximizing the window pane) would achieve what you want instead. The mouse would be unable to exit the window and no ugly Robot-esque hack needs to be used to force the user to stay within the borders.
Another workaround I just thought of - lock the cursor to the centre of the Frame, and make it invisible.Then render a software cursor where the real cursor should be.
You can then lock the cursor to whatever area you want.
Here's a sneaky one could work if you don't use mouse button 2 in your game.
Use a Robot to press down BUTTON2.
The idea is so the mouse gets dragged, not moved. Whenever you get a mouse moved event, it's because the user has released button2, so press it down again.
Whenever you get a mouse dragged event, if the mouse is outside the window, put it back in.

Categories