Let click in a region randomly

Asked by Ze-ping on 2017-02-10

Hello ,I have some question of coding siklui in Java.
Question1 :
 I want let sikuli to find a image location,and click it randomly.
My code is works for me ,but my code seems can be more easier .

Here is my code01:
----------------------------------------------------------
class Randomcb {
 Screen s = new Screen();
 Region reg = new Screen();
 Random ran = new Random();
 private int Rx,Ry,Rh,Rw,fx,fy;
 public void RndClik(String Path)
 {
 Rx = reg.exists(Path).getX();
    Ry = reg.exists(Path).getY();
    Rh = reg.exists(Path).getH();
    Rw = reg.exists(Path).getW();

   fx=Rx+ran.nextInt(Rw);
   fy=Ry+ran.nextInt(Rh);
   Location Lr =new Location(fx,fy);

    try{ s.click(Lr);}
    catch(FindFailed e){e.printStackTrace();}
    }
----------------------------------------------------------
And I can call by folowing:

Randomcb randomcb= new Randomcb();
randomcb.RndClik("image path");

Question2 :
I want just move mouse to a location by coordinate.
The same ,this code works for me,but when I just feel something wrong.
Maybe sikuli already has some way to do this?

Here is my code02
----------------------------------------------------------
public void MouseMoveTo(int bx,int by)
 {
         Region reg = new Screen();
  reg.x=bx;
                reg.y=by;
  Location Lr2= new Location(reg.x, reg.y);
   try{reg.mouseMove(Lr2);}
   catch(FindFailed e){e.printStackTrace();}
 }
----------------------------------------------------------

Thank you for your reading

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2017-02-11
Last query:
2017-02-11
Last reply:
2017-02-10
Best RaiMan (raimund-hocke) said : #1

first snippet:

Using version 1.1.1 this works:

class Randomcb {
 Random ran = new Random();
 Region reg = new Screen();
 public void RndClik(String path) {
  Match match = reg.exists(path);
  if (null != match) {
    match.getCenter().offset(ran.nextInt(match.w), ran.nextInt(match.h)).click();
  }
 }
}

RaiMan (raimund-hocke) said : #2

snippet 2:

simply:
new Location(x, y).hover();

Ze-ping (ascte) said : #3

Thanks RaiMan, that solved my question.

Ze-ping (ascte) said : #4

Thank you ,

Case 2 is much clear than my code.

But in case 1 ,I need to click in image region so getCenet have change to get TopLeft ,like following:

match.getTopLeft().offset((ran.nextInt(match.w)), (ran.nextInt(match.h))).click();

Anyway still thank you , :D

RaiMan (raimund-hocke) said : #5

case 1:
yes of course.
I started to click around the center and then realized, that you want to click anywhere in the match region, but did not revise my code :-( hence your solution is correct.

Thanks for feedback.