Class RegionBuilder

java.lang.Object
io.github.jspinak.brobot.model.element.RegionBuilder

public class RegionBuilder extends Object
Builder for creating Region objects with screen-size awareness and flexible adjustments.

This builder provides multiple ways to define regions:

  • Absolute coordinates and dimensions
  • Percentage-based positioning relative to screen size
  • Anchor-based positioning (center, corners, edges)
  • Adjustments to existing regions
  • Screen-relative sizing with aspect ratio preservation

Example usage:


 // Create a region in the center of the screen, 50% of screen size
 Region centerRegion = new RegionBuilder()
     .withScreenPercentage(0.5, 0.5)
     .centerOnScreen()
     .build();

 // Create a region with specific coordinates
 Region searchArea = new RegionBuilder()
     .x(100).y(100)
     .width(200).height(150)
     .build();

 // Create a region with adjustments
 Region expanded = new RegionBuilder()
     .fromRegion(existingRegion)
     .adjustX(10)
     .adjustY(-5)
     .adjustWidth(20)
     .adjustHeight(20)
     .build();
 
Since:
1.2.0
  • Constructor Details

    • RegionBuilder

      public RegionBuilder()
      Creates a new RegionBuilder with current screen dimensions detected.
  • Method Details

    • withRegion

      public RegionBuilder withRegion(int x, int y, int width, int height)
      Sets the base region using absolute coordinates.
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
      width - the width
      height - the height
      Returns:
      this builder
    • fromRegion

      public RegionBuilder fromRegion(Region region)
      Creates a builder from an existing region.
      Parameters:
      region - the source region
      Returns:
      this builder
    • withPosition

      public RegionBuilder withPosition(int x, int y)
      Sets the position using absolute coordinates.
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
      Returns:
      this builder
    • withSize

      public RegionBuilder withSize(int width, int height)
      Sets the size using absolute dimensions.
      Parameters:
      width - the width
      height - the height
      Returns:
      this builder
    • withWidth

      public RegionBuilder withWidth(int width)
      Sets the width directly.
      Parameters:
      width - the width
      Returns:
      this builder
    • withHeight

      public RegionBuilder withHeight(int height)
      Sets the height directly.
      Parameters:
      height - the height
      Returns:
      this builder
    • withScreenPercentagePosition

      public RegionBuilder withScreenPercentagePosition(double xPercent, double yPercent)
      Sets the position using screen percentage (0.0 to 1.0).
      Parameters:
      xPercent - x position as percentage of screen width
      yPercent - y position as percentage of screen height
      Returns:
      this builder
    • withScreenPercentageSize

      public RegionBuilder withScreenPercentageSize(double widthPercent, double heightPercent)
      Sets the size using screen percentage (0.0 to 1.0).
      Parameters:
      widthPercent - width as percentage of screen width
      heightPercent - height as percentage of screen height
      Returns:
      this builder
    • withScreenPercentage

      public RegionBuilder withScreenPercentage(double xPercent, double yPercent, double widthPercent, double heightPercent)
      Sets both position and size using screen percentages.
      Parameters:
      xPercent - x position as percentage
      yPercent - y position as percentage
      widthPercent - width as percentage
      heightPercent - height as percentage
      Returns:
      this builder
    • withScreenPercentage

      public RegionBuilder withScreenPercentage(double widthPercent, double heightPercent)
      Convenience method to set size using screen percentage.
      Parameters:
      widthPercent - width as percentage of screen
      heightPercent - height as percentage of screen
      Returns:
      this builder
    • adjustX

      public RegionBuilder adjustX(int adjustment)
      Adds an adjustment to the x-coordinate.
      Parameters:
      adjustment - pixels to add (negative to subtract)
      Returns:
      this builder
    • adjustY

      public RegionBuilder adjustY(int adjustment)
      Adds an adjustment to the y-coordinate.
      Parameters:
      adjustment - pixels to add (negative to subtract)
      Returns:
      this builder
    • adjustWidth

      public RegionBuilder adjustWidth(int adjustment)
      Adds an adjustment to the width.
      Parameters:
      adjustment - pixels to add (negative to subtract)
      Returns:
      this builder
    • adjustHeight

      public RegionBuilder adjustHeight(int adjustment)
      Adds an adjustment to the height.
      Parameters:
      adjustment - pixels to add (negative to subtract)
      Returns:
      this builder
    • adjustBy

      public RegionBuilder adjustBy(int x, int y, int width, int height)
      Adds adjustments to all dimensions.
      Parameters:
      x - x adjustment
      y - y adjustment
      width - width adjustment
      height - height adjustment
      Returns:
      this builder
    • expand

      public RegionBuilder expand(int pixels)
      Expands or contracts the region by the specified amount on all sides.
      Parameters:
      pixels - pixels to expand (negative to contract)
      Returns:
      this builder
    • withAnchor

      public RegionBuilder withAnchor(Positions.Name anchorName)
      Sets the anchor point using Positions.Name enum.
      Parameters:
      anchorName - the anchor point from Positions.Name
      Returns:
      this builder
    • withAnchor

      public RegionBuilder withAnchor(Position position)
      Sets the anchor point using a custom Position.
      Parameters:
      position - the custom position for anchoring (0.0 to 1.0 relative positioning)
      Returns:
      this builder
    • positionRelativeTo

      public RegionBuilder positionRelativeTo(Region referenceRegion, Position position)
      Positions this region relative to another region using a Position. The Position determines where in the reference region this region's anchor point will be placed.
      Parameters:
      referenceRegion - the region to position relative to
      position - the position within the reference region (0.0 to 1.0 relative positioning)
      Returns:
      this builder
    • positionRelativeTo

      public RegionBuilder positionRelativeTo(Region referenceRegion, Positions.Name positionName)
      Positions this region relative to another region using a named position.
      Parameters:
      referenceRegion - the region to position relative to
      positionName - the named position from Positions.Name
      Returns:
      this builder
    • withPosition

      public RegionBuilder withPosition(Position position)
      Creates a region at a specific position within the screen using Position percentages.
      Parameters:
      position - the position defining where to place the region (0.0 to 1.0 scale)
      Returns:
      this builder
    • withPosition

      public RegionBuilder withPosition(Positions.Name positionName)
      Creates a region at a named position on the screen.
      Parameters:
      positionName - the named position from Positions.Name
      Returns:
      this builder
    • centerOnScreen

      public RegionBuilder centerOnScreen()
      Centers the region on the screen.
      Returns:
      this builder
    • topLeft

      public RegionBuilder topLeft()
      Positions the region at the top-left of the screen.
      Returns:
      this builder
    • topRight

      public RegionBuilder topRight()
      Positions the region at the top-right of the screen.
      Returns:
      this builder
    • bottomLeft

      public RegionBuilder bottomLeft()
      Positions the region at the bottom-left of the screen.
      Returns:
      this builder
    • bottomRight

      public RegionBuilder bottomRight()
      Positions the region at the bottom-right of the screen.
      Returns:
      this builder
    • topCenter

      public RegionBuilder topCenter()
      Positions the region at the top-center of the screen.
      Returns:
      this builder
    • bottomCenter

      public RegionBuilder bottomCenter()
      Positions the region at the bottom-center of the screen.
      Returns:
      this builder
    • leftCenter

      public RegionBuilder leftCenter()
      Positions the region at the left-center of the screen.
      Returns:
      this builder
    • rightCenter

      public RegionBuilder rightCenter()
      Positions the region at the right-center of the screen.
      Returns:
      this builder
    • constrainToScreen

      public RegionBuilder constrainToScreen(boolean constrain)
      Ensures the region stays within screen bounds.
      Parameters:
      constrain - true to constrain to screen
      Returns:
      this builder
    • maintainAspectRatio

      public RegionBuilder maintainAspectRatio(boolean maintain)
      Maintains the aspect ratio when resizing.
      Parameters:
      maintain - true to maintain aspect ratio
      Returns:
      this builder
    • fullScreen

      public RegionBuilder fullScreen()
      Creates a region covering the full screen.
      Returns:
      this builder
    • topHalf

      public RegionBuilder topHalf()
      Creates a region for the top half of the screen.
      Returns:
      this builder
    • bottomHalf

      public RegionBuilder bottomHalf()
      Creates a region for the bottom half of the screen.
      Returns:
      this builder
    • leftHalf

      public RegionBuilder leftHalf()
      Creates a region for the left half of the screen.
      Returns:
      this builder
    • rightHalf

      public RegionBuilder rightHalf()
      Creates a region for the right half of the screen.
      Returns:
      this builder
    • build

      public Region build()
      Builds the Region with all specified parameters.
      Returns:
      the constructed Region