How to find location of JTree node on screen - java

I want to search a particular node of JTree and determine its location on screen. My aim is to start hovering mouse from the top left corner of that node and perform a mouse click in my JFC.
Can anyone help me with this? Is it possible?

Knowing the coordinates x and y (from the mouse event), you can get the path via JTree.getPathForLocation. The bounds of the node are returned by JTree.getPathBounds(path).

Without knowing more information, it's hard to nail this down.
However, I'd suggest you take a look at
JTree.getPathBounds
JTree.getRowBounds
JTree.getRowForPath
SwingUtilities.convertPointToScreen
This should help you work out where on the screen a node is

You can use processMouseEvent. And the MouseEvent will get you the X and Y coordinates of your mouse.

Related

JavaFX rounded corners hitbox

In my program I have styled my buttons using css. I am using "-fx-background-radius" to round the corners and have noticed that when I hover over where the corners used to be it allows me to click the button. I was just wondering if there is a way to make it so that the 'hit-box' for the button matches what you see.
Thank you.
The pickOnBounds property
Defines how the picking computation is done for this node when triggered by a MouseEvent or a contains function call. If pickOnBounds is true, then picking is computed by intersecting with the bounds of this node, else picking is computed by intersecting with the geometric shape of this node.
The "bounds" are essentially the rectangular region containing the node (button in this case). The "geometric shape" accounts for how the node is actually rendered.
So you just need to call setPickOnBounds(false) on the button.

Shape Rotation Issue - Java2d

I am developing one simple game in which i have encountered one small but important issue.
I have implemented absolute rotation in my logic.
When i start rotating an object when the object does not have any rotation , it works fine and i can rotate as in any direction without any problem as shown in the following link.
Initial Rotation Video
Now the problem arise when the object does have some rotation , and why i try to rotate in one of the direction , instead of being rotated in desire direction the rotation always starts from initial rotation as shown in the following link.
Rotation issue when shape has some rotation
I think the video shows everything , still if you have any questions please ask me.
I think the problem is , there should be a relative rotation in the direction of mouse pointer from whatever circle is selected .
Now about My Logic,
in mouse press event i just checked
Mouse Press
Whether the shape is selected on the canvas , if yes
if one of the four circles contains mouse point if yes
then initiateRotation
Mouse Drag
Using Vector Maths
I update the motion according to mouse points ,
calculate rotation angle according to the following method
Math.atan2(rotationVector.getY(), rotationVector.getX());
and apply rotation on this shape.
Rotation Vector i get from this class
Vector Rotation
I called above class startMotion in mouse press and updateMotion in mouse drag event.
What am i missing or doing anything wrong ?
We need to see some code to be able to help you out. It looks like you reset the rotation, whenever you initiateRotation, and then the object quickly rotates in place, according to your mouse position when you drag.

SWT DragSourceEvent image not anchored to mouse pointer correctly

I have the following issue when I drag and drop from a tree viewer in SWT. I am setting the image attribute of the dragSourceEvent e.g.
event.image = _dragImage
which works fine EXCEPT for instances where I have a long label on my tree viewer node. If I have a long string and start the drag event from the far right edge of the caption on the node, I notice that the drag image is not correctly anchored and is actually offset X pixels according to the distance from the start of the tree node string to the end of the tree node string.
Anyone any suggestions? Yee would probably have to demo to get an idea of it. Has anyone seen this?
If you set the fields event.offsetX and event.offsetY to 0, it will put the top-left of the image exactly on the mouse pointer.
If you set them to 100, it will put the top-left of the image 100 pixels to the left and above the mouse pointer.
SWT pre-populates these numbers in an attempt to get your image to line up with the offset of your original click. Not sure why it's not working in your case.

Getting the EXACT coords of Mouse in Java

Alright, I know HOW to do but It's doing something weird.
I am trying to get the correct X Y coords for a JMenu to appear. I've found a way but it's only a hack. All the right-clicking takes place on the JList, for now until I add character panel, so let's say you right-click near the top-left. You expect the Y coord to be around 40~ pixels and the Y coord to be around 100~ pixels right? Because you're clicking on the near left where the JList is. Wrong. The x y coords count from the top left of the JList when I want it to count from the top left of the WHOLE application. :S
So, what did I do? Since the Y coord is correct, I added 512 pixels to the X coord so it's always in the JList. Like so:
int newMouseX = 512+e.getX();
popup.show(tileOffline.this, newMouseX, e.getY()); // show item at mouse
However, as you can tell I will not be right-clicking in the JList forever.
How can I make it so I get the exact X Y coords of the mouse from the WHOLE applet?
Here's a pic to describe the situation WITHOUT the 512 hack:
I wonder if you're making this harder than it needs to be. The popup menu will position itself relative to the component you pass into the show method (JPopupMenu API). You should just be able to show the popupmenu doing something like this in order to get it to show at the correct location:
popup.show(myJList, e.getX(), e.getY());
SwingUtilities has methods suitable for converting to and from screen coordinates, as well as among Components.
You can use convertPointToScreen in javax.swing.SwingUtilities to convert to screen coordinates - just pass in the component that you're clicking!
convertPointToScreen(Point p, Component c)
http://download.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html
Use the MouseEvent.getXOnScreen() and MouseEvent.getYOnScreen() methods.
The MouseEvent that gets generated when you move or click a mouse contains the absolute coordinates on the screen. Use these two methods to get them.

Java mouse operations

I am writing a game in Java, and need to have mouse interaction. I was going to use MouseAdapter, but I've looked into it some, and it does not seem to have any means of determining the location of the mouse pointer without a click or action being done... What would be the recommended way of doing this?
A couple questions:
Would the mouse "location" refer to the location of the mouse in relation to the bounds of the monitor, the bounds of the game, or would it be represented as movements relative to a previous location?
How would one disable the mouse pointer in a windowed application? (ie. A first person shooter where the mouse movements rotates the players view rather than move a pointer) Is this possible?
Look at the MouseMotionListener. This will allow you to track mouse movements. It will give you the location of the mouse within the component that it is attached to. But if you look at SwingUtilities class there are some convenience methods to convert points to the screen or other components or the monitor.
For the cursor, you have the option to set your own bitmap for the cursor, you can look here. Or for a more recent SO answer, you can look here. So you could hide the cursor, or set it to an empty bitmap, and then intercept the mouse movements and events to control your view.
A tutorial on mouse events is at:
http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html
If you look here:
http://java.sun.com/javase/7/docs/api/java/awt/event/MouseEvent.html
you can get the absolute location, on the screen, or coordinates relative to the component.
If there is a movement to change direction then you will need to remove the mouse listener, or you can just have some logic where the event handler will just exit, doing nothing. I think this would be better, otherwise you have to keep track of when you add and remove the listeners.

Categories