org.sikuli.script.Region Java Examples

The following examples show how to use org.sikuli.script.Region. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Guide.java    From SikuliX1 with MIT License 6 votes vote down vote up
private void init(Region region) {
  try {
    robot = new Robot();
  } catch (AWTException e1) {
    e1.printStackTrace();
  }
  content = getJPanel();
  _region = region;
  Rectangle rect = _region.getRect();
  content.setPreferredSize(rect.getSize());
  add(content);
  setBounds(rect);
  getRootPane().putClientProperty("Window.shadow", Boolean.FALSE);
  ((JPanel) getContentPane()).setDoubleBuffered(true);
  setVisible(false);
  setFocusableWindowState(false);
}
 
Example #2
Source File: ImageCommand.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
public List<String> getObjectAreas(Object target, String name) {
    List<Map<String, String>> objectList = new ArrayList<>();
    if (target instanceof Region) {
        Region region = (Region) target;
        Map<String, String> obMap = new HashMap<>();
        obMap.put("name", name);
        obMap.put("area", "[" + region.x + "," + region.y + "," + region.w + "," + region.h + "]");
        objectList.add(obMap);
    }
    try {
        if (!objectList.isEmpty()) {
            return Arrays.asList(new ObjectMapper().writeValueAsString(objectList));
        }
    } catch (JsonProcessingException ex) {
        Logger.getLogger(ImageCommand.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}
 
Example #3
Source File: CommonImageMethods.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
/**
 * verify the image present on SCREEN
 */
@Action(object = ObjectType.IMAGE, desc = "Verify if the image [<Data>] is present on the screen.")
public void imgVerifyImage() {

    try {
        target = findTarget(imageObjectGroup, Flag.REGION_ONLY, Flag.MATCH_ONLY);
        if (target != null) {
            ((Region) target).highlight(1);
            Report.updateTestLog(Action, "The Image " + ObjectName + " Exists on screen",
                    Status.PASS);
            return;
        }
        Report.updateTestLog(Action,
                ObjectName
                + (imageObjectGroup.isLeaf() ? ExceptionType.Empty_Group : ExceptionType.Not_Found_on_Screen),
                Status.FAIL);
    } catch (Exception ex) {
        Report.updateTestLog(Action, ex.getMessage(), Status.DEBUG);
        Logger.getLogger(CommonImageMethods.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example #4
Source File: Mouse.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
/**
 * perform mouse up on the given image
 */
@Action(object = ObjectType.IMAGE, desc ="Perform  Mouse key Up  action on the image [<Object>]", input =InputType.YES)
public void imgmouseUp() {
    int button = parseToInt(Data, Button.LEFT);
    try {
        target = findTarget(imageObjectGroup, Flag.SET_OFFSET, Flag.MATCH_ONLY);
        if (target != null) {
            ((Region) target).mouseUp(button);
            Report.updateTestLog(Action, "Mouse Up action is done on " + ObjectName,
                    Status.DONE);
            return;

        }
        Report.updateTestLog(Action,
                ObjectName
                + (imageObjectGroup.isLeaf() ? ExceptionType.Empty_Group : ExceptionType.Not_Found_on_Screen),
                Status.FAIL);

    } catch (Exception ex) {
        Report.updateTestLog(Action,
                ex.getMessage(), Status.DEBUG);
        Logger.getLogger(Mouse.class.getName()).log(Level.SEVERE, null, ex);
    }

}
 
Example #5
Source File: PropertyEditor.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
private List<Shape> getMatchesList() {
    List<Shape> rMatches = new ArrayList<>();
    try {
        Iterator<?> it = getMatches();
        if (it != null) {
            Region sRegion = Region.create(shapes.get(0).getBounds());
            while (it.hasNext()) {
                Object region = it.next();
                Shape rx = ((Region) region).getRect();
                if (sRegion != null && sRegion.getRect().contains(rx.getBounds())) {
                    rMatches.add(rx);
                }
            }
        }

    } catch (FindFailed | IOException | NullPointerException ex) {
        Logger.getLogger(PropertyEditor.class.getName()).log(Level.SEVERE, null, ex);
    }
    return rMatches;
}
 
Example #6
Source File: Highlight.java    From SikuliX1 with MIT License 6 votes vote down vote up
public Highlight(Region reg, String color) {
  this();
  if (isShowable()) {
    givenColor = evalColor(color);
    region = reg;
    locx = reg.x;
    locy = reg.y;
    sidex = reg.h;
    sidey = reg.w;
    JPanel panel = initAsFrame();
    if (panel == null) {
      getRootPane().setBorder(BorderFactory.createLineBorder(givenColor, 3));
    } else {
      setContentPane(panel);
    }
  }
}
 
Example #7
Source File: DistanceComparator.java    From SikuliX1 with MIT License 6 votes vote down vote up
/**
 * Compares the distance of two {@link Region} objects.
 * @param region1 The first {@link Region} object
 * @param region2 The second {@link Region} object
 * @return
 * <ul>
 * <li>-1 if the distance to region2 is smaller than to region1</li>
 * <li>0 if the distances are equal</li>
 * <li>1 if the distance to region1 is smaller than to region2</li>
 * </ul>
 */
@Override
public int compare(Region region1, Region region2) {
    if (region1 == region2) {
        return 0;
    }

    double distance1 = Math.sqrt(Math.pow(y - region1.getY(),2) + Math.pow(x - region1.getX(),2));
    double distance2 = Math.sqrt(Math.pow(y - region2.getY(),2) + Math.pow(x - region2.getX(),2));

    if (distance1 == distance2) {
        return 0;
    }

    return distance1  < distance2 ? -1 : 1;
}
 
Example #8
Source File: Tracker.java    From SikuliX1 with MIT License 6 votes vote down vote up
boolean isPatternStillThereInTheSameLocation(){
   try {
      sleep(1000);
   } catch (InterruptedException e) {
   }
   Region center = new Region(match);
   //<editor-fold defaultstate="collapsed" desc="TODO Pattern with BufferedImage">
    /* center.x += center.w/4-2;
    * center.y += center.h/4-2;
    * center.w = center.w/2+4;
    * center.h = center.h/2+4;
    */
   //</editor-fold>
   Match m = center.exists(centerPattern,0);
   if (m == null)
      Debug.log("[Tracker] Pattern is not seen in the same location.");
   return m != null;
   // Debug.log("[Tracker] Pattern is still in the same location" + m);
}
 
Example #9
Source File: EditorRegionButton.java    From SikuliX1 with MIT License 6 votes vote down vote up
private BufferedImage getRegionImage(int x, int y, int w, int h) {
  Region region = Region.create(x, y, w, h);
  IScreen _screen = region.getScreen();
  ScreenImage simg = _screen.capture();
  int scr_w = simg.w, scr_h = simg.h;
  int max_h = 80; // FIXME: put max_h in UserPreferences
  float scale = (float) max_h / scr_h;
  scr_w *= scale;
  scr_h *= scale;
  BufferedImage screen = new BufferedImage(scr_w, scr_h, BufferedImage.TYPE_INT_RGB);
  Graphics2D screen_g2d = screen.createGraphics();
  try {
    screen_g2d.drawImage(simg.getBufferedImage(), 0, 0, scr_w, scr_h, null);
    int sx = (int) ((x - simg.x) * scale), sy = (int) ((y - simg.y) * scale),
            sw = (int) (w * scale), sh = (int) (h * scale);
    screen_g2d.setColor(new Color(255, 0, 0, 150));
    screen_g2d.fillRect(sx, sy, sw, sh);
  } catch (RasterFormatException e) {
    Debug.error(me + "getRegionImage: Problem making image\n%s", e.getMessage());
  }
  screen_g2d.dispose();
  return screen;
}
 
Example #10
Source File: Guide.java    From SikuliX1 with MIT License 6 votes vote down vote up
public void updateSpotlights(ArrayList<Region> regions) {
  removeSpotlights();

  if (regions.isEmpty()) {

    setBackground(null);
    content.setBackground(null);

  } else {

    // if there are spotlights added, darken the background
    setBackground(new Color(0f, 0f, 0f, DIMMING_OPACITY));
    content.setBackground(new Color(0f, 0f, 0f, DIMMING_OPACITY));
    for (Region r : regions) {
      SxSpotlight spotlight = new SxSpotlight(r);
      spotlight.setShape(SxSpotlight.CIRCLE);
      //addSpotlight(r,SxSpotlight.CIRCLE);
    }
  }

  repaint();
}
 
Example #11
Source File: Guide.java    From SikuliX1 with MIT License 6 votes vote down vote up
public void addTracker(Pattern pattern, Region r, Visual c) {
  Tracker tracker = null;

  // find a tracker already assigned to this pattern
  for (Tracker t : trackers) {
    if (t.isAlreadyTracking(pattern, r)) {
      tracker = t;
      break;
    }
  }

  if (tracker == null) {
    tracker = new Tracker(this, pattern, r);
    trackers.add(tracker);
  }

  tracker.setAnchor(c);
}
 
Example #12
Source File: Guide.java    From SikuliX1 with MIT License 6 votes vote down vote up
public Visual arrow(Object from, Object to) {
  Visual gc = null;
  if (from instanceof Region) {
    gc = new SxArrow(((Region) from).getCenter().getPoint(), ((Region) to).getCenter().getPoint());
  } else if (from instanceof Point || from instanceof Location) {
    gc = new SxArrow((Point) from, (Point) to);
  } else if (from instanceof Visual) {
    gc = new SxArrow((Visual) from, (Visual) to);
  }
  if (gc != null) {
    gc.setGuide(this);
    addToFront(gc);
  } else {
    Debug.log(2, "Guide.arrow: invalid arguments");
  }
  return gc;
}
 
Example #13
Source File: Tracker.java    From SikuliX1 with MIT License 6 votes vote down vote up
public Tracker(Guide guide, Pattern pattern, Region match){
   this.guide = guide;
   //this.match = match;
   screen = new Screen();
   BufferedImage image;
   BufferedImage center;
   this.pattern = pattern;
   try {
      image = pattern.getBImage();
      int w = image.getWidth();
      int h = image.getHeight();
      center = image.getSubimage(w/4,h/4,w/2,h/2);
      centerPattern = new Pattern(center);
   } catch (Exception e) {
      e.printStackTrace();
   }
}
 
Example #14
Source File: SxBracket.java    From SikuliX1 with MIT License 6 votes vote down vote up
@Override
public Visual setLocationRelativeToRegion(Region region, Layout side) {
   if (side == Layout.TOP){
      setActualSize(region.w,thickness);
      setDirection(DIRECTION_SOUTH);
   } else if (side == Layout.BOTTOM){
      setActualSize(region.w,thickness);
      setDirection(DIRECTION_NORTH);
   } else if (side == Layout.LEFT){
      setActualSize(thickness,region.h);
      setDirection(DIRECTION_EAST);
   } else if (side == Layout.RIGHT){
      setActualSize(thickness,region.h);
      setDirection(DIRECTION_WEST);
   }

   if (side == Layout.LEFT || side == Layout.RIGHT){
      length = region.h;
   }else{
      length = region.w;
   }
   return super.setLocationRelativeToRegion(region,side);
}
 
Example #15
Source File: Tracker.java    From SikuliX1 with MIT License 5 votes vote down vote up
public boolean isAlreadyTracking(Pattern pattern, Region match) {
   try {
      boolean sameMatch = this.match == match;
      boolean sameBufferedImage = this.pattern.getBImage() == pattern.getBImage();
      boolean sameFilename = (this.pattern.getFilename() != null &&
            (this.pattern.getFilename().compareTo(pattern.getFilename()) == 0));
      return sameMatch || sameBufferedImage || sameFilename;
   } catch (Exception e) {
      return false;
   }
}
 
Example #16
Source File: SxBeam.java    From SikuliX1 with MIT License 5 votes vote down vote up
public SxBeam(Guide guide, Region target) {
  super(new Color(1f, 0f, 0f, 0.7f), null);
  super.addObserver(this);
  this.guide = guide;
  this.target = target;

  /*
   setBackground(null);
   // when opaque is set to false, the content seems to get cleared properly
   // this is tested on both Windows and Mac
   SysUtil.getOSUtil().setWindowOpaque(this, false);
   setOpacity(0.7f);
   */
}
 
Example #17
Source File: SxCallout.java    From SikuliX1 with MIT License 5 votes vote down vote up
@Override
public Visual setLocationRelativeToRegion(Region region, Layout side) {
  if (side != layout) {
    layout = side;
    updateComponent();
  }
  targetRegion = region;
  return super.setLocationRelativeToRegion(targetRegion, side);
}
 
Example #18
Source File: AndroidRegion.java    From sikuli-monkey with MIT License 5 votes vote down vote up
@Override
public Region nearby(int range) {
    Rectangle bounds = getScreen().getBounds();
    Rectangle rect = new Rectangle(x - range, y - range, w + range * 2, h + range * 2);
    rect = rect.intersection(bounds);

    return newRegion(rect);
}
 
Example #19
Source File: ElementBasicsTest.java    From SikuliX1 with MIT License 5 votes vote down vote up
@Test
public void test010_ImageRegion() {
  //defaultFrame.setVisible(true);
  testIntro();
  Assume.assumeFalse("Running headless - ignoring test", RunTime.isHeadless());
  Region region = new Region(100, 200, 300, 400);
  Image image = new Image(region);
  testOutro("%s", image);
  assertTrue("NotValid: " + image.toString(), image.isValid());
}
 
Example #20
Source File: ElementBasicsTest.java    From SikuliX1 with MIT License 5 votes vote down vote up
@Test
public void test012_ImageScreenImage() {
  testIntro();
  Assume.assumeFalse("Running headless - ignoring test", RunTime.isHeadless());
  Region region = new Region(100, 200, 300, 400);
  Image image = region.getImage();
  testOutro("%s", image);
  assertTrue("NotValid: " + image.toString(), image.isValid());
}
 
Example #21
Source File: Mouse.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
/**
 * perform mouse down on the given image
 */
@Action(object = ObjectType.IMAGE, 
		desc ="Perform  Mouse key Down action on the image [<Object>]", 
		input =InputType.YES)
public void imgmouseDown() {
    int button = parseToInt(Data, Button.LEFT);
    try {
        target = findTarget(imageObjectGroup, Flag.SET_OFFSET, Flag.MATCH_ONLY);
        if (target != null) {
            ((Region) target).mouseDown(button);
            Report.updateTestLog(Action, "Mouse Down action is done on " + ObjectName,
                    Status.DONE);
            return;

        }
        Report.updateTestLog(Action,
                ObjectName
                + (imageObjectGroup.isLeaf() ? ExceptionType.Empty_Group : ExceptionType.Not_Found_on_Screen),
                Status.FAIL);

    } catch (Exception ex) {
        Report.updateTestLog(Action,
                ex.getMessage(), Status.DEBUG);
        Logger.getLogger(Mouse.class.getName()).log(Level.SEVERE, null, ex);
    }

}
 
Example #22
Source File: ImageCommand.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
Region getRegion(Region r, String where, Integer... size) {

        switch (where.toUpperCase()) {
            case "RIGHT":
                return r.right(size.length == 1 ? size[0] : r.w);
            case "LEFT":
                return r.left(size.length == 1 ? size[0] : r.w);
            case "ABOVE":
                return r.above(size.length == 1 ? size[0] : r.h);
            case "BELOW":
                return r.below(size.length == 1 ? size[0] : r.h);
            default:
                return r;
        }
    }
 
Example #23
Source File: CommonImageMethods.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
/**
 * clear all text in the given image
 */
@Action(object = ObjectType.IMAGE,
        desc = "Clear the text present inside the [<Object>]"
)
public void imgClearText() {

    try {
        target = findTarget(imageObjectGroup, Flag.SET_OFFSET, Flag.MATCH_ONLY);
        if (target != null) {
            ((Region) target).click();

            robot.keyPress(getKeyEvent("Ctrl"));
            robot.keyPress(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_A);
            robot.keyRelease(getKeyEvent("Ctrl"));
            robot.keyPress(getKeyEvent("BACKSPACE"));
            robot.keyRelease(getKeyEvent("BACKSPACE"));
            Report.updateTestLog(Action,
                    "Cleared Text  on " + ObjectName, Status.DONE);
            return;

        }
        Report.updateTestLog(Action,
                ObjectName
                + (imageObjectGroup.isLeaf() ? ExceptionType.Empty_Group : ExceptionType.Not_Found_on_Screen),
                Status.FAIL);
    } catch (Exception ex) {
        Report.updateTestLog(Action, ex.getMessage(), Status.DEBUG);
        Logger.getLogger(CommonImageMethods.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example #24
Source File: SxImage.java    From SikuliX1 with MIT License 5 votes vote down vote up
@Override
public Visual setScale(float scale) {
  this.scale = scale;
  if (scale == 0) {
    int x = (int) (getTarget().getCenter().x - image.getWidth()/2);
    int y = (int) (getTarget().getCenter().y - image.getHeight()/2);
    setTarget(Region.create(x, y, image.getWidth(), image.getHeight()));
  } else {
    w = (int) (scale * image.getWidth());
    h = (int) (scale * image.getHeight());
  }
  return this;
}
 
Example #25
Source File: CommonImageMethods.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
/**
 * clear and set the value on a image
 */
@Action(object = ObjectType.IMAGE,
        desc = "clear text from image[<Object>] and enters the new text [<Data>]", input = InputType.YES)
public void imgClearAndSet() {
    try {
        target = findTarget(imageObjectGroup, Flag.SET_OFFSET, Flag.MATCH_ONLY);
        if (target != null) {
            ((Region) target).click();
            robot.keyPress(getKeyEvent("Ctrl"));
            robot.keyPress(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_A);
            robot.keyRelease(getKeyEvent("Ctrl"));
            robot.keyPress(getKeyEvent("BACKSPACE"));
            robot.keyRelease(getKeyEvent("BACKSPACE"));
            Thread.sleep(99);
            SCREEN.paste(Data);
            Report.updateTestLog(Action,
                    "Clear and Set on " + ObjectName + " is done", Status.DONE);
            return;

        }
        Report.updateTestLog(Action,
                ObjectName
                + (imageObjectGroup.isLeaf() ? ExceptionType.Empty_Group : ExceptionType.Not_Found_on_Screen),
                Status.FAIL);
    } catch (Exception ex) {
        Report.updateTestLog(Action, ex.getMessage(), Status.DEBUG);
        Logger.getLogger(CommonImageMethods.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example #26
Source File: PatternPaneTargetOffset.java    From SikuliX1 with MIT License 5 votes vote down vote up
void findTarget(final String patFilename, final Location initOffset) {
	new Thread(() -> {
     Region screenUnion = Region.create(0, 0, 1, 1);
		Finder f = new Finder(_simg, screenUnion);
		f.find(patFilename);

		EventQueue.invokeLater(() -> {
 			try {
 				if (f.hasNext()) {
 //TODO rewrite completely for ScreenUnion
           Screen s = (Screen) screenUnion.getScreen();
           s.setAsScreenUnion();
 					_match = f.next();
           s.setAsScreen();
 					if (initOffset != null) {
 						setTarget(initOffset.x, initOffset.y);
 					} else {
 						setTarget(0, 0);
 					}
 				}
 				_img = ImageIO.read(new File(patFilename));
 			} catch (IOException e) {
 				Debug.error(me + "Can't load " + patFilename);
 			}
 			synchronized (PatternPaneTargetOffset.this) {
 				_finding = false;
 			}
 			repaint();
		});
	}).start();
}
 
Example #27
Source File: DragAndDrop.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
/**
 * Drag and drop to the given location
 */
@Action(object = ObjectType.IMAGE, 
		desc =" Drag an image [<Object>] and drop it in a user-defined region [<Data>]", 
		input =InputType.YES)
public void imgDragandDropAt() {

    try {
        int x = Integer.valueOf(Data.split(",", -1)[0]), y = Integer
                .valueOf(Data.split(",", -1)[1]), width = Integer
                .valueOf(Data.split(",", -1)[2]), height = Integer
                .valueOf(Data.split(",", -1)[3]);
        target = findTarget(imageObjectGroup, Flag.SET_OFFSET, Flag.MATCH_ONLY);
        droptarget = Region.create(x, y, width, height);

        if (target != null) {
            if (SCREEN.dragDrop(target, droptarget) == 1) {
                Report.updateTestLog(Action,
                        "Draged image " + ObjectName + " and Dropped at given location", Status.DONE);
                return;
            }
        }
        Report.updateTestLog(Action,
                ObjectName
                + (imageObjectGroup.isLeaf() ? ExceptionType.Empty_Group : ExceptionType.Not_Found_on_Screen),
                Status.FAIL);
    } catch (Exception ex) {
        Report.updateTestLog(Action,
                ex.getMessage(), Status.DEBUG);
        Logger.getLogger(DragAndDrop.class.getName()).log(Level.SEVERE, null, ex);
    }

}
 
Example #28
Source File: AndroidRegion.java    From sikuli-monkey with MIT License 5 votes vote down vote up
@Override
public Region right(int range) {
    Rectangle bounds = getScreen().getBounds();
    Rectangle rect = new Rectangle(x + w, y, range, h);
    rect = rect.intersection(bounds);

    return newRegion(rect);
}
 
Example #29
Source File: Text.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
private void storeText(String where) {
    try {
        String varName = Data.contains(",") ? Data.split(",", -1)[0] : Data;
        if (varName.startsWith("%") && varName.endsWith("%")) {
            target = findTarget(imageObjectGroup, Flag.REGION_ONLY,
                    Flag.SET_COORDINATES, Flag.IMAGE_ONLY);
            if (target != null) {
                Region rx = getRegion((Region) target, where);
                rx.highlight(1);
                String text = rx.text();
                if (text != null) {
                    addVar(varName, text);
                    Report.updateTestLog(Action, "Text '"
                            + text + "' is stored in '" + varName + "'",
                            Status.DONE);
                    return;
                }
            }
            Report.updateTestLog(Action,
                    ObjectName
                    + (imageObjectGroup.isLeaf() ? ExceptionType.Empty_Group :ExceptionType.Not_Found_on_Screen),
                    Status.FAIL);
        } else {
            Report.updateTestLog(Action,
                    "Variable format is not correct", Status.FAIL);
        }
    } catch (Exception ex) {
        Report.updateTestLog(Action,
                ex.getMessage(), Status.DEBUG);
        Logger.getLogger(Text.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example #30
Source File: Guide.java    From SikuliX1 with MIT License 5 votes vote down vote up
public void addTracker(Pattern pattern, Region r, ArrayList<Visual> components) {
  Tracker tracker = new Tracker(this, pattern, r);
  for (Visual c : components) {
    tracker.setAnchor(c);
  }
  trackers.add(tracker);
}