Getting the EXACT coords of Mouse in Java - 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.

Related

Setting the pivot point of a JPanel to its center

I am building a Java application that is going to feature two circles of random sizes that need to be clicked by the user. The time between the click on the first and the second circle is going to be measured. Unfortunately, since I am new to Java so things have been slow for me. Currently I have my application draw circles and measure time between clicks using System.nanoTime() but now I am running into a problem.
Because the circles need to be a fixed distance away from eachother I want to use the center of the circles as the origin points. So basically I want to be able to provide coordinates for the circle so that the center of the circle should be at those coordinates. The distance between the circles then describes the distance between the centers. My circle currently is embedded into a JPanel but if I set the JPanel's position it moves the top left to that position.
Of course I have done some searching read that I may need to play around with either AffineTransform or Graphics2D.translate() which I have tried in paintComponent() but this got a bit confusing so then I tried to override setlocation and subtract the radius from the position. It sort of works but it is not the most clean solution. Can aonyone give me some pointers on how to do this?
Thanks in advance.
If I understand the problem statement, all such pairs of circles will lie on opposite sides of a circle centered in the enclosing panel, as shown here. Simply choose a random 0 ≤ θ < π and find its opposite at π - θ. Note how the example's rendering scales as the panel is resized.
As an aside, the example uses setPreferredSize() to establish the dimensions of the drawing panel, but you may want to override getPreferredSize() instead.
Addendum: The example uses fillOval() to render the circles, but you can use draw() with any desired Shape; the latter provides several contains() methods suitable for hit testing, as mentioned here.
You have the coordinates for the two center for the circle (x1, y1) and (x2, y2).
The size of the radius is random.
Once you have the radius of the two, r1 and r2, simply position them at (x1-r1, y1-r1) and (x2-r2, y2-r2).
You can use java.awt.Point to represent the center, and use
center.translate(-radius, -radius)
and use the new translated value as position for the drawing.
Maybe you think it is not a clean solution, but why not? Everything in Java is painted by giving the top left corner for the position, so is the use of the center that is not clean :).
To calculate the left top position by doing -radius is clean :)

Dragging an object in Swing

I have a problem which I have to solve with my code. So I have a checkerboard and when I click on something I want to drag around (i.e. checker), I make visible a label which holds picture of it (I know it ain't the neat solution but I was told to do it so) in a proper place calculated by a fomula. I wanted to use the same formula to be able calculate coordinates of the checker under mouseDragged event but here's the problem. If I just set the location of the label by simple getX() or getY() from the event then it's top left corner is "jumping" under my mouse pointer as soon as I move the mouse. I'd like it to stick to the cursor in the place I clicked it, so I did something like this:
public void mouseDragged(MouseEvent evt) {
int col = (evt.getX() - 4) / 40;
int row = (evt.getY() - 4) / 40;
int setX = evt.getX()-(col*40);
int setY = evt.getY()-(row*40);
if (dragged)
inv.setLocation(evt.getX()-setX+42, evt.getY()-setY+37);
where col is the X coordinate of the column I want to drage the checker from (and so is the row) and setX is just a variable I'm using below. And these calculations seem to be ok to me, but they make me able only to drage the checker in a manner where it doesn't move at all (like I was dragging something transparent) and when I enter finally a new place on the board it's being painted there and so on. Not a fluent drag but it's being consequently drawn in the fields under my cursor during dragging.
The +42 and + 37 are just some numbers to calcualte it in proper place since the board isn't starting from (0,0).
The reason the corner of the checker is "jumping" to your mouse is because of the way images are drawn. When you set the coordinates of the image you are setting the point P(0,0) of the image, which is the top-left corner when drawn on a screen.
To fix this issue, in addition to keeping track of the row and column number, you need to keep track of the x and y offset (in pixels) you want to maintain though the mouse movement in each checker board "cell".
This means when you click, you might click the checker in row 2, column 3, with an (x, y) offset of (12,14). I am assuming your squares are larger than 14x14 pixels.
I turned your 42 into BOARD_X_OFFSET and 37 into BOARD_Y_OFFSET
I have added two doubles cellX and cellY which hold the offset within the cell.
This will make your set location look similar to this:
inv.setLocation(evt.getX()-setX+BOARD_X_OFFSET+cellX, evt.getY()-setY+BOARD_Y_OFFSET+cellY);

Making fillRect fill dynamically from right to left

Hi i am currently working on a Java app and i have to draw a little using Canvas, Graphics etc..
So if i click a point and drag it across, it should have a line drawn in between (Think of drawing a line in paint).
I am currently using fillRect and the question is, is there a way to fillRect from right to left? Or do i have to explicitly create a workaround for this?
fillRect() method needs four arguments, x, y, width, height. What you should do is just compute the values of those arguments. If you want to draw fillRect from right to left, you just need to decrease x and increase width perhaps as your mouse move. That's it.
To add to ntalbs excellent answer (+1).
Basically, when the user clicks a point, you need to store that as the anchor point. When they drag the mouse, you need to determine in which direction the mouse has dragged.
If the click.x > drag.x, the the drag.x becomes the x parameter for your rectangle, otherwise it's the click.x.
Width and height are simple determine as the difference between the click and drag points (taking into consideration which is larger ;))

How to find location of JTree node on screen

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.

How do I get the mouse position on click when it is outside of the component?

I'm writing a small application in Java that will click ten different locations on my screen each minute for 10 minutes. But before doing the clicking I want to choose the locations it will click by clicking on the screen and storing the x & y coordinates in an arraylist.
I have read that I can get it by using MouseInfo however I want to get the coordinates when the mouse is clicked (which may not include being on the component). So how does one do this?
Will I have to create a see through component that takes up the entire screen and get the coordinates that way? Or is there a better way to go about this?
Try this one...
Use MouseMoved event to get the mouse position.
Plot the Rectangle in the window or your component, using the Rectangle class and store the values.
In the MouseClicked event, get the values of x and y from the mouse moved event and compare with your rectangle array.
If the values are between the range the perform the action. (Sorry for the grammer mistakes.)
Ex: code
mousemoved Event:
x=event.getX();
y=event.getY();
mouseclicked Event:
// use Event object e or event to get value of
x=event.getX();
y=event.getY();
// this is more easy than the mousemoved event..
for(...) {
if(x > rectangle[i].getMin && rectangle[i].getMax < x
&& y > rectangle[i].getMin && rectangle[i].getMax < y)
{
// your action..
}
else
{
}
}
I decided to just make a translucent window the size of the screen (which is barely visible) and collected the mouse positions that way. It seemed to work, but I was hoping that I wouldn't have to paint an extra component.
Summarized (for those that care):
Make translucent window
User is able to see through the window and can click where one wants
Mouse coordinates are collected
Window is discarded
So far this seems to be my best option, unless someone else mentions one shortly :)

Categories