Package gov.nasa.worldwind.render
Class Size
- java.lang.Object
-
- gov.nasa.worldwind.render.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, orAVKey.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 modestring
("NativeDimension", "MaintainAspectRatio", "ExplicitDimension"), into one of the mode constants (NATIVE_DIMENSION
,MAINTAIN_ASPECT_RATIO
, orEXPLICIT_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 specifiedrestorableSupport
.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 specifiedRestorableSupport
.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.
-
-
-
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
-
widthMode
protected java.lang.String widthMode
-
widthUnits
protected java.lang.String widthUnits
Units of width.
-
widthParam
protected double widthParam
Width size parameter.
-
heightMode
protected java.lang.String heightMode
-
heightUnits
protected java.lang.String heightUnits
Units of height.
-
heightParam
protected double heightParam
Height size parameter.
-
-
Constructor Detail
-
Size
public Size()
Create a Size object that will preserve native dimensions.
-
Size
public 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.- Parameters:
widthMode
- Width mode, one ofNATIVE_DIMENSION
,MAINTAIN_ASPECT_RATIO
, orEXPLICIT_DIMENSION
.widthParam
- The width (applies only toEXPLICIT_DIMENSION
mode).widthUnits
- Units ofwidth
. EitherAVKey.PIXELS
orAVKey.PIXELS
.heightMode
- height mode, one ofNATIVE_DIMENSION
,MAINTAIN_ASPECT_RATIO
, orEXPLICIT_DIMENSION
.heightParam
- The height (applies only toEXPLICIT_DIMENSION
mode).heightUnits
- Units ofheight
. EitherAVKey.PIXELS
orAVKey.PIXELS
.- See Also:
setWidth(String, double, String)
,setHeight(String, double, String)
-
-
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.
-
setWidth
public void setWidth(java.lang.String mode, double width, java.lang.String units)
Set the width.- Parameters:
mode
- Width mode, one ofNATIVE_DIMENSION
,MAINTAIN_ASPECT_RATIO
, orEXPLICIT_DIMENSION
.width
- The width (applies only toEXPLICIT_DIMENSION
mode).units
- Units ofwidth
. EitherAVKey.PIXELS
orAVKey.PIXELS
.- Throws:
java.lang.IllegalArgumentException
- ifmode
is null.
-
setHeight
public void setHeight(java.lang.String mode, double height, java.lang.String units)
Set the height.- Parameters:
mode
- Width mode, one ofNATIVE_DIMENSION
,MAINTAIN_ASPECT_RATIO
, orEXPLICIT_DIMENSION
.height
- The width (applies only toEXPLICIT_DIMENSION
mode).units
- Units ofwidth
. EitherAVKey.PIXELS
orAVKey.FRACTION
.- Throws:
java.lang.IllegalArgumentException
- ifmode
is null.
-
getWidthUnits
public java.lang.String getWidthUnits()
Returns the units of the offset X value. SeesetWidth(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. SeesetHeight(String, double, String)
for a description of the recognized values.- Returns:
- the units of the offset Y value, or null.
-
getWidthMode
public java.lang.String getWidthMode()
Get the mode of the width dimension.- Returns:
- Width mode, one of
NATIVE_DIMENSION
,MAINTAIN_ASPECT_RATIO
, orEXPLICIT_DIMENSION
.
-
getHeightMode
public java.lang.String getHeightMode()
Get the mode of the height dimension.- Returns:
- Height mode, one of
NATIVE_DIMENSION
,MAINTAIN_ASPECT_RATIO
, orEXPLICIT_DIMENSION
.
-
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 ofAVKey.PIXELS
orAVKey.FRACTION
. If theunits
value is not one of the expected options,AVKey.PIXELS
is used as the default.containerDimension
- The viewport dimension.- Returns:
- Size in pixels
-
getRestorableState
public void getRestorableState(RestorableSupport restorableSupport, RestorableSupport.StateObject context)
Saves the size's current state in the specifiedrestorableSupport
. Ifcontext
is notnull
, the state is appended to it. Otherwise the state is added to theRestorableSupport
root. This state can be restored later by callingrestoreState(gov.nasa.worldwind.util.RestorableSupport, gov.nasa.worldwind.util.RestorableSupport.StateObject)
.- Parameters:
restorableSupport
- theRestorableSupport
that receives the size's state.context
- theStateObject
the state is appended to, if notnull
.- Throws:
java.lang.IllegalArgumentException
- ifrestorableSupport
isnull
.
-
restoreState
public void restoreState(RestorableSupport restorableSupport, RestorableSupport.StateObject context)
Restores the state of any size parameters contained in the specifiedRestorableSupport
. If theStateObject
is notnull
it's searched for state values, otherwise theRestorableSupport
root is searched.- Parameters:
restorableSupport
- theRestorableSupport
that contains the size's state.context
- theStateObject
to search for state values, if notnull
.- Throws:
java.lang.IllegalArgumentException
- ifrestorableSupport
isnull
.
-
convertLegacyModeString
protected java.lang.String convertLegacyModeString(java.lang.String string)
Converts a legacy size modestring
("NativeDimension", "MaintainAspectRatio", "ExplicitDimension"), into one of the mode constants (NATIVE_DIMENSION
,MAINTAIN_ASPECT_RATIO
, orEXPLICIT_DIMENSION
). Returns the input string unmodified if the input does not match a legacy size mode.- Parameters:
string
- the legacy size modeString
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 classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-