extends Screen will make it work but why...

Asked by Ethan.kwok

Hi RaiMen,

It's me again...I am trying to make my extension working now,

In the first case, I kind of wrote it like this,

public class extendFuncs extends Region{
 public int GetHome() throws Exception{
  int i;
  for(i=0;i<3;i++){
   type(Key.ESC);
   wait(2.0);
  }
  return 0;
 }
}

And while executing my script, I got a null Pointer exception. I guess it's caused by _robot(right?).
So I modified the code like this in order to initialize _robot, in the second case,

public class extendFuncs extends Region{
 public extendFuncs(){
  Rectangle r = new Screen().getBounds();
  Region.create((int)(r.getX()), (int)(r.getY()), (int)(r.width), (int)(r.height));
 }

 public int GetHome() throws Exception{
  int i;
  for(i=0;i<3;i++){
   this.type(Key.ESC);
   this.wait(2.0);
  }
  return 0;
 }
}

And now, I will get a FindFailed exception...
Finally, I wrote it like this, in the third case,

public class extendFuncs extends Screen{
 public int GetHome() throws Exception{
  int i;
  for(i=0;i<3;i++){
   type(Key.ESC);
   wait(2.0);
  }
  return 0;
 }
}

and now it's working but why? I notice that class screen extends class region but _robot is private...

As a starter of Java, I am almost lost now...

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

The class Region currently has some quirks (caused by the upward back connection to Jython level - known bug), that make it very difficult to extend it normally.

But using Screen instead does not really make sense, since each Screen object has its own Robot object.

So if you want to use classes/methods, to have your own wrapping features, do not use extend, but use just Screen and Region objects normally:

public class XtendRegion {
 Region region;
 XtendRegion () {
  region = new Screen();
 }
 XtendRegion (x, y, w, h) {
  region = Region.create(x,y,w,h)
 }
// ... more constructors, if needed
 public int GetHome() throws Exception{
  int i;
  for(i=0;i<3;i++){
   region.type(Key.ESC);
   region.wait(2.0);
  }
  return 0;
 }
}

Can you help with this problem?

Provide an answer of your own, or ask Ethan.kwok for more information if necessary.

To post a message you must log in.