2016-05-21 138 views
0

我正在寫代碼,首先將鼠標位置添加到arraylist(with dealys),之後,它將由moveMouse(robot)重複。我認爲我做得很好。但它不起作用。誰能幫我?謝謝!Java機器人不工作

代碼: 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++; 
    } 
} 
} 

主。

public class Main { 
public static void main(String[] args) { 
    CoursorMove triup = new CoursorMove(); 
    triup.play(); 
    triup.moveCoursor(); 
    } 
} 
+0

請描述發生VS應該用[MCVE]發生什麼。謝謝 –

+0

提示:'CoursorMove bang = new CoursorMove();'在每個for循環中創建一個全新的'bang',並在循環結束時丟棄它。那是你要的嗎? –

回答

3

下面是一些應該有所幫助的修改。

首先,你並不需要存儲多少座標獨立變量,你必須

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

其次,你永遠不會加入到同一座標列表中,因爲您使用類的一個新實例。您根本不需要創建一個,您可以直接在當前實例上調用這些方法。

public void play() { 

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

     // sleep thread 
    } 
} 

然後在下面同樣的問題,你可能只需要一個機器人,而不是一個每天循環

public void moveCoursor() { 

    Robot robot = new Robot(); 
    while (index < getHowmany()) { 
     try { 
      robot.mouseMove... 
+0

WoW!男人你真了不起!等等...你和Hansa。我只用方法打法改變了一個。我有object.getCoordinate。在我剛剛獲得協調之後。我從代碼中刪除對象。所有的工作都非常棒!十分感謝!祝你今天愉快! – Brade

3

你確認你跳進

while (index < howmany) {} 

循環?


從我這裏看到的是你把:

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

到類直接。但是,在添加項目之後,您絕不會更新「howmany」。 因此,在初始化時howmany = 0,因爲coordinates.size()在開始時爲0。

我想你必須在添加座標後設置「howmany」的值。

例如

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

編輯: 另外,你必須停止每次創建新CoursorMove對象。我更新了播放方法

+0

是的,那就是我說的。 :) 因此我加了'howmany = coordinates.size();'到play()方法。 – Hansa

+0

感謝您的回答。我更新了代碼,但問題沒有解決。任何建議? – Brade

+0

你可以驗證你輸入while循環嗎?你可以調試它或添加一個System.out.println到循環 – Hansa