What I want to do is inside the rest of my code have it wait for and recognise a mouse click event and then be able to use the coordinates of that click in the rest of the code, not just print it to the screen on click as pretty much every other example I have found does.
I am new to java and this is the first time I'm making an interface so I do apologise for any mistakes and I will fix them as soon as I can.
Unfortunately, as I don't have a clue how to do what I am asking for, I can only represent it in a sort of pseudocode crossed with bits of code or I wouldn't be asking here in the first place.
edit: Below comment says don't do this, which I understand but I really need to be able to get those coordinates to be usable in that area of code (which is a lot longer than shown below) so if anyone has any alternative ways in which I could achieve that then please share.
It is the two clicks at two different locations to get two different coordinates to then manipulate that is really throwing me.
.....
for(int i = 0; i < 15; i++) {
wait for mouse click;
int x = x coordinate of mouse click;
int y = y coordinate of mouse click;
wait for mouse click;
int x2 = x coordinate of second mouse click;
int y2 = y coordinate of second mouse click;
if (closestVertex(x,y) != closestVertex(x2,y2) && ! graph.isline(x,y,x2,y2)) {
graph.insertLine(x,y,x2,y2);
}
}
.... etc...
So far the solutions I have seen have involved looking at where the pointer is at a given moment but that is not the location only at the moment when clicked. This type of solution is illustrated below:
int x = MouseInfo.getPointerInfo().getLocation.x;
int y = MouseInfo.getPointerInfo().getLocation.y;
And also another common one that crops up is altering the MouseListener as below:
@Override
public void mouseClicked(MouseEvent e) {
int x=e.getX();
int y=e.getY();
System.out.println(x+","+y);
}
But for this option, yes, you can print the coordinate, but I don't see a way that I can use the coordinate details outside of the mouseClicked function as I would like.
In terms of getting it to wait for the action to occur and not just keep going through the loop, I have a couple of ideas. I thought of adding a while loop inside and using wait() and Thread.sleep(100) but the issue is I had no idea of how to say essentially:
while(event hasn't occurred) {
wait()
}
int x = x coordinate of mouse click;
int y = y coordinate of mouse click;
...etc
If you have any new ideas I could try I would greatly appreciate it as I have been through I don't even know how many links on google looking for a way to make this work.
You need to change the control flow of your program so that your code is responding to UI events, not trying to run the UI itself. Try something like this:
class MyStateMachine {
int i = 0; // this is your loop index
boolean waitingForSecondClick = false; // is the next click the first or second?
int firstX; // coordinates of the first click
int firstY;
void receiveCoordinates(int x, int y) {
if (waitingForSecondClick) {
if (closestvertex(....)) {
// do your stuff
}
++i;
waitingForSecondClick = false;
} else {
firstX = x;
firstY = y;
waitingForSecondClick = true;
}
}
So your mouse listener becomes:
@Override
public void mouseClicked(MouseEvent e) {
stateMachine.receiveCoordinates(e.getX(), e.getY())
}
The first time your object receives a mouse click it just saves the coordinates in its instance variables and remembers that the next click is a 'second click'. Next click it uses both clicks to do your task, increments i
and starts looking for another 'first click'.