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?

有帮助吗?

解决方案 2

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 :)

其他提示

Try this one...

  1. Use MouseMoved event to get the mouse position.
  2. Plot the Rectangle in the window or your component, using the Rectangle class and store the values.
  3. In the MouseClicked event, get the values of x and y from the mouse moved event and compare with your rectangle array.
  4. 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
    {

    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top