简体   繁体   中英

Java robot doesn't work

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();
    }
}

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.

eg

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

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... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM