public final class ImageUtilities extends Object
Images can be represented as instances of either Image
or Icon
. For best
results on HiDPI displays, clients should use the ImageUtilities.image2Icon(Image)
method provided by
this class when converting an Image
to an Icon
, rather than constructing
ImageIcon
instances themselves. When doing manual painting, clients should use
Icon.paintIcon(Component, Graphics, int, int)
rather than
Graphics.drawImage(Image, int, int, ImageObserver)
.
Modifier and Type | Field and Description |
---|---|
static String |
PROPERTY_URL
Property that holds URL of the image bits.
|
Modifier and Type | Method and Description |
---|---|
static Image |
addToolTipToImage(Image image,
String text)
Add text to tool tip for given image (creates new or returns cached, original remains unmodified)
Text can contain HTML tags e.g.
|
static Image |
assignToolTipToImage(Image image,
String text)
Assign tool tip text to given image (creates new or returns cached, original remains unmodified)
Text can contain HTML tags e.g.
|
static Icon |
createDisabledIcon(Icon icon)
Creates disabled (color saturation lowered) icon.
|
static Image |
createDisabledImage(Image image)
Creates disabled (color saturation lowered) image.
|
static URL |
findImageBaseURL(Image image)
Attempts to find image's URL, if it is defined.
|
static String |
getImageToolTip(Image image)
Get tool tip text for given image
|
static Image |
icon2Image(Icon icon)
Converts given icon to a
Image . |
static Icon |
image2Icon(Image image)
Converts given image to an icon.
|
static Image |
loadImage(String resourceID)
Loads an image from the specified resource ID.
|
static Image |
loadImage(String resource,
boolean localized)
Loads an image based on resource path.
|
static ImageIcon |
loadImageIcon(String resource,
boolean localized)
Loads an icon based on resource path.
|
static Image |
mergeImages(Image image1,
Image image2,
int x,
int y)
This method merges two images into the new one.
|
public static final String PROPERTY_URL
public static final Image loadImage(String resourceID)
If the default lookup contains a service provider for the SVGLoader
interface, and
there exists an SVG version of the requested image (e.g. "icon.svg" exists when "icon.png"
was requested), the SVG version will be loaded instead of the originally requested bitmap.
SVG images can also be requested directly. The SVG document's root element must contain
explicit width/height attributes. An SVG loader implementation can be installed via the
optional openide.util.ui.svg
module.
To paint SVG images at arbitrary resolutions, convert the returned Image
to an
Icon
using ImageUtilities.image2Icon(Image)
, and set an appropriate transform on the
Graphics2D
instance passed to Icon.paintIcon(Component, Graphics, int, int)
.
When painting on HiDPI-capable Graphics2D
instances provided by Swing, the
appropriate transform will already be in place.
resourceID
- resource path of the icon (no initial slash)public static final Image loadImage(String resource, boolean localized)
ImageUtilities.loadImage(String)
but may do a localized search.
For example, requesting org/netbeans/modules/foo/resources/foo.gif
might actually find org/netbeans/modules/foo/resources/foo_ja.gif
or org/netbeans/modules/foo/resources/foo_mybranding.gif
.
Caching of loaded images can be used internally to improve performance.
Since version 8.12 the returned image object responds to call
image.getProperty(
by returning the internal
ImageUtilities.PROPERTY_URL
, null)URL
of the found and loaded resource
. Convenience method ImageUtilities.findImageBaseURL(java.awt.Image)
should be used in preference to direct property access.
If the current look and feel is 'dark' (UIManager.getBoolean("nb.dark.theme")
)
then the method first attempts to load image <original file name>_dark.<original extension>.
If such file doesn't exist the default one is loaded instead.
resource
- resource path of the image (no initial slash)localized
- true for localized searchpublic static final ImageIcon loadImageIcon(String resource, boolean localized)
ImageUtilities.loadImage(String, boolean)
, returns ImageIcon instead of Image.
If the current look and feel is 'dark' (UIManager.getBoolean("nb.dark.theme")
)
then the method first attempts to load image <original file name>_dark.<original extension>.
If such file doesn't exist the default one is loaded instead.
resource
- resource path of the icon (no initial slash)localized
- localized resource should be usedpublic static final Image mergeImages(Image image1, Image image2, int x, int y)
image1
- underlying imageimage2
- second imagex
- x position of top-left cornery
- y position of top-left cornerpublic static final Icon image2Icon(Image image)
image
- to be convertedpublic static final Image icon2Image(Icon icon)
Image
.
A scalable Icon
instance can always be recovered by passing the returned
Image
to ImageUtilities.image2Icon(Image)
again, i.e. for painting on HiDPI screens.
icon
- Icon
to be converted.public static final Image assignToolTipToImage(Image image, String text)
image
- image to which tool tip should be settext
- tool tip textpublic static final String getImageToolTip(Image image)
image
- image which is asked for tool tip textpublic static final Image addToolTipToImage(Image image, String text)
text
- text to add to tool tippublic static Icon createDisabledIcon(Icon icon)
icon
- original icon used for conversionpublic static Image createDisabledImage(Image image)
image
- original image used for conversionpublic static URL findImageBaseURL(Image image)
image
- image to inspectnull
if not defined.