Class Location

java.lang.Object
swingtree.layout.Location

@Immutable public final class Location 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
     
    static Location
    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 Location
    of(Point p)
    A factory method that creates a new location from the supplied AWT Point.
    static Location
     
    A Location consists of two x and y coordinates in 2D space, which is why this convenience method allows you to transform this Location 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 Location 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 Location 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 Location 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 Location 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 Location 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.
    • toPoint

      public Point toPoint()
      A Location consists of two x and y coordinates in 2D space, which is why this convenience method allows you to transform this Location 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