2011-12-16 7 views
2

私は、java.awt.Robotを使って自動クリッカーを作っています。しかし、私が持っている懸念の一つは、動きがあまり人間的ではないということです。誰かが私のコードにいくつかの変更を提案して、より人間的なものにすることができますか?今はまっすぐに動きます。マウスの動きを人間的にする(目的地まで直線ではなく円弧を使用)

/** 
* 
* @param robot The java.awt.Robot being utilized 
* @param sx The start x position of the mouse 
* @param sy The start y potition of the mouse 
* @param ex The end x position of the mouse 
* @param ey The end y position of the mouse 
* @param speed The speed at which to travel 
*/ 
public void moveMouse(Robot robot, int sx, int sy, int ex, int ey, int speed){ 
    for (int i=0; i<100; i++){ 
     int mov_x = ((ex * i)/100) + (sx*(100-i)/100); 
     int mov_y = ((ey * i)/100) + (sy*(100-i)/100); 
     robot.mouseMove(mov_x,mov_y); 
     robot.delay(speed); 
    } 

} 

更新: 私はBézier Curvesを利用したアルゴリズムで行くことにしました。私が変更を実装してから非常に長い時間がかかりましたが、人々が将来的に役立つと思うように、ここに投稿したかったのです。ここで私は結局何をしたのですか:

public class MouseEvent{ 
    public int getMouseX(){ 
     return MouseInfo.getPointerInfo().getLocation().x; 
    } 

    public int getMouseY(){ 
     return MouseInfo.getPointerInfo().getLocation().y; 
    } 

    public void moveMouse(int speed, int destX, int destY, int ranX, int ranY){ 
     Mouse.moveMouse(new Robot(), new Point(getMouseX(),getMouseY()), new Point(destX, destY), speed, ranX, ranY); 
    } 
} 

public class Mouse { 
    public static void moveMouse(Robot robot, Point s, Point e, int speed, int ranX, int ranY){ 
     if(Math.abs(e.x-s.x) <= ranX && Math.abs(e.y-s.y) <= ranY) 
      return; 

     Point[] cooardList; 
     double t; //the time interval 
     double k = .025; 
     cooardList = new Point[4]; 

     //set the beginning and end points 
     cooardList[0] = s; 
     cooardList[3] = new Point(e.x+random(-ranX,ranX),e.y+(random(-ranY,ranY))); 

     int xout = (int)(Math.abs(e.x - s.x) /10); 
     int yout = (int)(Math.abs(e.y - s.y) /10); 

     int x=0,y=0; 

     x = s.x < e.x 
      ? s.x + ((xout > 0) ? random(1,xout) : 1) 
      : s.x - ((xout > 0) ? random(1,xout) : 1); 
     y = s.y < e.y 
      ? s.y + ((yout > 0) ? random(1,yout) : 1) 
      : s.y - ((yout > 0) ? random(1,yout) : 1); 
     cooardList[1] = new Point(x,y); 

     x = e.x < s.x 
      ? e.x + ((xout > 0) ? random(1,xout) : 1) 
      : e.x - ((xout > 0) ? random(1,xout) : 1); 
     y = e.y < s.y 
      ? e.y + ((yout > 0) ? random(1,yout) : 1) 
      : e.y - ((yout > 0) ? random(1,yout) : 1); 
     cooardList[2] = new Point(x,y); 

     double px = 0,py = 0; 
     for(t=k;t<=1+k;t+=k){ 
      //use Berstein polynomials 
      px=(cooardList[0].x+t*(-cooardList[0].x*3+t*(3*cooardList[0].x- 
       cooardList[0].x*t)))+t*(3*cooardList[1].x+t*(-6*cooardList[1].x+ 
       cooardList[1].x*3*t))+t*t*(cooardList[2].x*3-cooardList[2].x*3*t)+ 
       cooardList[3].x*t*t*t; 
      py=(cooardList[0].y+t*(-cooardList[0].y*3+t*(3*cooardList[0].y- 
       cooardList[0].y*t)))+t*(3*cooardList[1].y+t*(-6*cooardList[1].y+ 
       cooardList[1].y*3*t))+t*t*(cooardList[2].y*3-cooardList[2].y*3*t)+ 
       cooardList[3].y*t*t*t; 
      robot.mouseMove((int)px, (int)py); 
      robot.delay(random(speed,speed*2)); 
     } 
    }  
} 
+0

「人間的」とはどういう意味ですか? – nebula

+0

まっすぐには動かず、ここには数ピクセルの円弧があります。 –

+0

次に乱数を生成し、その数を使って直線を変化させます。次に、ロボットクラスを使用します。 – nebula

答えて

1

Catmull-Romメソッドを使用できます。終点のどこかのどこかにランダムなコントロールポイントを生成し、直線がどこにあるのか、開始から終了まで(パラメータt、0から1へ)各ステップの座標を求める。

参照のデモアプレットとソース:http://www.cse.unsw.edu.au/~lambert/splines/

1
public void moveMouse(int sx, int sy, int ex, int ey, int speed) throws AWTException { 
     Robot robot = new Robot(); 
     int a = 10; 
     boolean flag = true; 
     for (int i = 0; i < 100; i++) { 
      int mov_x = ((ex * i)/100) + (sx * (100 - i)/100); 
      int mov_y = ((ey * i)/100) + (sy * (100 - i)/100); 
      if (flag == true) { 
       robot.mouseMove(mov_x + a, mov_y); // adds 10 to X-axis 
       flag = false; 
      } else { 
       robot.mouseMove(mov_x - 2 * a, mov_y); // subtracts 20 to X-axis 
       flag = true; 
      } 
      robot.delay(speed); 
     } 
    } 

はちょうどあなたのコードを操作します。これにより、マウスがX方向の直線経路に移動します。あなたはここからあなたが望むものを達成することができます。ちょうどアイデアを得る。 mov_xmov_yを操作できる場合は、任意の方法で移動できます。

+0

私が間違っていれば訂正します。あなたは終点が目標の目的地になることを保証しました。また、これはすべての場所でジグザグです。たとえあなたが= 1を変えても、マウスはまだ急速に動揺します。 –

+0

moveMouse()に何を渡しましたか?私はmoveMouse(30,30,30,500,100)を試しました。私は、円は人のようなものではない円の一部に幾分似ていることを意味します。 – nebula

+0

ところで、サークル、ライン、または任意のタイプのカーブの動きを乱数(あなたの言葉では人間のようなもの)で生成したい場合は、まずサークル、ラインまたはカーブの描画アルゴリズムを知ってから乱数を生成する必要があります。 http://stackoverflow.com/questions/6641977/how-to-create-a-curve-between-2-points-in-2d-and-get-back-points-that-makes-that – nebula

関連する問題