Class ImageProvider
- java.lang.Object
-
- org.openstreetmap.josm.tools.ImageProvider
-
public class ImageProvider extends java.lang.Object
Helper class to support the application with images.How to use:
ImageIcon icon = new ImageProvider(name).setMaxSize(ImageSizes.MAP).get();
(there are more options, see below)short form:
ImageIcon icon = ImageProvider.get(name);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ImageProvider.ImageLocations
static class
ImageProvider.ImageSizes
Supported image sizesstatic class
ImageProvider.ImageType
Supported image types
-
Field Summary
Fields Modifier and Type Field Description protected java.io.File
archive
archive file to take image fromprivate static java.util.Map<java.lang.String,ImageResource>
cache
The icon cacheprivate static java.awt.Point
CROSSHAIR_HOTSPOT
protected static int
CURSOR_SIZE_HOTSPOT_IS_RELATIVE_TO
The cursor hotspot constantsDEFAULT_HOTSPOT
andCROSSHAIR_HOTSPOT
are relative to this cursor sizeprivate static java.util.regex.Pattern
dataUrlPattern
data:[<mediatype>][;base64],<data>
private static java.awt.Point
DEFAULT_HOTSPOT
protected java.util.Collection<java.lang.String>
dirs
directories in which images are searchedprivate static java.lang.String
HTTP_PROTOCOL
private static java.lang.String
HTTPS_PROTOCOL
protected java.lang.String
id
caching identifierprivate static java.util.concurrent.ExecutorService
IMAGE_FETCHER
protected java.lang.String
inArchiveDir
directory inside the archiveprotected boolean
isDisabled
true
if icon must be grayed outprotected boolean
multiResolution
true
if multi-resolution image is requestedprotected java.lang.String
name
image file nameprotected boolean
optional
In case of errors do not throw exception but returnnull
for missing imageprivate static java.util.Map<OsmPrimitiveType,javax.swing.ImageIcon>
osmPrimitiveTypeCache
small cache of critical images used in many parts of the applicationprotected java.util.List<ImageOverlay>
overlayInfo
ordered list of overlay imagesstatic java.lang.String
PROP_TRANSPARENCY_COLOR
Property set onBufferedImage
returned byread(java.io.File, boolean, boolean)
if metadata is required.static java.lang.String
PROP_TRANSPARENCY_FORCED
Property set onBufferedImage
returned bymakeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.protected java.lang.String
subdir
sub directory the image can be found inprotected boolean
suppressWarnings
true
if warnings should be suppressedprivate static com.kitfox.svg.SVGUniverse
svgUniverse
protected int
virtualHeight
virtual height of the resulting image, -1 when original image data should be usedprotected int
virtualMaxHeight
virtual maximum height of the resulting image, -1 for no restrictionprotected int
virtualMaxWidth
virtual maximum width of the resulting image, -1 for no restrictionprotected int
virtualWidth
virtual width of the resulting image, -1 when original image data should be usedprivate static java.lang.String
WIKI_PROTOCOL
-
Constructor Summary
Constructors Constructor Description ImageProvider(java.lang.String name)
Constructs a newImageProvider
from a filename.ImageProvider(java.lang.String subdir, java.lang.String name)
Constructs a newImageProvider
from a filename in a given directory.ImageProvider(ImageProvider image)
Constructs a newImageProvider
from an existing one.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ImageProvider
addOverlay(ImageOverlay overlay)
Add an overlay over the image.static void
clearCache()
Clears the internal image caches.static javax.swing.ImageIcon
createBlankIcon(ImageProvider.ImageSizes size)
Creates a blank icon of the given size.static java.awt.Image
createBoundedImage(java.awt.Image img, int maxSize)
Creates a scaled down version of the input image to fit maximum dimensions.(package private) static java.awt.image.BufferedImage
createImageFromSvg(com.kitfox.svg.SVGDiagram svg, java.awt.Dimension dim, ImageResizeMode resizeMode)
Constructs an image from the given SVG data.private static javax.imageio.stream.ImageInputStream
createImageInputStream(java.lang.Object input)
static java.awt.image.BufferedImage
createScaledImage(java.awt.image.BufferedImage img, int targetWidth, int targetHeight, java.lang.Object hint)
Returns a scaled instance of the providedBufferedImage
.javax.swing.ImageIcon
get()
Execute the image request and scale result.static javax.swing.ImageIcon
get(java.lang.String name)
Load an image with a given file name.static javax.swing.ImageIcon
get(java.lang.String subdir, java.lang.String name)
Load an image with a given file name.static javax.swing.ImageIcon
get(java.lang.String subdir, java.lang.String name, ImageProvider.ImageSizes size)
Load an image from directory with a given file name and size.static javax.swing.ImageIcon
get(java.lang.String name, ImageProvider.ImageSizes size)
Load an image with a given file name and size.static javax.swing.ImageIcon
get(OsmPrimitiveType type)
Replies the icon for an OSM primitive typejava.util.concurrent.CompletableFuture<java.lang.Void>
getAsync(java.util.function.Consumer<? super javax.swing.ImageIcon> action)
Load the image in a background thread.static java.awt.Cursor
getCursor(java.lang.String name, java.lang.String overlay)
Load a cursor with a given file name, optionally decorated with an overlay image.(package private) static java.awt.Image
getCursorImage(java.lang.String name, java.lang.String overlay, java.util.function.UnaryOperator<java.awt.Dimension> bestCursorSizeFunction, java.awt.Point hotSpot)
Load a cursor image with a given file name, optionally decorated with an overlay imagejava.lang.String
getDataURL()
Execute the image request and scale result.static javax.swing.ImageIcon
getEmpty(ImageProvider.ImageSizes size)
Load an empty image with a given size.static javax.swing.ImageIcon
getIfAvailable(java.lang.String name)
Load an image with a given file name, but do not throw an exception when the image cannot be found.static javax.swing.ImageIcon
getIfAvailable(java.lang.String subdir, java.lang.String name)
Load an image with a given file name, but do not throw an exception when the image cannot be found.private static ImageResource
getIfAvailableDataUrl(java.lang.String url)
Internal implementation of the image request for inline images (data: urls).private static ImageResource
getIfAvailableHttp(java.lang.String url, ImageProvider.ImageType type)
Internal implementation of the image request for URL's.private ImageResource
getIfAvailableImpl()
Internal implementation of the image request.private static ImageResource
getIfAvailableLocalURL(java.net.URL path, ImageProvider.ImageType type)
Internal implementation of the image request for local images.private static ImageResource
getIfAvailableWiki(java.lang.String name, ImageProvider.ImageType type)
Internal implementation of the image request for wiki images.private static ImageResource
getIfAvailableZip(java.lang.String fullName, java.io.File archive, java.lang.String inArchiveDir, ImageProvider.ImageType type)
Internal implementation of the image request for images in Zip archives.private java.net.URL
getImageUrl(java.lang.String imageName)
private static java.net.URL
getImageUrl(java.lang.String path, java.lang.String name)
private static java.lang.String
getImgUrlFromWikiInfoPage(java.lang.String base, java.lang.String fn)
Reads the wiki page on a certain file in html format in order to find the real image URL.static javax.swing.ImageIcon
getPadded(OsmPrimitive primitive, java.awt.Dimension iconSize)
Returns anImageIcon
for the given OSM object, at the specified size.ImageResource
getResource()
Execute the image request.java.util.concurrent.CompletableFuture<java.lang.Void>
getResourceAsync(java.util.function.Consumer<? super ImageResource> action)
Load the image in a background thread.private static com.kitfox.svg.SVGUniverse
getSvgUniverse()
static java.awt.Color
getTransparentColor(java.awt.image.ColorModel model, javax.imageio.ImageReader reader)
Returns theTransparentColor
defined in image reader metadata.static boolean
hasTransparentColor(java.awt.image.BufferedImage bi)
Determines if the givenBufferedImage
has a transparent color determined by a previous call toread(java.io.File, boolean, boolean)
.boolean
isRemote()
Determines if this icon is located on a remote location (http, https, wiki).static boolean
isTransparencyForced(java.awt.image.BufferedImage bi)
Determines if the transparency of the givenBufferedImage
has been enforced by a previous call tomakeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.static java.awt.image.BufferedImage
makeImageTransparent(java.awt.image.BufferedImage bi, java.awt.Color color)
Returns a transparent version of the given image, based on the given transparent color.private static java.awt.Color
parseRGB(java.lang.String... s)
static java.awt.image.BufferedImage
read(java.io.File input, boolean readMetadata, boolean enforceTransparency)
Returns aBufferedImage
as the result of decoding a suppliedFile
with anImageReader
chosen automatically from among those currently registered.static java.awt.image.BufferedImage
read(java.io.InputStream input, boolean readMetadata, boolean enforceTransparency)
Returns aBufferedImage
as the result of decoding a suppliedInputStream
with anImageReader
chosen automatically from among those currently registered.static java.awt.image.BufferedImage
read(java.net.URL input, boolean readMetadata, boolean enforceTransparency)
Returns aBufferedImage
as the result of decoding a suppliedURL
with anImageReader
chosen automatically from among those currently registered.static java.awt.image.BufferedImage
read(java.net.URL input, boolean readMetadata, boolean enforceTransparency, java.util.function.Function<javax.imageio.ImageReader,javax.imageio.ImageReadParam> readParamFunction)
Returns aBufferedImage
as the result of decoding a suppliedURL
with anImageReader
chosen automatically from among those currently registered.static java.awt.image.BufferedImage
read(javax.imageio.stream.ImageInputStream stream, boolean readMetadata, boolean enforceTransparency)
Returns aBufferedImage
as the result of decoding a suppliedImageInputStream
with anImageReader
chosen automatically from among those currently registered.private static java.awt.image.BufferedImage
read(javax.imageio.stream.ImageInputStream stream, boolean readMetadata, boolean enforceTransparency, java.util.function.Function<javax.imageio.ImageReader,javax.imageio.ImageReadParam> readParamFunction)
ImageProvider
resetMaxSize(java.awt.Dimension maxSize)
Limit the maximum size of the image.ImageProvider
setArchive(java.io.File archive)
Specify a zip file where the image is located.ImageProvider
setDirs(java.util.Collection<java.lang.String> dirs)
Directories to look for the image.ImageProvider
setDisabled(boolean disabled)
Set, if image must be filtered to grayscale so it will look like disabled icon.ImageProvider
setHeight(int height)
Set image heightImageProvider
setId(java.lang.String id)
Set an id used for caching.ImageProvider
setInArchiveDir(java.lang.String inArchiveDir)
Specify a base path inside the zip file.ImageProvider
setMaxHeight(int maxHeight)
Limit the maximum height of the image.ImageProvider
setMaxSize(int maxSize)
Convenience method, seesetMaxSize(Dimension)
.ImageProvider
setMaxSize(java.awt.Dimension maxSize)
Limit the maximum size of the image.ImageProvider
setMaxSize(ImageProvider.ImageSizes size)
Limit the maximum size of the image.ImageProvider
setMaxWidth(int maxWidth)
Limit the maximum width of the image.ImageProvider
setMultiResolution(boolean multiResolution)
Decide, if multi-resolution image is requested (defaulttrue
).ImageProvider
setOptional(boolean optional)
Decide, if an exception should be thrown, when the image cannot be located.ImageProvider
setSize(int width, int height)
Set the dimensions of the image.ImageProvider
setSize(java.awt.Dimension size)
Set the dimensions of the image.ImageProvider
setSize(ImageProvider.ImageSizes size)
Set the dimensions of the image.ImageProvider
setSuppressWarnings(boolean suppressWarnings)
Suppresses warning on the command line in case the image cannot be found.ImageProvider
setWidth(int width)
Set image widthstatic void
shutdown(boolean now)
Shutdown background image fetcher.static java.awt.image.BufferedImage
toBufferedImage(java.awt.Image image)
Converts anImage
to aBufferedImage
instance.static java.awt.image.BufferedImage
toBufferedImage(java.awt.Image image, java.awt.Rectangle cropArea)
Converts anRectangle
area ofImage
to aBufferedImage
instance.java.lang.String
toString()
-
-
-
Field Detail
-
HTTP_PROTOCOL
private static final java.lang.String HTTP_PROTOCOL
- See Also:
- Constant Field Values
-
HTTPS_PROTOCOL
private static final java.lang.String HTTPS_PROTOCOL
- See Also:
- Constant Field Values
-
WIKI_PROTOCOL
private static final java.lang.String WIKI_PROTOCOL
- See Also:
- Constant Field Values
-
PROP_TRANSPARENCY_FORCED
public static final java.lang.String PROP_TRANSPARENCY_FORCED
Property set onBufferedImage
returned bymakeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.- Since:
- 7132
- See Also:
- Constant Field Values
-
PROP_TRANSPARENCY_COLOR
public static final java.lang.String PROP_TRANSPARENCY_COLOR
Property set onBufferedImage
returned byread(java.io.File, boolean, boolean)
if metadata is required.- Since:
- 7132
- See Also:
- Constant Field Values
-
dirs
protected java.util.Collection<java.lang.String> dirs
directories in which images are searched
-
id
protected java.lang.String id
caching identifier
-
subdir
protected java.lang.String subdir
sub directory the image can be found in
-
name
protected final java.lang.String name
image file name
-
archive
protected java.io.File archive
archive file to take image from
-
inArchiveDir
protected java.lang.String inArchiveDir
directory inside the archive
-
virtualWidth
protected int virtualWidth
virtual width of the resulting image, -1 when original image data should be used
-
virtualHeight
protected int virtualHeight
virtual height of the resulting image, -1 when original image data should be used
-
virtualMaxWidth
protected int virtualMaxWidth
virtual maximum width of the resulting image, -1 for no restriction
-
virtualMaxHeight
protected int virtualMaxHeight
virtual maximum height of the resulting image, -1 for no restriction
-
optional
protected boolean optional
In case of errors do not throw exception but returnnull
for missing image
-
suppressWarnings
protected boolean suppressWarnings
true
if warnings should be suppressed
-
overlayInfo
protected java.util.List<ImageOverlay> overlayInfo
ordered list of overlay images
-
isDisabled
protected boolean isDisabled
true
if icon must be grayed out
-
multiResolution
protected boolean multiResolution
true
if multi-resolution image is requested
-
svgUniverse
private static com.kitfox.svg.SVGUniverse svgUniverse
-
cache
private static final java.util.Map<java.lang.String,ImageResource> cache
The icon cache
-
osmPrimitiveTypeCache
private static final java.util.Map<OsmPrimitiveType,javax.swing.ImageIcon> osmPrimitiveTypeCache
small cache of critical images used in many parts of the application
-
IMAGE_FETCHER
private static final java.util.concurrent.ExecutorService IMAGE_FETCHER
-
dataUrlPattern
private static final java.util.regex.Pattern dataUrlPattern
data:[<mediatype>][;base64],<data>
- See Also:
- RFC2397
-
CURSOR_SIZE_HOTSPOT_IS_RELATIVE_TO
protected static final int CURSOR_SIZE_HOTSPOT_IS_RELATIVE_TO
The cursor hotspot constantsDEFAULT_HOTSPOT
andCROSSHAIR_HOTSPOT
are relative to this cursor size- See Also:
- Constant Field Values
-
DEFAULT_HOTSPOT
private static final java.awt.Point DEFAULT_HOTSPOT
-
CROSSHAIR_HOTSPOT
private static final java.awt.Point CROSSHAIR_HOTSPOT
-
-
Constructor Detail
-
ImageProvider
public ImageProvider(java.lang.String subdir, java.lang.String name)
Constructs a newImageProvider
from a filename in a given directory.- Parameters:
subdir
- subdirectory the image lies inname
- the name of the image. If it does not end with '.png' or '.svg', both extensions are tried.- Throws:
java.lang.NullPointerException
- if name is null
-
ImageProvider
public ImageProvider(java.lang.String name)
Constructs a newImageProvider
from a filename.- Parameters:
name
- the name of the image. If it does not end with '.png' or '.svg', both extensions are tried.- Throws:
java.lang.NullPointerException
- if name is null
-
ImageProvider
public ImageProvider(ImageProvider image)
Constructs a newImageProvider
from an existing one.- Parameters:
image
- the existing image provider to be copied- Since:
- 8095
-
-
Method Detail
-
setDirs
public ImageProvider setDirs(java.util.Collection<java.lang.String> dirs)
Directories to look for the image.- Parameters:
dirs
- The directories to look for.- Returns:
- the current object, for convenience
-
setId
public ImageProvider setId(java.lang.String id)
Set an id used for caching. If name starts withhttp://
Id is not used for the cache. (A URL is unique anyway.)- Parameters:
id
- the id for the cached image- Returns:
- the current object, for convenience
-
setArchive
public ImageProvider setArchive(java.io.File archive)
Specify a zip file where the image is located.(optional)
- Parameters:
archive
- zip file where the image is located- Returns:
- the current object, for convenience
-
setInArchiveDir
public ImageProvider setInArchiveDir(java.lang.String inArchiveDir)
Specify a base path inside the zip file.The subdir and name will be relative to this path.
(optional)
- Parameters:
inArchiveDir
- path inside the archive- Returns:
- the current object, for convenience
-
addOverlay
public ImageProvider addOverlay(ImageOverlay overlay)
Add an overlay over the image. Multiple overlays are possible.- Parameters:
overlay
- overlay image and placement specification- Returns:
- the current object, for convenience
- Since:
- 8095
-
setSize
public ImageProvider setSize(java.awt.Dimension size)
Set the dimensions of the image.If not specified, the original size of the image is used. The width part of the dimension can be -1. Then it will only set the height but keep the aspect ratio. (And the other way around.)
- Parameters:
size
- final dimensions of the image- Returns:
- the current object, for convenience
-
setSize
public ImageProvider setSize(ImageProvider.ImageSizes size)
Set the dimensions of the image.If not specified, the original size of the image is used.
- Parameters:
size
- final dimensions of the image- Returns:
- the current object, for convenience
- Since:
- 7687
-
setSize
public ImageProvider setSize(int width, int height)
Set the dimensions of the image.- Parameters:
width
- final width of the imageheight
- final height of the image- Returns:
- the current object, for convenience
- Since:
- 10358
-
setWidth
public ImageProvider setWidth(int width)
Set image width- Parameters:
width
- final width of the image- Returns:
- the current object, for convenience
- See Also:
setSize(java.awt.Dimension)
-
setHeight
public ImageProvider setHeight(int height)
Set image height- Parameters:
height
- final height of the image- Returns:
- the current object, for convenience
- See Also:
setSize(java.awt.Dimension)
-
setMaxSize
public ImageProvider setMaxSize(java.awt.Dimension maxSize)
Limit the maximum size of the image.It will shrink the image if necessary, but keep the aspect ratio. The given width or height can be -1 which means this direction is not bounded.
'size' and 'maxSize' are not compatible, you should set only one of them.
- Parameters:
maxSize
- maximum image size- Returns:
- the current object, for convenience
-
resetMaxSize
public ImageProvider resetMaxSize(java.awt.Dimension maxSize)
Limit the maximum size of the image.It will shrink the image if necessary, but keep the aspect ratio. The given width or height can be -1 which means this direction is not bounded.
This function sets value using the most restrictive of the new or existing set of values.
- Parameters:
maxSize
- maximum image size- Returns:
- the current object, for convenience
- See Also:
setMaxSize(Dimension)
-
setMaxSize
public ImageProvider setMaxSize(ImageProvider.ImageSizes size)
Limit the maximum size of the image.It will shrink the image if necessary, but keep the aspect ratio. The given width or height can be -1 which means this direction is not bounded.
'size' and 'maxSize' are not compatible, you should set only one of them.
- Parameters:
size
- maximum image size- Returns:
- the current object, for convenience
- Since:
- 7687
-
setMaxSize
public ImageProvider setMaxSize(int maxSize)
Convenience method, seesetMaxSize(Dimension)
.- Parameters:
maxSize
- maximum image size- Returns:
- the current object, for convenience
-
setMaxWidth
public ImageProvider setMaxWidth(int maxWidth)
Limit the maximum width of the image.- Parameters:
maxWidth
- maximum image width- Returns:
- the current object, for convenience
- See Also:
setMaxSize(java.awt.Dimension)
-
setMaxHeight
public ImageProvider setMaxHeight(int maxHeight)
Limit the maximum height of the image.- Parameters:
maxHeight
- maximum image height- Returns:
- the current object, for convenience
- See Also:
setMaxSize(java.awt.Dimension)
-
setOptional
public ImageProvider setOptional(boolean optional)
Decide, if an exception should be thrown, when the image cannot be located.Set to true, when the image URL comes from user data and the image may be missing.
- Parameters:
optional
- true, if JOSM should not throw a RuntimeException in case the image cannot be located.- Returns:
- the current object, for convenience
-
setSuppressWarnings
public ImageProvider setSuppressWarnings(boolean suppressWarnings)
Suppresses warning on the command line in case the image cannot be found.In combination with setOptional(true);
- Parameters:
suppressWarnings
- iftrue
warnings are suppressed- Returns:
- the current object, for convenience
-
setDisabled
public ImageProvider setDisabled(boolean disabled)
Set, if image must be filtered to grayscale so it will look like disabled icon.- Parameters:
disabled
- true, if image must be grayed out for disabled state- Returns:
- the current object, for convenience
- Since:
- 10428
-
setMultiResolution
public ImageProvider setMultiResolution(boolean multiResolution)
Decide, if multi-resolution image is requested (defaulttrue
).A
java.awt.image.MultiResolutionImage
is a Java 9Image
implementation, which adds support for HiDPI displays. The effect will be that in HiDPI mode, when GUI elements are scaled by a factor 1.5, 2.0, etc., the images are not just up-scaled, but a higher resolution version of the image is rendered instead.Use
HiDPISupport.getBaseImage(java.awt.Image)
to extract the original image from a multi-resolution image.See
HiDPISupport.processMRImage(java.awt.Image, java.util.function.UnaryOperator<java.awt.Image>)
for how to process the image without removing the multi-resolution magic.- Parameters:
multiResolution
- true, if multi-resolution image is requested- Returns:
- the current object, for convenience
-
isRemote
public boolean isRemote()
Determines if this icon is located on a remote location (http, https, wiki).- Returns:
true
if this icon is located on a remote location (http, https, wiki)- Since:
- 13250
-
get
public javax.swing.ImageIcon get()
Execute the image request and scale result.- Returns:
- the requested image or null if the request failed
-
getDataURL
public java.lang.String getDataURL()
Execute the image request and scale result.- Returns:
- the requested image as data: URL or null if the request failed
- Since:
- 16872
-
getAsync
public java.util.concurrent.CompletableFuture<java.lang.Void> getAsync(java.util.function.Consumer<? super javax.swing.ImageIcon> action)
Load the image in a background thread.This method returns immediately and runs the image request asynchronously.
- Parameters:
action
- the action that will deal with the image- Returns:
- the future of the requested image
- Since:
- 13252
-
getResource
public ImageResource getResource()
Execute the image request.- Returns:
- the requested image or null if the request failed
- Since:
- 7693
-
getResourceAsync
public java.util.concurrent.CompletableFuture<java.lang.Void> getResourceAsync(java.util.function.Consumer<? super ImageResource> action)
Load the image in a background thread.This method returns immediately and runs the image request asynchronously.
- Parameters:
action
- the action that will deal with the image- Returns:
- the future of the requested image
- Since:
- 13252
-
get
public static javax.swing.ImageIcon get(java.lang.String subdir, java.lang.String name)
Load an image with a given file name.- Parameters:
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)- Returns:
- The requested Image.
- Throws:
java.lang.RuntimeException
- if the image cannot be located
-
get
public static javax.swing.ImageIcon get(java.lang.String name)
Load an image with a given file name.- Parameters:
name
- The icon name (base name with or without '.png' or '.svg' extension)- Returns:
- the requested image or null if the request failed
- See Also:
get(String, String)
-
get
public static javax.swing.ImageIcon get(java.lang.String subdir, java.lang.String name, ImageProvider.ImageSizes size)
Load an image from directory with a given file name and size.- Parameters:
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)size
- Target icon size- Returns:
- The requested Image.
- Throws:
java.lang.RuntimeException
- if the image cannot be located- Since:
- 10428
-
getEmpty
public static javax.swing.ImageIcon getEmpty(ImageProvider.ImageSizes size)
Load an empty image with a given size.- Parameters:
size
- Target icon size- Returns:
- The requested Image.
- Since:
- 10358
-
getIfAvailable
public static javax.swing.ImageIcon getIfAvailable(java.lang.String subdir, java.lang.String name)
Load an image with a given file name, but do not throw an exception when the image cannot be found.- Parameters:
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)- Returns:
- the requested image or null if the request failed
- See Also:
get(String, String)
-
get
public static javax.swing.ImageIcon get(java.lang.String name, ImageProvider.ImageSizes size)
Load an image with a given file name and size.- Parameters:
name
- The icon name (base name with or without '.png' or '.svg' extension)size
- Target icon size- Returns:
- the requested image or null if the request failed
- Since:
- 10428
- See Also:
get(String, String)
-
getIfAvailable
public static javax.swing.ImageIcon getIfAvailable(java.lang.String name)
Load an image with a given file name, but do not throw an exception when the image cannot be found.- Parameters:
name
- The icon name (base name with or without '.png' or '.svg' extension)- Returns:
- the requested image or null if the request failed
- See Also:
getIfAvailable(String, String)
-
clearCache
public static void clearCache()
Clears the internal image caches.- Since:
- 11021
-
getIfAvailableImpl
private ImageResource getIfAvailableImpl()
Internal implementation of the image request.- Returns:
- the requested image or null if the request failed
-
getIfAvailableHttp
private static ImageResource getIfAvailableHttp(java.lang.String url, ImageProvider.ImageType type)
Internal implementation of the image request for URL's.- Parameters:
url
- URL of the imagetype
- data type of the image- Returns:
- the requested image or null if the request failed
-
getIfAvailableDataUrl
private static ImageResource getIfAvailableDataUrl(java.lang.String url)
Internal implementation of the image request for inline images (data: urls).- Parameters:
url
- the data URL for image extraction- Returns:
- the requested image or null if the request failed
-
getIfAvailableWiki
private static ImageResource getIfAvailableWiki(java.lang.String name, ImageProvider.ImageType type)
Internal implementation of the image request for wiki images.- Parameters:
name
- image file nametype
- data type of the image- Returns:
- the requested image or null if the request failed
-
getIfAvailableZip
private static ImageResource getIfAvailableZip(java.lang.String fullName, java.io.File archive, java.lang.String inArchiveDir, ImageProvider.ImageType type)
Internal implementation of the image request for images in Zip archives.- Parameters:
fullName
- image file namearchive
- the archive to get image frominArchiveDir
- directory of the image inside the archive ornull
type
- data type of the image- Returns:
- the requested image or null if the request failed
-
getIfAvailableLocalURL
private static ImageResource getIfAvailableLocalURL(java.net.URL path, ImageProvider.ImageType type)
Internal implementation of the image request for local images.- Parameters:
path
- image file pathtype
- data type of the image- Returns:
- the requested image or null if the request failed
-
getImageUrl
private static java.net.URL getImageUrl(java.lang.String path, java.lang.String name)
-
getImageUrl
private java.net.URL getImageUrl(java.lang.String imageName)
-
getImgUrlFromWikiInfoPage
private static java.lang.String getImgUrlFromWikiInfoPage(java.lang.String base, java.lang.String fn)
Reads the wiki page on a certain file in html format in order to find the real image URL.- Parameters:
base
- base URL for Wiki imagefn
- filename of the Wiki image- Returns:
- image URL for a Wiki image or null in case of error
-
getCursor
public static java.awt.Cursor getCursor(java.lang.String name, java.lang.String overlay)
Load a cursor with a given file name, optionally decorated with an overlay image.- Parameters:
name
- the cursor image filename in "cursor" directoryoverlay
- optional overlay image- Returns:
- cursor with a given file name, optionally decorated with an overlay image
-
getCursorImage
static java.awt.Image getCursorImage(java.lang.String name, java.lang.String overlay, java.util.function.UnaryOperator<java.awt.Dimension> bestCursorSizeFunction, java.awt.Point hotSpot)
Load a cursor image with a given file name, optionally decorated with an overlay image- Parameters:
name
- the cursor image filename in "cursor" directoryoverlay
- optional overlay imagebestCursorSizeFunction
- computes the best cursor size, seeToolkit.getBestCursorSize(int, int)
hotSpot
- will be set to the properly scaled hotspot of the cursor- Returns:
- cursor with a given file name, optionally decorated with an overlay image
-
createBoundedImage
public static java.awt.Image createBoundedImage(java.awt.Image img, int maxSize)
Creates a scaled down version of the input image to fit maximum dimensions. (Keeps aspect ratio)- Parameters:
img
- the image to be scaled down.maxSize
- the maximum size in pixels (both for width and height)- Returns:
- the image after scaling.
- Since:
- 6172
-
createScaledImage
public static java.awt.image.BufferedImage createScaledImage(java.awt.image.BufferedImage img, int targetWidth, int targetHeight, java.lang.Object hint)
Returns a scaled instance of the providedBufferedImage
. This method will use a multi-step scaling technique that provides higher quality than the usual one-step technique (only useful in downscaling cases, wheretargetWidth
ortargetHeight
is smaller than the original dimensions, and generally only when theBILINEAR
hint is specified).- Parameters:
img
- the original image to be scaledtargetWidth
- the desired width of the scaled instance, in pixelstargetHeight
- the desired height of the scaled instance, in pixelshint
- one of the rendering hints that corresponds toRenderingHints.KEY_INTERPOLATION
(e.g.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
,RenderingHints.VALUE_INTERPOLATION_BILINEAR
,RenderingHints.VALUE_INTERPOLATION_BICUBIC
)- Returns:
- a scaled version of the original
BufferedImage
- Since:
- 13038
-
get
public static javax.swing.ImageIcon get(OsmPrimitiveType type)
Replies the icon for an OSM primitive type- Parameters:
type
- the type- Returns:
- the icon
-
getPadded
public static javax.swing.ImageIcon getPadded(OsmPrimitive primitive, java.awt.Dimension iconSize)
Returns anImageIcon
for the given OSM object, at the specified size. This is a slow operation.- Parameters:
primitive
- Object for which an icon shall be fetched. The icon is chosen based on tags.iconSize
- Target size of icon. Icon is padded if required.- Returns:
- Icon for
primitive
that fits in cell. - Since:
- 8903
-
createImageFromSvg
static java.awt.image.BufferedImage createImageFromSvg(com.kitfox.svg.SVGDiagram svg, java.awt.Dimension dim, ImageResizeMode resizeMode)
Constructs an image from the given SVG data.- Parameters:
svg
- the SVG datadim
- the desired image dimensionresizeMode
- how to size/resize the image- Returns:
- an image from the given SVG data at the desired dimension.
-
getSvgUniverse
private static com.kitfox.svg.SVGUniverse getSvgUniverse()
-
read
public static java.awt.image.BufferedImage read(java.io.File input, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
Returns aBufferedImage
as the result of decoding a suppliedFile
with anImageReader
chosen automatically from among those currently registered. TheFile
is wrapped in anImageInputStream
. If no registeredImageReader
claims to be able to read the resulting stream,null
is returned.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching in theImageInputStream
that is created.Note that there is no
read
method that takes a filename as aString
; use this method instead after creating aFile
from the filename.This method does not attempt to locate
ImageReader
s that can read directly from aFile
; that may be accomplished usingIIORegistry
andImageReaderSpi
.- Parameters:
input
- aFile
to read from.readMetadata
- iftrue
, makes sure to read image metadata to detect transparency color, if any. In that case the color can be retrieved later throughPROP_TRANSPARENCY_COLOR
. Always consideredtrue
ifenforceTransparency
is alsotrue
enforceTransparency
- iftrue
, makes sure to read image metadata and, if the image does not provide an alpha channel but defines aTransparentColor
metadata node, that the resulting image has a transparency set toTRANSLUCENT
and uses the correct transparent color.- Returns:
- a
BufferedImage
containing the decoded contents of the input, ornull
. - Throws:
java.lang.IllegalArgumentException
- ifinput
isnull
.java.io.IOException
- if an error occurs during reading.- Since:
- 7132
- See Also:
BufferedImage.getProperty(java.lang.String, java.awt.image.ImageObserver)
-
read
public static java.awt.image.BufferedImage read(java.io.InputStream input, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
Returns aBufferedImage
as the result of decoding a suppliedInputStream
with anImageReader
chosen automatically from among those currently registered. TheInputStream
is wrapped in anImageInputStream
. If no registeredImageReader
claims to be able to read the resulting stream,null
is returned.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching in theImageInputStream
that is created.This method does not attempt to locate
ImageReader
s that can read directly from anInputStream
; that may be accomplished usingIIORegistry
andImageReaderSpi
.This method does not close the provided
InputStream
after the read operation has completed; it is the responsibility of the caller to close the stream, if desired.- Parameters:
input
- anInputStream
to read from.readMetadata
- iftrue
, makes sure to read image metadata to detect transparency color for non translucent images, if any. In that case the color can be retrieved later throughPROP_TRANSPARENCY_COLOR
. Always consideredtrue
ifenforceTransparency
is alsotrue
enforceTransparency
- iftrue
, makes sure to read image metadata and, if the image does not provide an alpha channel but defines aTransparentColor
metadata node, that the resulting image has a transparency set toTRANSLUCENT
and uses the correct transparent color.- Returns:
- a
BufferedImage
containing the decoded contents of the input, ornull
. - Throws:
java.lang.IllegalArgumentException
- ifinput
isnull
.java.io.IOException
- if an error occurs during reading.- Since:
- 7132
-
read
public static java.awt.image.BufferedImage read(java.net.URL input, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
Returns aBufferedImage
as the result of decoding a suppliedURL
with anImageReader
chosen automatically from among those currently registered. AnInputStream
is obtained from theURL
, which is wrapped in anImageInputStream
. If no registeredImageReader
claims to be able to read the resulting stream,null
is returned.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching in theImageInputStream
that is created.This method does not attempt to locate
ImageReader
s that can read directly from aURL
; that may be accomplished usingIIORegistry
andImageReaderSpi
.- Parameters:
input
- aURL
to read from.readMetadata
- iftrue
, makes sure to read image metadata to detect transparency color for non translucent images, if any. In that case the color can be retrieved later throughPROP_TRANSPARENCY_COLOR
. Always consideredtrue
ifenforceTransparency
is alsotrue
enforceTransparency
- iftrue
, makes sure to read image metadata and, if the image does not provide an alpha channel but defines aTransparentColor
metadata node, that the resulting image has a transparency set toTRANSLUCENT
and uses the correct transparent color.- Returns:
- a
BufferedImage
containing the decoded contents of the input, ornull
. - Throws:
java.lang.IllegalArgumentException
- ifinput
isnull
.java.io.IOException
- if an error occurs during reading.- Since:
- 7132
-
read
public static java.awt.image.BufferedImage read(java.net.URL input, boolean readMetadata, boolean enforceTransparency, java.util.function.Function<javax.imageio.ImageReader,javax.imageio.ImageReadParam> readParamFunction) throws java.io.IOException
Returns aBufferedImage
as the result of decoding a suppliedURL
with anImageReader
chosen automatically from among those currently registered. AnInputStream
is obtained from theURL
, which is wrapped in anImageInputStream
. If no registeredImageReader
claims to be able to read the resulting stream,null
is returned.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching in theImageInputStream
that is created.This method does not attempt to locate
ImageReader
s that can read directly from aURL
; that may be accomplished usingIIORegistry
andImageReaderSpi
.- Parameters:
input
- aURL
to read from.readMetadata
- iftrue
, makes sure to read image metadata to detect transparency color for non translucent images, if any. In that case the color can be retrieved later throughPROP_TRANSPARENCY_COLOR
. Always consideredtrue
ifenforceTransparency
is alsotrue
enforceTransparency
- iftrue
, makes sure to read image metadata and, if the image does not provide an alpha channel but defines aTransparentColor
metadata node, that the resulting image has a transparency set toTRANSLUCENT
and uses the correct transparent color.readParamFunction
- a function to compute the read parameters from the image reader- Returns:
- a
BufferedImage
containing the decoded contents of the input, ornull
. - Throws:
java.lang.IllegalArgumentException
- ifinput
isnull
.java.io.IOException
- if an error occurs during reading.- Since:
- 17880
-
read
public static java.awt.image.BufferedImage read(javax.imageio.stream.ImageInputStream stream, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
Returns aBufferedImage
as the result of decoding a suppliedImageInputStream
with anImageReader
chosen automatically from among those currently registered. If no registeredImageReader
claims to be able to read the stream,null
is returned.Unlike most other methods in this class, this method does close the provided
ImageInputStream
after the read operation has completed, unlessnull
is returned, in which case this method does not close the stream.- Parameters:
stream
- anImageInputStream
to read from.readMetadata
- iftrue
, makes sure to read image metadata to detect transparency color for non translucent images, if any. In that case the color can be retrieved later throughPROP_TRANSPARENCY_COLOR
. Always consideredtrue
ifenforceTransparency
is alsotrue
enforceTransparency
- iftrue
, makes sure to read image metadata and, if the image does not provide an alpha channel but defines aTransparentColor
metadata node, that the resulting image has a transparency set toTRANSLUCENT
and uses the correct transparent color. For Java < 11 only.- Returns:
- a
BufferedImage
containing the decoded contents of the input, ornull
. - Throws:
java.lang.IllegalArgumentException
- ifstream
isnull
.java.io.IOException
- if an error occurs during reading.- Since:
- 7132
-
read
private static java.awt.image.BufferedImage read(javax.imageio.stream.ImageInputStream stream, boolean readMetadata, boolean enforceTransparency, java.util.function.Function<javax.imageio.ImageReader,javax.imageio.ImageReadParam> readParamFunction) throws java.io.IOException
- Throws:
java.io.IOException
-
getTransparentColor
public static java.awt.Color getTransparentColor(java.awt.image.ColorModel model, javax.imageio.ImageReader reader) throws java.io.IOException
Returns theTransparentColor
defined in image reader metadata.- Parameters:
model
- The image color modelreader
- The image reader- Returns:
- the
TransparentColor
defined in image reader metadata, ornull
- Throws:
java.io.IOException
- if an error occurs during reading- Since:
- 7499
- See Also:
- javax_imageio_1.0 metadata
-
parseRGB
private static java.awt.Color parseRGB(java.lang.String... s)
-
makeImageTransparent
public static java.awt.image.BufferedImage makeImageTransparent(java.awt.image.BufferedImage bi, java.awt.Color color)
Returns a transparent version of the given image, based on the given transparent color.- Parameters:
bi
- The image to convertcolor
- The transparent color- Returns:
- The same image as
bi
where all pixels of the given color are transparent. This resulting image has also the special propertyPROP_TRANSPARENCY_FORCED
set tocolor
- Since:
- 7132
- See Also:
BufferedImage.getProperty(java.lang.String, java.awt.image.ImageObserver)
,isTransparencyForced(java.awt.image.BufferedImage)
-
isTransparencyForced
public static boolean isTransparencyForced(java.awt.image.BufferedImage bi)
Determines if the transparency of the givenBufferedImage
has been enforced by a previous call tomakeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.- Parameters:
bi
- TheBufferedImage
to test- Returns:
true
if the transparency ofbi
has been enforced by a previous call tomakeImageTransparent
.- Since:
- 7132
- See Also:
makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
-
hasTransparentColor
public static boolean hasTransparentColor(java.awt.image.BufferedImage bi)
Determines if the givenBufferedImage
has a transparent color determined by a previous call toread(java.io.File, boolean, boolean)
.- Parameters:
bi
- TheBufferedImage
to test- Returns:
true
ifbi
has a transparent color determined by a previous call toread
.- Since:
- 7132
- See Also:
read(java.io.File, boolean, boolean)
-
shutdown
public static void shutdown(boolean now)
Shutdown background image fetcher.- Parameters:
now
- iftrue
, attempts to stop all actively executing tasks, halts the processing of waiting tasks. iffalse
, initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted- Since:
- 8412
-
toBufferedImage
public static java.awt.image.BufferedImage toBufferedImage(java.awt.Image image)
Converts anImage
to aBufferedImage
instance.- Parameters:
image
- image to convert- Returns:
- a
BufferedImage
instance for the givenImage
. - Since:
- 13038
-
toBufferedImage
public static java.awt.image.BufferedImage toBufferedImage(java.awt.Image image, java.awt.Rectangle cropArea)
Converts anRectangle
area ofImage
to aBufferedImage
instance.- Parameters:
image
- image to convertcropArea
- rectangle to crop image with- Returns:
- a
BufferedImage
instance for the cropped area ofImage
. - Since:
- 13127
-
createImageInputStream
private static javax.imageio.stream.ImageInputStream createImageInputStream(java.lang.Object input) throws java.io.IOException
- Throws:
java.io.IOException
-
createBlankIcon
public static javax.swing.ImageIcon createBlankIcon(ImageProvider.ImageSizes size)
Creates a blank icon of the given size.- Parameters:
size
- image size- Returns:
- a blank icon of the given size
- Since:
- 13984
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-