Package swingtree.layout
Class Position
java.lang.Object
swingtree.layout.Position
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 TypeMethodDescriptionboolean
int
hashCode()
minus
(float dx, float dy) Creates a new location where the specifieddx
anddy
values are subtracted from the x- and y-coordinates of this location.Creates a new location where the x- and y-coordinates of the specifiedPosition
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 theorigin()
constant if both coordinates are zero.static Position
A factory method that creates a new location from the supplied AWTPoint
.static Position
origin()
plus
(float dx, float dy) Creates a new location where the specifieddx
anddy
values are added to the x- and y-coordinates of this location.Creates a new location where the x- and y-coordinates of the specifiedPosition
are added to the x- and y-coordinates of this location.toPoint()
toString()
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.
-
Method Details
-
of
A factory method that creates a new location with the specified x- and y-coordinates or returns theorigin()
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
A factory method that creates a new location from the supplied AWTPoint
. If the point is null, aNullPointerException
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
-
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
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
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
Creates a new location where the specifieddx
anddy
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
Creates a new location where the x- and y-coordinates of the specifiedPosition
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
Creates a new location where the specifieddx
anddy
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
Creates a new location where the x- and y-coordinates of the specifiedPosition
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
APosition
consists of two x and y coordinates in 2D space, which is why this convenience method allows you to transform thisPosition
object to an AWTPoint
.- Returns:
- A new AWT
Point
with the same x- and y-coordinates as this location.
-
toString
-
equals
-
hashCode
public int hashCode()
-