0

im tring to animate motion of the mouse from 1 button to another in an array with the class robot. here are the two methods I used:

public void optimusprime(int row, int column, JButton current) throws InterruptedException { Point p; Point p2; double x; double y; double x2; double y2; double conx = 0; double m; double b; double cony; p = current.getLocationOnScreen(); x = (int)( p.getX() + 30.5); y = (int)( p.getY() + 30.5); optimus((int) x, (int) y); p2 = mesa[row][column].getLocationOnScreen(); x2 = (int) (p2.getX() + 30.5); y2 = (int) (p2.getY() + 30.5); m = (y2 - y) / (x2 - x); b = y - (m * x); while (conx != x2) { conx = x; cony = (m * conx) + b; optimus((int) conx, (int) cony); conx++; Thread.sleep(500); } } public void optimus(int x, int y) { try { Robot robot = new Robot(); robot.mouseMove(x, y); } catch (AWTException e) { } } 

can any 1 help me here or at least give a recommendation?. got stock in an endless loop(had to shutdown the pc) and it didnt work at all; Im new to java it could be several stupid mistakes ;

1 Answer 1

1

The problem may be that your conx never truly equals your x2 because you're doing all your math as doubles. That would mean that conx would go from being slightly less than x2 to being slightly greater than x2 and would either bounce back and forth between the two or would continue moving along its current trajectory. You will want to add some logic to make sure you don't step over your target, which you could do by setting a threshold for once it's close enough or by stopping as soon as you over step it.

Sign up to request clarification or add additional context in comments.

1 Comment

thx it was the conx=x inside the loop conx stayed at x all the time ahh stupid mistake als0 had to add if (x2>x)else just in case x2<x. The stepping over thing also help thx!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.