Class Dimension

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class Dimension
    extends java.lang.Object
    implements java.lang.Cloneable, java.io.Serializable
    A Dimension represents a width and a height in 2-dimensional space. It provides various methods for manipulating the Dimension or creating new derived objects.
    See Also:
    Serialized Form
    • Field Detail

      • width

        public double width
        This Dimension's width. It is the distance on the x-axis that is stored in this Dimension.
      • height

        public double height
        This Dimension's height. It is the distance of the y-axis that is stored in this Dimension.
    • Constructor Detail

      • Dimension

        public Dimension()
        Constructs a Dimension of zero width and height.
      • Dimension

        public Dimension​(Dimension d)
        Constructs a Dimension with the width and height of the passed-in Dimension.
        Parameters:
        d - the Dimension supplying the initial width and height values
      • Dimension

        public Dimension​(double w,
                         double h)
        Constructs a Dimension with the supplied width and height values.
        Parameters:
        w - the width of the new Dimension
        h - the height of the new Dimension
    • Method Detail

      • clone

        public Dimension clone()
        Overrides:
        clone in class java.lang.Object
      • equals

        public boolean equals​(double width,
                              double height)
        Returns true if this Dimension's width and height are equal to the given width and height.
        Parameters:
        width - the width
        height - the height
        Returns:
        true if this dimension's width and height are equal to those given.
      • equals

        public boolean equals​(java.lang.Object o)
        Returns whether the input Object is equivalent to this Dimension. true if the Object is a Dimension and its width and height are equal to this Dimension's width and height, false otherwise.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - the Object being tested for equality
        Returns:
        true if the given object is equal to this dimension
      • expand

        public Dimension expand​(Dimension d)
        Expands the size of this Dimension by the specified amount.
        Parameters:
        d - the Dimension providing the expansion width and height
        Returns:
        this for convenience
      • expand

        public Dimension expand​(double w,
                                double h)
        Expands the size of this Dimension by the specified width and height.
        Parameters:
        w - Value by which the width should be increased
        h - Value by which the height should be increased
        Returns:
        this for convenience
      • getCopy

        public Dimension getCopy()
        Creates and returns a copy of this Dimension.
        Returns:
        a copy of this Dimension
      • getExpanded

        public Dimension getExpanded​(Dimension d)
        Creates and returns a Dimension representing the sum of this Dimension and the one specified.
        Parameters:
        d - the dimension providing the expansion width and height
        Returns:
        a new dimension expanded by d
      • getExpanded

        public Dimension getExpanded​(double w,
                                     double h)
        Creates and returns a new Dimension representing the sum of this Dimension and the one specified.
        Parameters:
        w - value by which the width of this is to be expanded
        h - value by which the height of this is to be expanded
        Returns:
        a new Dimension expanded by the given values
      • getHeight

        public double getHeight()
        Returns the height of this dimension.
        Returns:
        The current height
      • getIntersected

        public Dimension getIntersected​(Dimension d)
        Creates and returns a new Dimension representing the intersection of this Dimension and the one specified.
        Parameters:
        d - the Dimension to intersect with
        Returns:
        A new Dimension representing the intersection
      • getNegated

        public Dimension getNegated()
        Creates and returns a new Dimension with negated values.
        Returns:
        a new Dimension with negated values
      • getScaled

        public Dimension getScaled​(double amount)
        Creates a new Dimension with its width and height scaled by the specified value.
        Parameters:
        amount - Value by which the width and height are scaled
        Returns:
        a new dimension with the scale applied
      • getScaled

        public Dimension getScaled​(double widthFactor,
                                   double heightFactor)
        Creates a new Dimension with its width and height scaled by the specified values.
        Parameters:
        widthFactor - the value by which the width is to be scaled
        heightFactor - the value by which the height is to be scaled
        Returns:
        a new dimension with the scale applied
      • getShrinked

        public Dimension getShrinked​(Dimension d)
        Creates and returns a new Dimension whose size will be reduced by the width and height of the given Dimension.
        Parameters:
        d - the dimension whose width and height values will be considered
        Returns:
        a new dimension representing the difference
      • getShrinked

        public Dimension getShrinked​(double w,
                                     double h)
        Creates and returns a new Dimension whose size will be reduced by the given width and height.
        Parameters:
        w - the value by which the width is to be reduced
        h - the value by which the height is to be reduced
        Returns:
        a new dimension representing the difference
      • getTransposed

        public Dimension getTransposed()
        Creates a new Dimension with its height and width swapped. Useful in orientation change calculations.
        Returns:
        a new Dimension with its height and width swapped
      • getUnioned

        public Dimension getUnioned​(Dimension d)
        Creates a new Dimension representing the union of this Dimension with the one specified. Union is defined as the max() of the values from each Dimension.
        Parameters:
        d - the Dimension to be unioned
        Returns:
        a new Dimension
      • getWidth

        public double getWidth()
        Returns the width of this dimension
        Returns:
        the current width of this dimension
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Object.hashCode()
      • intersect

        public Dimension intersect​(Dimension d)
        This Dimension is intersected with the one specified. Intersection is performed by taking the min() of the values from each dimension.
        Parameters:
        d - the Dimension used to perform the min()
        Returns:
        this for convenience
      • isEmpty

        public boolean isEmpty()
        Returns true if either dimension is less than or equal to 0.
        Returns:
        true if either dimension is less than or equal to 0.
      • negate

        public Dimension negate()
        Negates the width and height of this Dimension.
        Returns:
        this for convenience
      • scale

        public Dimension scale​(double factor)
        Scales the width and height of this Dimension by the amount supplied, and returns this for convenience.
        Parameters:
        factor - value by which this Dimension's width and height are to be scaled
        Returns:
        this for convenience
      • scale

        public Dimension scale​(double widthFactor,
                               double heightFactor)
        Scales the width of this Dimension by w and scales the height of this Dimension by h. Returns this for convenience.
        Parameters:
        widthFactor - the value by which the width is to be scaled
        heightFactor - the value by which the height is to be scaled
        Returns:
        this for convenience
      • setHeight

        public Dimension setHeight​(double height)
        Sets the height of this Rectangle to the specified one.
        Parameters:
        height - The new height
        Returns:
        this for convenience
      • setSize

        public Dimension setSize​(Dimension d)
        Copies the width and height values of the input Dimension to this Dimension.
        Parameters:
        d - the dimension supplying the values
        Returns:
        this for convenience
      • setSize

        public Dimension setSize​(double w,
                                 double h)
        Sets the size of this dimension to the specified width and height.
        Parameters:
        w - The new width
        h - The new height
        Returns:
        this for convenience
      • setWidth

        public Dimension setWidth​(double width)
        Sets the width of this Rectangle to the specified one.
        Parameters:
        width - The new width
        Returns:
        this for convenience
      • shrink

        public Dimension shrink​(Dimension d)
        Shrinks the size of this Dimension by the width and height values of the given Dimension.
        Parameters:
        d - The dimension whose width and height values are to be used
        Returns:
        this for convenience
      • shrink

        public Dimension shrink​(double w,
                                double h)
        Reduces the width of this Dimension by w, and reduces the height of this Dimension by h. Returns this for convenience.
        Parameters:
        w - the value by which the width is to be reduced
        h - the value by which the height is to be reduced
        Returns:
        this for convenience
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        See Also:
        Object.toString()
      • transpose

        public Dimension transpose()
        Swaps the width and height of this Dimension, and returns this for convenience. Can be useful in orientation changes.
        Returns:
        this for convenience
      • union

        public Dimension union​(Dimension d)
        Sets the width of this Dimension to the greater of this Dimension's width and d.width. Likewise for this Dimension's height.
        Parameters:
        d - the Dimension to union with this Dimension
        Returns:
        this for convenience