Class Position

java.lang.Object
swingtree.layout.Position

@Immutable public final class Position extends Object
An immutable value based class that represents a location in a two-dimensional coordinate system specified in float precision and specifically designed for Swing components. It can be used as an alternative to the AWT Point class, but in situations where immutability is desired (which should be most cases).

Use the of(float, float) factory method to create a new instance or withX(int) and withY(int) to create a new instance with a modified value.

  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    int
     
    minus(float dx, float dy)
    Creates a new location where the specified dx and dy values are subtracted from the x- and y-coordinates of this location.
    minus(Position other)
    Creates a new location where the x- and y-coordinates of the specified Position are subtracted from the x- and y-coordinates of this location.
    static Position
    of(float x, float y)
    A factory method that creates a new location with the specified x- and y-coordinates or returns the origin() constant if both coordinates are zero.
    static Position
    of(Point p)
    A factory method that creates a new location from the supplied AWT Point.
    static Position
     
    plus(float dx, float dy)
    Creates a new location where the specified dx and dy values are added to the x- and y-coordinates of this location.
    plus(Position other)
    Creates a new location where the x- and y-coordinates of the specified Position are added to the x- and y-coordinates of this location.
    A Position consists of two x and y coordinates in 2D space, which is why this convenience method allows you to transform this Position object to an AWT Point.
     
    withX(int x)
    Allows you to create an updated version of this location with the specified x-coordinate and the same y-coordinate as this location instance.
    withY(int y)
    Allows you to create an updated version of this location with the specified y-coordinate and the same x-coordinate as this location instance.
    float
    x()
    Exposes the x coordinate of this location in the form of a float, which describes the horizontal position in a two-dimensional coordinate system.
    float
    y()
    Exposes the y coordinate of this location in the form of a float, which describes the vertical position in a two-dimensional coordinate system.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • of

      public static Position of(float x, float y)
      A factory method that creates a new location with the specified x- and y-coordinates or returns the origin() constant if both coordinates are zero.
      Parameters:
      x - The x-coordinate of the location to create.
      y - The y-coordinate of the location to create.
      Returns:
      A new location with the specified x- and y-coordinates. If both coordinates are zero, the origin() is returned.
    • of

      public static Position of(Point p)
      A factory method that creates a new location from the supplied AWT Point. If the point is null, a NullPointerException is thrown.
      Parameters:
      p - The point to create a location from.
      Returns:
      A new location with the x- and y-coordinates of the specified point. If both coordinates are zero, the origin() is returned.
    • origin

      public static Position origin()
    • x

      public float x()
      Exposes the x coordinate of this location in the form of a float, which describes the horizontal position in a two-dimensional coordinate system. So the larger the x value of the location, the further to the right it is.
      Returns:
      The x-coordinate of this location.
    • y

      public float y()
      Exposes the y coordinate of this location in the form of a float, which describes the vertical position in a two-dimensional coordinate system. So the larger the y value of the location, the further down it is.
      Returns:
      The y-coordinate of this location.
    • withY

      public Position withY(int y)
      Allows you to create an updated version of this location with the specified y-coordinate and the same x-coordinate as this location instance.
      Parameters:
      y - The y-coordinate of the location to create.
      Returns:
      A new location with the same x-coordinate as this location and the specified y-coordinate.
    • withX

      public Position withX(int x)
      Allows you to create an updated version of this location with the specified x-coordinate and the same y-coordinate as this location instance.
      Parameters:
      x - The x-coordinate of the location to create.
      Returns:
      A new location with the same y-coordinate as this location and the specified x-coordinate.
    • plus

      public Position plus(float dx, float dy)
      Creates a new location where the specified dx and dy values are added to the x- and y-coordinates of this location.
      Parameters:
      dx - The amount to increase the x-coordinate by.
      dy - The amount to increase the y-coordinate by.
      Returns:
      A new location with the x- and y-coordinates of this location increased by the specified values.
    • plus

      public Position plus(Position other)
      Creates a new location where the x- and y-coordinates of the specified Position are added to the x- and y-coordinates of this location.
      Parameters:
      other - The location to add to this location.
      Returns:
      A new location with the x- and y-coordinates of this location increased by the x- and y-coordinates of the specified location.
    • minus

      public Position minus(float dx, float dy)
      Creates a new location where the specified dx and dy values are subtracted from the x- and y-coordinates of this location.
      Parameters:
      dx - The amount to decrease the x-coordinate by.
      dy - The amount to decrease the y-coordinate by.
      Returns:
      A new location with the x- and y-coordinates of this location decreased by the specified values.
    • minus

      public Position minus(Position other)
      Creates a new location where the x- and y-coordinates of the specified Position are subtracted from the x- and y-coordinates of this location.
      Parameters:
      other - The location to subtract from this location.
      Returns:
      A new location with the x- and y-coordinates of this location decreased by the x- and y-coordinates of the specified location.
    • toPoint

      public Point toPoint()
      A Position consists of two x and y coordinates in 2D space, which is why this convenience method allows you to transform this Position object to an AWT Point.
      Returns:
      A new AWT Point with the same x- and y-coordinates as this location.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object