Package ca.cgjennings.graphics
Class MultiResolutionImageResource
- java.lang.Object
-
- java.awt.Image
-
- java.awt.image.AbstractMultiResolutionImage
-
- ca.cgjennings.graphics.MultiResolutionImageResource
-
- All Implemented Interfaces:
java.awt.image.MultiResolutionImage
public class MultiResolutionImageResource extends java.awt.image.AbstractMultiResolutionImage
An image loaded from a resource URL that can support multiple resolutions. Multiple source images may be provided using the familiar @Nx notation, and the best version for the drawing context will be used automatically. For example, you could provide resource files namedreload.png
,reload@2x.png
, andreload@3x.png
, and when drawn to a graphics context the most appropriate version will be selected automatically. Missing images will be filled in by scaling from an image that is available. The following "@Nx" values are currently supported: "@1.25x", "@1.5x", "@1.75x", "@2x", "@2.25x", "@2.5x", "@3x", "@3.5x", "@4x", "@8x", "@16x"- Since:
- 3.4
- Author:
- Chris Jennings
-
-
Constructor Summary
Constructors Constructor Description MultiResolutionImageResource(java.lang.String baseResource)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.awt.image.BufferedImage
getBaseImage()
protected java.awt.image.BufferedImage
getImageVariant(int variant, boolean cache)
Returns the best image to use for the requested scale variant.java.awt.image.BufferedImage
getResolutionVariant(double destImageWidth, double destImageHeight)
java.util.List<java.awt.Image>
getResolutionVariants()
java.lang.String
getResource()
Returns the resource path used to create this image.protected java.lang.String
getResourcePathForVariant(float scale, java.lang.String tag)
Given a string describing the variant, such as"@2x"
, returns the resource path that would contain that variant, if it exists.boolean
isFixedScaleAssumed()
protected java.awt.image.BufferedImage
loadVariantResource(int variant)
If there is an "@Nx" version of the image available, load and return it.void
setFixedScaleAssumed(boolean isFixed)
Sets whether the image should assume that the image will be requested at certain fixed scales repeatedly.protected java.awt.image.BufferedImage
synthesizeVariant(int variant)
java.lang.String
toString()
Returns a string description of this image, for debugging purposes.-
Methods inherited from class java.awt.image.AbstractMultiResolutionImage
getGraphics, getHeight, getProperty, getSource, getWidth
-
-
-
-
Method Detail
-
getResource
public java.lang.String getResource()
Returns the resource path used to create this image.- Returns:
- the original resource path with no resolution tags
-
setFixedScaleAssumed
public final void setFixedScaleAssumed(boolean isFixed)
Sets whether the image should assume that the image will be requested at certain fixed scales repeatedly. This default is to assume that this is true. For example, this assumption is true when the image will be painted to a UI that is being scaled on a high DPI display, since the image will be requested repeatedly at the fixed DPI scaling factor.When a scale is requested for which no "@Nx" variant exists, an alternative image that does exist will be chosen instead. When this property is true, the alternative image will be scaled to the exact size that the variant image would be if it existed, and this pre-scaled image may be retained to speed up future requests for the same image. Setting this to false may reduce the memory footprint of this instance at the possible cost of slower and/or lower quality results when rendering the image.
- Parameters:
isFixed
- if true, it is assumed that images at certain fixed scales will be requested repeatedly
-
isFixedScaleAssumed
public final boolean isFixedScaleAssumed()
-
getResourcePathForVariant
protected java.lang.String getResourcePathForVariant(float scale, java.lang.String tag)
Given a string describing the variant, such as"@2x"
, returns the resource path that would contain that variant, if it exists. Subclasses can override this to modify how the variant images are located.
-
loadVariantResource
protected java.awt.image.BufferedImage loadVariantResource(int variant)
If there is an "@Nx" version of the image available, load and return it. Otherwise return null.
-
synthesizeVariant
protected java.awt.image.BufferedImage synthesizeVariant(int variant)
-
getImageVariant
protected java.awt.image.BufferedImage getImageVariant(int variant, boolean cache)
Returns the best image to use for the requested scale variant.- Parameters:
variant
- the index of the desired "@Nx" variantcache
- if true, and the image must be loaded or generated, then it will be stored in an internal cache for fast future access- Returns:
- the non-null variant image for the requested scale index
-
getBaseImage
public java.awt.image.BufferedImage getBaseImage()
- Specified by:
getBaseImage
in classjava.awt.image.AbstractMultiResolutionImage
-
getResolutionVariant
public java.awt.image.BufferedImage getResolutionVariant(double destImageWidth, double destImageHeight)
-
getResolutionVariants
public java.util.List<java.awt.Image> getResolutionVariants()
-
toString
public java.lang.String toString()
Returns a string description of this image, for debugging purposes.- Overrides:
toString
in classjava.lang.Object
-
-