Class Size


  • public class Size
    extends java.lang.Object
    Defines the dimensions of an image, label or other screen-space item relative to a container (for example, the viewport). A size contains a width, a height, a width size mode, a height size mode, and for each of these a "units" string indicating the coordinate units.

    The possible size modes are:

    • NATIVE_DIMENSION - Maintain the native dimensions.
    • MAINTAIN_ASPECT_RATIO - Maintain the aspect ratio of the image when one dimension is specified and the other is not.
    • EXPLICIT_DIMENSION - Use an explicit dimension. This dimension may be either an absolute pixel value, or a fraction of the container.

    Recognized units are AVKey.PIXELS, which indicates pixel units relative to the lower left corner of the image, or AVKey.FRACTION, which indicates the units are fractions of the image width and height.

    Examples:

     Width mode      Height mode      Width (Units)      Height (Units)        Result
     --------------------------------------------------------------------------------------------------------------------
     Native          Native           N/A                N/A                   Keep native dimensions
     Aspect ratio    Explicit         N/A                100 (pix)             Scale image so that height is 100 pixels,
                                                                               but maintain aspect ratio
     Explicit        Aspect ratio     0.5 (fraction)     N/A                   Make the width half of the container, and
                                                                               scale height to maintain aspect ratio
     Explicit        Native           1.0 (fraction)     N/A                   Stretch the image to fill the width of the
                                                                               container, but do not scale the height.
     
    This class implements the functionality of a KML size.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String EXPLICIT_DIMENSION
      Size mode to indicate that the size parameter indicates an explicit dimension measured either in pixels, or as a fraction of the container.
      protected java.lang.String heightMode
      Size mode for height.
      protected double heightParam
      Height size parameter.
      protected java.lang.String heightUnits
      Units of height.
      static java.lang.String MAINTAIN_ASPECT_RATIO
      Size mode to indicate that the content's aspect ratio must be maintained.
      static java.lang.String NATIVE_DIMENSION
      Size mode to indicate that the content's native dimension must be used.
      protected java.lang.String widthMode
      Size mode for width.
      protected double widthParam
      Width size parameter.
      protected java.lang.String widthUnits
      Units of width.
    • Constructor Summary

      Constructors 
      Constructor Description
      Size()
      Create a Size object that will preserve native dimensions.
      Size​(java.lang.String widthMode, double widthParam, java.lang.String widthUnits, java.lang.String heightMode, double heightParam, java.lang.String heightUnits)
      Create a Size with specified dimensions.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.awt.Dimension compute​(int rectWidth, int rectHeight, int containerWidth, int containerHeight)
      Computes the width and height of a rectangle within a container rectangle.
      protected double computeSize​(double size, java.lang.String units, double containerDimension)
      Compute a dimension taking into account the units of the dimension.
      protected java.lang.String convertLegacyModeString​(java.lang.String string)
      Converts a legacy size mode string ("NativeDimension", "MaintainAspectRatio", "ExplicitDimension"), into one of the mode constants (NATIVE_DIMENSION, MAINTAIN_ASPECT_RATIO, or EXPLICIT_DIMENSION).
      boolean equals​(java.lang.Object o)  
      static Size fromFraction​(double widthFraction, double heightFraction)
      Creates a new size from explicit fraction dimensions.
      static Size fromPixels​(int widthInPixels, int heightInPixels)
      Create a size from explicit pixel dimensions.
      double getHeight()
      Get the unscaled height.
      java.lang.String getHeightMode()
      Get the mode of the height dimension.
      java.lang.String getHeightUnits()
      Returns the units of the offset Y value.
      void getRestorableState​(RestorableSupport restorableSupport, RestorableSupport.StateObject context)
      Saves the size's current state in the specified restorableSupport.
      double getWidth()
      Get the unscaled width.
      java.lang.String getWidthMode()
      Get the mode of the width dimension.
      java.lang.String getWidthUnits()
      Returns the units of the offset X value.
      int hashCode()  
      void restoreState​(RestorableSupport restorableSupport, RestorableSupport.StateObject context)
      Restores the state of any size parameters contained in the specified RestorableSupport.
      void setHeight​(java.lang.String mode, double height, java.lang.String units)
      Set the height.
      void setWidth​(java.lang.String mode, double width, java.lang.String units)
      Set the width.
      • Methods inherited from class java.lang.Object

        clone, getClass, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NATIVE_DIMENSION

        public static final java.lang.String NATIVE_DIMENSION
        Size mode to indicate that the content's native dimension must be used.
        See Also:
        Constant Field Values
      • MAINTAIN_ASPECT_RATIO

        public static final java.lang.String MAINTAIN_ASPECT_RATIO
        Size mode to indicate that the content's aspect ratio must be maintained.
        See Also:
        Constant Field Values
      • EXPLICIT_DIMENSION

        public static final java.lang.String EXPLICIT_DIMENSION
        Size mode to indicate that the size parameter indicates an explicit dimension measured either in pixels, or as a fraction of the container.
        See Also:
        Constant Field Values
      • widthUnits

        protected java.lang.String widthUnits
        Units of width.
      • widthParam

        protected double widthParam
        Width size parameter.
      • heightUnits

        protected java.lang.String heightUnits
        Units of height.
      • heightParam

        protected double heightParam
        Height size parameter.
    • Method Detail

      • fromPixels

        public static Size fromPixels​(int widthInPixels,
                                      int heightInPixels)
        Create a size from explicit pixel dimensions.
        Parameters:
        widthInPixels - Width of rectangle in pixels.
        heightInPixels - Height of rectangle in pixels.
        Returns:
        New size object.
      • fromFraction

        public static Size fromFraction​(double widthFraction,
                                        double heightFraction)
        Creates a new size from explicit fraction dimensions.
        Parameters:
        widthFraction - the size's width as a fraction of the containing rectangle.
        heightFraction - the size's height as a fraction of the containing rectangle.
        Returns:
        a new size with the specified width and height.
      • getWidthUnits

        public java.lang.String getWidthUnits()
        Returns the units of the offset X value. See setWidth(String, double, String) for a description of the recognized values.
        Returns:
        the units of the offset X value, or null.
      • getHeightUnits

        public java.lang.String getHeightUnits()
        Returns the units of the offset Y value. See setHeight(String, double, String) for a description of the recognized values.
        Returns:
        the units of the offset Y value, or null.
      • getWidth

        public double getWidth()
        Get the unscaled width.
        Returns:
        Unscaled width. The units of this value depend on the current height units.
        See Also:
        getWidthMode(), getWidthUnits()
      • getHeight

        public double getHeight()
        Get the unscaled height.
        Returns:
        Unscaled height. The units of this value depend on the current height units.
        See Also:
        getHeightMode(), getHeightUnits()
      • compute

        public java.awt.Dimension compute​(int rectWidth,
                                          int rectHeight,
                                          int containerWidth,
                                          int containerHeight)
        Computes the width and height of a rectangle within a container rectangle.
        Parameters:
        rectWidth - The width of the rectangle to size.
        rectHeight - The height of the rectangle to size.
        containerWidth - The width of the container.
        containerHeight - The height of the container.
        Returns:
        The desired image dimensions.
      • computeSize

        protected double computeSize​(double size,
                                     java.lang.String units,
                                     double containerDimension)
        Compute a dimension taking into account the units of the dimension.
        Parameters:
        size - The size parameter.
        units - One of AVKey.PIXELS or AVKey.FRACTION. If the units value is not one of the expected options, AVKey.PIXELS is used as the default.
        containerDimension - The viewport dimension.
        Returns:
        Size in pixels
      • restoreState

        public void restoreState​(RestorableSupport restorableSupport,
                                 RestorableSupport.StateObject context)
        Restores the state of any size parameters contained in the specified RestorableSupport. If the StateObject is not null it's searched for state values, otherwise the RestorableSupport root is searched.
        Parameters:
        restorableSupport - the RestorableSupport that contains the size's state.
        context - the StateObject to search for state values, if not null.
        Throws:
        java.lang.IllegalArgumentException - if restorableSupport is null.
      • convertLegacyModeString

        protected java.lang.String convertLegacyModeString​(java.lang.String string)
        Converts a legacy size mode string ("NativeDimension", "MaintainAspectRatio", "ExplicitDimension"), into one of the mode constants (NATIVE_DIMENSION, MAINTAIN_ASPECT_RATIO, or EXPLICIT_DIMENSION). Returns the input string unmodified if the input does not match a legacy size mode.
        Parameters:
        string - the legacy size mode String to convert to a size mode.
        Returns:
        a size mode constant, or the input string if string is not a legacy size mode.
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object