Class ScalableImageIcon
- java.lang.Object
-
- javax.swing.ImageIcon
-
- com.nomagic.ui.ScalableImageIcon
-
- All Implemented Interfaces:
ResizableIcon,java.io.Serializable,java.lang.Cloneable,javax.accessibility.Accessible,javax.swing.Icon
- Direct Known Subclasses:
DoubleSizeImageIcon,ResizableIconImageIcon
@OpenApiAll public class ScalableImageIcon extends javax.swing.ImageIcon implements ResizableIcon
HiDPI(Retina) friendly ImageIcon implementation.
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:
- /com/product/icons/a.png
- /com/product/icons/a.svg
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:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ScalableImageIcon(byte[] imageData)Load icon from given bytes.ScalableImageIcon(byte[] imageData, java.net.URI uri)Load icon from given bytes.ScalableImageIcon(java.awt.Image image)Wrap given image and use it for painting.ScalableImageIcon(java.lang.Class clazz, java.lang.String location)Load icon from a resources of given class.ScalableImageIcon(java.lang.Class clazz, java.lang.String location, boolean silent)Load icon from a resources of given class.ScalableImageIcon(java.net.URL url)Load icon from a given url.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectclone()Returns cloned instance of this icon.static ScalableImageIconcreate(java.lang.String fileName)Load icon from a file.voiddoNotScale()Do not scale this icon by screen dpi.intgetIconHeight()intgetIconWidth()java.awt.ImagegetImage()java.lang.StringgetLocation()java.net.URLgetURL()voidpaintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)voidpaintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y, int w, int h)Draw the icon in the given bounds.voidsetImage(java.awt.Image image)static ScalableImageIcontoImageIcon(ResizableIcon icon)Convert given icon to a ImageIcon.static ScalableImageIcontoImageIcon(ResizableIcon icon, int width, int height)Convert given icon to a ImageIcon of given width and height.static ScalableImageIcontoImageIcon(javax.swing.Icon icon)Convert given icon to a ImageIcon.
-
-
-
Constructor Detail
-
ScalableImageIcon
public ScalableImageIcon(java.awt.Image image)
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
public ScalableImageIcon(byte[] imageData, java.net.URI uri)Load icon from given bytes.- Parameters:
imageData- image bytesuri- uri describing the bytes (format and etc)
-
ScalableImageIcon
public ScalableImageIcon(java.net.URL url)
Load icon from a given url.- Parameters:
url- url
-
ScalableImageIcon
public ScalableImageIcon(java.lang.Class clazz, java.lang.String location)Load icon from a resources of given class.- Parameters:
clazz- classlocation- resources location relative to a given class
-
ScalableImageIcon
public ScalableImageIcon(java.lang.Class clazz, java.lang.String location, boolean silent)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 Detail
-
create
@CheckForNull public static ScalableImageIcon create(java.lang.String fileName)
Load icon from a file.- Parameters:
fileName- file name
-
paintIcon
public void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)- Specified by:
paintIconin interfacejavax.swing.Icon- Overrides:
paintIconin classjavax.swing.ImageIcon
-
paintIcon
public void paintIcon(@CheckForNull java.awt.Component c, java.awt.Graphics g, int x, int y, int w, int h)Description copied from interface:ResizableIconDraw 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:
paintIconin 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:
getIconHeightin interfacejavax.swing.Icon- Overrides:
getIconHeightin classjavax.swing.ImageIcon
-
getIconWidth
public int getIconWidth()
- Specified by:
getIconWidthin interfacejavax.swing.Icon- Overrides:
getIconWidthin classjavax.swing.ImageIcon
-
setImage
public void setImage(@CheckForNull java.awt.Image image)- Overrides:
setImagein classjavax.swing.ImageIcon
-
getImage
public java.awt.Image getImage()
- Overrides:
getImagein classjavax.swing.ImageIcon
-
getLocation
@CheckForNull public java.lang.String getLocation()
- Returns:
- location of resource if icon is loaded from resources of some class. Null otherwise
-
getURL
public java.net.URL getURL()
- Returns:
- url if icon is loaded from url. Null otherwise
-
clone
public java.lang.Object clone()
Description copied from interface:ResizableIconReturns cloned instance of this icon.- Specified by:
clonein interfaceResizableIcon- Overrides:
clonein classjava.lang.Object- 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
@CheckForNull public static ScalableImageIcon toImageIcon(@CheckForNull javax.swing.Icon icon)
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
@CheckForNull public static ScalableImageIcon toImageIcon(@CheckForNull ResizableIcon icon)
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
-
-