1

I was writing code which first adds mouse position to arraylist (with dealys) and after that, it will repeat by moveMouse (robot). I think that I'm doing all well. But it's not working. Can anyone help me? Thanks!

Code: CoursorMove

public class CoursorMove {

private ArrayList<Point> coordinates = new ArrayList<>();

public void addNewObjectWithCoordinates() {
    coordinates.add(MouseInfo.getPointerInfo().getLocation());
}

public Point getCoordinate(int index) {
    return coordinates.get(index);

}

public void play() {

    for (int i = 0; i < 5; i++) {
        CoursorMove bang = new CoursorMove();
        bang.addNewObjectWithCoordinates();
        System.out.println(bang.getCoordinate(0).getX());

        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

int howmany = coordinates.size();
int index = 0;

public int getHowmany() {
    return howmany;
}

public void setHowmany(int howmany) {
    this.howmany = howmany;
}

public void moveCoursor() {

    while (index < howmany) {
        try {
            Robot robot = new Robot();
            robot.mouseMove(coordinates.get(index).x, coordinates.get(index).y);
            robot.delay(1500);
        } catch (AWTException e) {
            System.err.println("Error CM 68L"); // error CoursorMove class
                                                // Line 68
            e.printStackTrace();
        }
        index++;
    }
}
 }

Main.

public class Main {
public static void main(String[] args) {
    CoursorMove triup = new CoursorMove();
    triup.play();
    triup.moveCoursor();
    }
}
2
  • Please describe what happens vs what should happen with a minimal reproducible example. Thank you Commented May 21, 2016 at 17:14
  • Hint: CoursorMove bang = new CoursorMove(); makes a brand new bang in every for loop and discards it at the end of the loop. Is that what you want? Commented May 21, 2016 at 17:16

2 Answers 2

3

Here are a few modifications that should help.

First, you don't need to store separate variables for how many coordinates you have

public int getHowmany() {
    return coordinates.size();
}

Second, you are never adding to the same coordinates list because you use a new instance of your class. You don't need to make one at all, you can call those methods directly on the current instance.

public void play() {

    for (int i = 0; i < 5; i++) {
        addNewObjectWithCoordinates();
        System.out.println(getCoordinate(0).getX());

        // sleep thread 
    }
}

Then same problem below, you probably only want one robot, not one for every loop

public void moveCoursor() {

    Robot robot = new Robot();
    while (index < getHowmany()) {
        try {
            robot.mouseMove... 
Sign up to request clarification or add additional context in comments.

1 Comment

WoW! Man You're amazing! Wait... You and Hansa. I change only one with method play. I have object.getCoordinate. And after i have just getCoordinate. I delete object from the code. And all is fantastic working! Big thanks! Have a nice day!
2

Did you verify that you jump into the

while (index < howmany) {}

loop?


from what I see here is you put:

int howmany = coordinates.size();
int index = 0;

into your class directly. But you never update "howmany" after you added items to it. As a result is howmany = 0 at initialization, because coordinates.size() is 0 in the beginning.

I guess you have to set "howmany"'s value after you added your coordinates.

e.g.

public void play() {

  for (int i = 0; i < 5; i++) {
    addNewObjectWithCoordinates();
    System.out.println(getCoordinate(0).getX());

    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
  }
  howmany = coordinates.size();
}

EDIT: Additionally you have to stop creating a new CoursorMove object every time. I updated the play method for that

4 Comments

yes, thats what I am saying. :) Therefore I added 'howmany = coordinates.size();' to the play() method.
Thanks for your answer. I updated the code, but problem isnt fixed. Any suggest?
could you verify that you enter the while loop? you could debug it or add a System.out.println into the loop
Sure, I added println, and it isnt writing. Like program don't see this method. But before before when i wasnt add question on stackover loop give me some error but compiler dont find antyhing bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.