Class ScalableImageIcon
- All Implemented Interfaces:
ResizableIcon
,Serializable
,Cloneable
,Accessible
,Icon
- Direct Known Subclasses:
DoubleSizeImageIcon
,ResizableIconImageIcon
This class dynamically chooses a right image by current screen dpi and/or scaling factor defined in the system (Retina and etc). If scaling is needed and svg icon with the same name is available at given location, svg icon will be used instead of a bitmap icon.
Dynamic choosing will work only if two icons are provided at the same location. For example:
ScalableImageIcon(Product.class, "/com/product/icons/a.png") will load a.png on a regular dpi monitor, but a.svg will be loaded on HiDPI monitor. Keep in mind that svg icon size can be any. Svg icon will be re-sized according to the size of a.png icon and scaling factor. For example if size of a.png is 16x16 and scaling factor is 2, loaded svg icon will be re-sized to 32x32 if needed.
Original icon will be loaded and scaled if svg icon is not provided, but HiDPI with scaling is used.
Icon image loading is lazy. Image will be loaded only if icon is painted, getImage() is called or icon width/height is retrieved.
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionScalableImageIcon
(byte[] imageData) Load icon from given bytes.ScalableImageIcon
(byte[] imageData, URI uri) Load icon from given bytes.ScalableImageIcon
(Image image) Wrap given image and use it for painting.ScalableImageIcon
(Class clazz, String location) Load icon from a resources of given class.ScalableImageIcon
(Class clazz, String location, boolean silent) Load icon from a resources of given class.ScalableImageIcon
(URL url) Load icon from a given url. -
Method Summary
Modifier and TypeMethodDescriptionclone()
Returns cloned instance of this icon.static ScalableImageIcon
Load icon from a file.void
Do not scale this icon by screen dpi.int
int
getImage()
getURL()
void
void
Draw the icon in the given bounds.void
static ScalableImageIcon
toImageIcon
(ResizableIcon icon) Convert given icon to a ImageIcon.static ScalableImageIcon
toImageIcon
(ResizableIcon icon, int width, int height) Convert given icon to a ImageIcon of given width and height.static ScalableImageIcon
toImageIcon
(Icon icon) Convert given icon to a ImageIcon.Methods inherited from class javax.swing.ImageIcon
getAccessibleContext, getDescription, getImageLoadStatus, getImageObserver, loadImage, setDescription, setImageObserver, toString
-
Constructor Details
-
ScalableImageIcon
Wrap given image and use it for painting.- Parameters:
image
- image
-
ScalableImageIcon
public ScalableImageIcon(byte[] imageData) Load icon from given bytes.- Parameters:
imageData
- image bytes
-
ScalableImageIcon
Load icon from given bytes.- Parameters:
imageData
- image bytesuri
- uri describing the bytes (format and etc)
-
ScalableImageIcon
Load icon from a given url.- Parameters:
url
- url
-
ScalableImageIcon
Load icon from a resources of given class.- Parameters:
clazz
- classlocation
- resources location relative to a given class
-
ScalableImageIcon
Load icon from a resources of given class.- Parameters:
clazz
- classlocation
- resources location relative to a given classsilent
- if true, does not report to log file about missing icon at given location
-
-
Method Details
-
create
Load icon from a file.- Parameters:
fileName
- file name
-
paintIcon
-
paintIcon
Description copied from interface:ResizableIcon
Draw the icon in the given bounds. Icon implementations may use the Component argument to get properties useful for painting, e.g. the foreground or background color.- Specified by:
paintIcon
in interfaceResizableIcon
- Parameters:
c
- the given component.g
- the graphics to paint on.x
- x coordinate.y
- y coordinate.w
- width to paint.h
- height to paint.
-
getIconHeight
public int getIconHeight()- Specified by:
getIconHeight
in interfaceIcon
- Overrides:
getIconHeight
in classImageIcon
-
getIconWidth
public int getIconWidth()- Specified by:
getIconWidth
in interfaceIcon
- Overrides:
getIconWidth
in classImageIcon
-
setImage
-
getImage
-
getLocation
- Returns:
- location of resource if icon is loaded from resources of some class. Null otherwise
-
getURL
- Returns:
- url if icon is loaded from url. Null otherwise
-
clone
Description copied from interface:ResizableIcon
Returns cloned instance of this icon.- Specified by:
clone
in interfaceResizableIcon
- Overrides:
clone
in classObject
- Returns:
- cloned instance.
-
doNotScale
public void doNotScale()Do not scale this icon by screen dpi. This methods has effect only if ScalableImageIcon was just created and image was not loaded yet. -
toImageIcon
@CheckForNull public static ScalableImageIcon toImageIcon(@CheckForNull ResizableIcon icon, int width, int height) Convert given icon to a ImageIcon of given width and height. Same icon is returned if it is already ImageIcon and has same size.- Parameters:
icon
- icon to convertwidth
- icon widthheight
- icon height- Returns:
- converted icon or same icon if it is already suitable
-
toImageIcon
Convert given icon to a ImageIcon. Same icon is returned if it is already ImageIcon.- Parameters:
icon
- icon to convert- Returns:
- converted icon or same icon if it is already suitable
-
toImageIcon
Convert given icon to a ImageIcon. Same icon is returned if it is already ImageIcon.- Parameters:
icon
- icon to convert- Returns:
- converted icon or same icon if it is already suitable
-