I have this class that first stores the current coordinates of the mouse cursor, then creates two random integer numbers x and y both are between -10 and 10 and then moves mouse cursor horizontally and vertically on the amount that equals those generated numbers respectively.
public class Test {
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private static int width= (int) screenSize.getWidth();
private static int height=(int) screenSize.getHeight();
private static int currentX=(int) MouseInfo.getPointerInfo().getLocation().getX();
private static int currentY =(int) MouseInfo.getPointerInfo().getLocation().getY();
public static void main(String[] args) throws java.awt.AWTException {
Robot robot=new Robot();
Random random = new Random();
int xMove;
int yMove;
int[] resultArray=new int[10000];
for (int i=0; i<10000; i++) {
refreshCoords();
xMove=random.nextInt(20) - 10; // - 10 to 10
yMove=random.nextInt(20) - 10;
robot.mouseMove(currentX + xMove, currentY + yMove);
resultArray[i]=xMove;
}
System.out.println(sumArray(resultArray)); //returns - 4000/-5000
}
private static void refreshCoords() {
currentX=(int) MouseInfo.getPointerInfo().getLocation().getX();
currentY=(int) MouseInfo.getPointerInfo().getLocation().getY();
}
private static int sumArray(int[] resultArray) {
int result=0;
for (int i=0; i<resultArray.length; i++) {
result+=resultArray[i];
}
return result;
}
}
The thing is, although I expected the mouse cursor to mostly stay at the same place or move across the screen gradually and not too far, the mouse cursor tends to move to the upper left corner no matter where it was first located. I can't figure out what is wrong here, why does this happen? I would think that if with each loop the mouse cursor moves - 10 to 10 to the right/left and to the top/bottom it would wander randomly across the screen, not take liking of some particular position/corner. I summed all the horizontal moves and it gives a big negative number (-4000+ or -5000+) indicating that the cursor indeed tends to move to the left border...
Aucun commentaire:
Enregistrer un commentaire