public class ImageProvider extends java.lang.Object
ImageIcon icon = new ImageProvider(name).setMaxWidth(24).setMaxHeight(24).get();
(there are more options, see below)
short form:
ImageIcon icon = ImageProvider.get(name);
Modifier and Type | Class and Description |
---|---|
static interface |
ImageProvider.ImageCallback
Callback interface for asynchronous image loading.
|
static interface |
ImageProvider.ImageResourceCallback
Callback interface for asynchronous image loading (with delayed scaling possibility).
|
static class |
ImageProvider.ImageSizes
Supported image sizes
|
static class |
ImageProvider.ImageType
Supported image types
|
static class |
ImageProvider.OverlayPosition
Position of an overlay icon
|
private static class |
ImageProvider.SAXReturnException
Quit parsing, when a certain condition is met
|
Modifier and Type | Field and Description |
---|---|
protected java.util.Collection<java.lang.ClassLoader> |
additionalClassLoaders |
protected java.io.File |
archive |
private static java.util.Map<java.lang.String,ImageResource> |
cache
The icon cache
|
private static java.util.regex.Pattern |
dataUrlPattern
data:[<mediatype>][;base64],<data> |
(package private) static double |
DEGREE_90
90 degrees in radians units
|
protected java.util.Collection<java.lang.String> |
dirs |
protected int |
height |
private static java.lang.String |
HTTP_PROTOCOL |
private static java.lang.String |
HTTPS_PROTOCOL |
protected java.lang.String |
id |
private static java.util.concurrent.ExecutorService |
IMAGE_FETCHER |
protected java.lang.String |
inArchiveDir |
protected int |
maxHeight |
protected int |
maxWidth |
protected java.lang.String |
name |
protected boolean |
optional |
static java.lang.String |
PROP_TRANSPARENCY_COLOR
Property set on
BufferedImage returned by read(java.io.File, boolean, boolean) if metadata is required. |
static java.lang.String |
PROP_TRANSPARENCY_FORCED
Property set on
BufferedImage returned by makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color) . |
private static java.util.Map<java.awt.Image,java.util.Map<java.lang.Long,ImageResource>> |
ROTATE_CACHE
Caches the image data for rotated versions of the same image.
|
protected java.lang.String |
subdir |
protected boolean |
suppressWarnings |
private static SVGUniverse |
svgUniverse |
protected int |
width |
private static java.lang.String |
WIKI_PROTOCOL |
Constructor and Description |
---|
ImageProvider(java.lang.String name)
Constructs a new
ImageProvider from a filename. |
ImageProvider(java.lang.String subdir,
java.lang.String name)
Constructs a new
ImageProvider from a filename in a given directory. |
Modifier and Type | Method and Description |
---|---|
static java.awt.Image |
createBoundedImage(java.awt.Image img,
int maxSize)
Creates a scaled down version of the input image to fit maximum dimensions.
|
static java.awt.image.BufferedImage |
createImageFromSvg(SVGDiagram svg,
java.awt.Dimension dim)
Constructs an image from the given SVG data.
|
static java.awt.Image |
createRotatedImage(java.awt.Image img,
double rotatedAngle)
Creates a rotated version of the input image.
|
static java.awt.Image |
createRotatedImage(java.awt.Image img,
double rotatedAngle,
java.awt.Dimension dimension)
Creates a rotated version of the input image, scaled to the given dimension.
|
javax.swing.ImageIcon |
get()
Execute the image request and scale result.
|
static javax.swing.ImageIcon |
get(OsmPrimitiveType type)
Replies the icon for an OSM primitive type
|
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 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.
|
static javax.swing.ImageIcon |
getIfAvailable(java.lang.String name) |
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) |
private static ImageResource |
getIfAvailableHttp(java.lang.String url,
ImageProvider.ImageType type) |
private ImageResource |
getIfAvailableImpl(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders) |
private static ImageResource |
getIfAvailableLocalURL(java.net.URL path,
ImageProvider.ImageType type) |
private static ImageResource |
getIfAvailableWiki(java.lang.String name,
ImageProvider.ImageType type) |
private static ImageResource |
getIfAvailableZip(java.lang.String fullName,
java.io.File archive,
java.lang.String inArchiveDir,
ImageProvider.ImageType type) |
static java.awt.Dimension |
getImageSizes(ImageProvider.ImageSizes size)
Convert enumerated size values to real numbers
|
private static java.net.URL |
getImageUrl(java.lang.String imageName,
java.util.Collection<java.lang.String> dirs,
java.util.Collection<java.lang.ClassLoader> additionalClassLoaders) |
private static java.net.URL |
getImageUrl(java.lang.String path,
java.lang.String name,
java.util.Collection<java.lang.ClassLoader> additionalClassLoaders) |
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.
|
void |
getInBackground(ImageProvider.ImageCallback callback)
Load the image in a background thread.
|
void |
getInBackground(ImageProvider.ImageResourceCallback callback)
Load the image in a background thread.
|
ImageResource |
getResource()
Execute the image request.
|
private static SVGUniverse |
getSvgUniverse() |
static java.awt.Color |
getTransparentColor(java.awt.image.ColorModel model,
javax.imageio.ImageReader reader)
Returns the
TransparentColor defined in image reader metadata. |
static boolean |
hasTransparentColor(java.awt.image.BufferedImage bi)
Determines if the given
BufferedImage has a transparent color determiend by a previous call to read(java.io.File, boolean, boolean) . |
static boolean |
isTransparencyForced(java.awt.image.BufferedImage bi)
Determines if the transparency of the given
BufferedImage has been enforced by a previous call to makeImageTransparent(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.
|
static javax.swing.ImageIcon |
overlay(javax.swing.Icon ground,
javax.swing.Icon overlay,
ImageProvider.OverlayPosition pos)
Deprecated.
this method will be refactored
|
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 a
BufferedImage as the result of decoding
a supplied File with an ImageReader
chosen automatically from among those currently registered. |
static java.awt.image.BufferedImage |
read(javax.imageio.stream.ImageInputStream stream,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied ImageInputStream with an
ImageReader chosen automatically from among those
currently registered. |
static java.awt.image.BufferedImage |
read(java.io.InputStream input,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied InputStream with an ImageReader
chosen automatically from among those currently registered. |
static java.awt.image.BufferedImage |
read(java.net.URL input,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied URL with an ImageReader
chosen automatically from among those currently registered. |
ImageProvider |
setAdditionalClassLoaders(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
Add a collection of additional class loaders to search image for.
|
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 |
setHeight(int height) |
ImageProvider |
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) |
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 |
setMaxSize(int maxSize)
Convenience method, see
setMaxSize(Dimension) . |
ImageProvider |
setMaxWidth(int maxWidth) |
ImageProvider |
setOptional(boolean optional)
Decide, if an exception should be thrown, when the image cannot be located.
|
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) |
private static final java.lang.String HTTP_PROTOCOL
private static final java.lang.String HTTPS_PROTOCOL
private static final java.lang.String WIKI_PROTOCOL
public static java.lang.String PROP_TRANSPARENCY_FORCED
BufferedImage
returned by makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.public static java.lang.String PROP_TRANSPARENCY_COLOR
BufferedImage
returned by read(java.io.File, boolean, boolean)
if metadata is required.protected java.util.Collection<java.lang.String> dirs
protected java.lang.String id
protected java.lang.String subdir
protected java.lang.String name
protected java.io.File archive
protected java.lang.String inArchiveDir
protected int width
protected int height
protected int maxWidth
protected int maxHeight
protected boolean optional
protected boolean suppressWarnings
protected java.util.Collection<java.lang.ClassLoader> additionalClassLoaders
private static SVGUniverse svgUniverse
private static final java.util.Map<java.lang.String,ImageResource> cache
private static final java.util.Map<java.awt.Image,java.util.Map<java.lang.Long,ImageResource>> ROTATE_CACHE
private static final java.util.concurrent.ExecutorService IMAGE_FETCHER
private static final java.util.regex.Pattern dataUrlPattern
data:[<mediatype>][;base64],<data>
static final double DEGREE_90
public ImageProvider(java.lang.String subdir, java.lang.String name)
ImageProvider
from a filename in a given directory.subdir
- subdirectory the image lies inname
- the name of the image. If it does not end with '.png' or '.svg',
both extensions are tried.public ImageProvider(java.lang.String name)
ImageProvider
from a filename.name
- the name of the image. If it does not end with '.png' or '.svg',
both extensions are tried.public ImageProvider setDirs(java.util.Collection<java.lang.String> dirs)
dirs
- The directories to look for.public ImageProvider setId(java.lang.String id)
public ImageProvider setArchive(java.io.File archive)
public ImageProvider setInArchiveDir(java.lang.String inArchiveDir)
public static java.awt.Dimension getImageSizes(ImageProvider.ImageSizes size)
size
- the size enumerationpublic ImageProvider setSize(java.awt.Dimension size)
public ImageProvider setSize(ImageProvider.ImageSizes size)
public ImageProvider setWidth(int width)
setSize(java.awt.Dimension)
public ImageProvider setHeight(int height)
setSize(java.awt.Dimension)
public ImageProvider setMaxSize(java.awt.Dimension maxSize)
public ImageProvider setMaxSize(ImageProvider.ImageSizes size)
public ImageProvider setMaxSize(int maxSize)
setMaxSize(Dimension)
.public ImageProvider setMaxWidth(int maxWidth)
setMaxSize(java.awt.Dimension)
public ImageProvider setMaxHeight(int maxHeight)
setMaxSize(java.awt.Dimension)
public ImageProvider setOptional(boolean optional)
optional
- true, if JOSM should not throw a RuntimeException
in case the image cannot be located.public ImageProvider setSuppressWarnings(boolean suppressWarnings)
public ImageProvider setAdditionalClassLoaders(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
public javax.swing.ImageIcon get()
public ImageResource getResource()
public void getInBackground(ImageProvider.ImageCallback callback)
callback
- a callback. It is called, when the image is ready.
This can happen before the call to this method returns or it may be
invoked some time (seconds) later. If no image is available, a null
value is returned to callback (just like get()
).public void getInBackground(ImageProvider.ImageResourceCallback callback)
callback
- a callback. It is called, when the image is ready.
This can happen before the call to this method returns or it may be
invoked some time (seconds) later. If no image is available, a null
value is returned to callback (just like get()
).public static javax.swing.ImageIcon get(java.lang.String subdir, java.lang.String name)
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)java.lang.RuntimeException
- if the image cannot be locatedpublic static javax.swing.ImageIcon get(java.lang.String name)
name
- The icon name (base name with or without '.png' or '.svg' extension)get(String, String)
public static javax.swing.ImageIcon getIfAvailable(java.lang.String subdir, java.lang.String name)
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)get(String, String)
public static javax.swing.ImageIcon getIfAvailable(java.lang.String name)
name
- The icon name (base name with or without '.png' or '.svg' extension)getIfAvailable(String, String)
private ImageResource getIfAvailableImpl(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
private static ImageResource getIfAvailableHttp(java.lang.String url, ImageProvider.ImageType type)
private static ImageResource getIfAvailableDataUrl(java.lang.String url)
private static ImageResource getIfAvailableWiki(java.lang.String name, ImageProvider.ImageType type)
private static ImageResource getIfAvailableZip(java.lang.String fullName, java.io.File archive, java.lang.String inArchiveDir, ImageProvider.ImageType type)
private static ImageResource getIfAvailableLocalURL(java.net.URL path, ImageProvider.ImageType type)
private static java.net.URL getImageUrl(java.lang.String path, java.lang.String name, java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
private static java.net.URL getImageUrl(java.lang.String imageName, java.util.Collection<java.lang.String> dirs, java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
private static java.lang.String getImgUrlFromWikiInfoPage(java.lang.String base, java.lang.String fn)
public static java.awt.Cursor getCursor(java.lang.String name, java.lang.String overlay)
name
- the cursor image filename in "cursor" directoryoverlay
- optional overlay image@Deprecated public static javax.swing.ImageIcon overlay(javax.swing.Icon ground, javax.swing.Icon overlay, ImageProvider.OverlayPosition pos)
ground
- the base imageoverlay
- the overlay image (can be smaller than the base image)pos
- position of the overlay image inside the base image (positioned
in one of the corners)public static java.awt.Image createRotatedImage(java.awt.Image img, double rotatedAngle)
img
- the image to be rotated.rotatedAngle
- the rotated angle, in degree, clockwise. It could be any double but we
will mod it with 360 before using it. More over for caching performance, it will be rounded to
an entire value between 0 and 360.public static java.awt.Image createRotatedImage(java.awt.Image img, double rotatedAngle, java.awt.Dimension dimension)
img
- the image to be rotated.rotatedAngle
- the rotated angle, in degree, clockwise. It could be any double but we
will mod it with 360 before using it. More over for caching performance, it will be rounded to
an entire value between 0 and 360.dimension
- The requested dimensions. Use (-1,-1) for the original size
and (width, -1) to set the width, but otherwise scale the image proportionally.public static java.awt.Image createBoundedImage(java.awt.Image img, int maxSize)
img
- the image to be scaled down.maxSize
- the maximum size in pixels (both for width and height)public static javax.swing.ImageIcon get(OsmPrimitiveType type)
type
- the typepublic static java.awt.image.BufferedImage createImageFromSvg(SVGDiagram svg, java.awt.Dimension dim)
svg
- the SVG datadim
- the desired image dimensionprivate static SVGUniverse getSvgUniverse()
public static java.awt.image.BufferedImage read(java.io.File input, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
BufferedImage
as the result of decoding
a supplied File
with an ImageReader
chosen automatically from among those currently registered.
The File
is wrapped in an
ImageInputStream
. If no registered
ImageReader
claims to be able to read the
resulting stream, null
is returned.
The current cache settings from getUseCache
and
getCacheDirectory
will be used to control caching in the
ImageInputStream
that is created.
Note that there is no read
method that takes a
filename as a String
; use this method instead after
creating a File
from the filename.
This method does not attempt to locate
ImageReader
s that can read directly from a
File
; that may be accomplished using
IIORegistry
and ImageReaderSpi
.
input
- a File
to read from.readMetadata
- if true
, makes sure to read image metadata to detect transparency color, if any.
In that case the color can be retrieved later through PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded
contents of the input, or null
.java.lang.IllegalArgumentException
- if input
is null
.java.io.IOException
- if an error occurs during reading.BufferedImage.getProperty(java.lang.String, java.awt.image.ImageObserver)
public static java.awt.image.BufferedImage read(java.io.InputStream input, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
BufferedImage
as the result of decoding
a supplied InputStream
with an ImageReader
chosen automatically from among those currently registered.
The InputStream
is wrapped in an
ImageInputStream
. If no registered
ImageReader
claims to be able to read the
resulting stream, null
is returned.
The current cache settings from getUseCache
and
getCacheDirectory
will be used to control caching in the
ImageInputStream
that is created.
This method does not attempt to locate
ImageReader
s that can read directly from an
InputStream
; that may be accomplished using
IIORegistry
and ImageReaderSpi
.
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.
input
- an InputStream
to read from.readMetadata
- if true
, 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 through PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded
contents of the input, or null
.java.lang.IllegalArgumentException
- if input
is null
.java.io.IOException
- if an error occurs during reading.public static java.awt.image.BufferedImage read(java.net.URL input, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
BufferedImage
as the result of decoding
a supplied URL
with an ImageReader
chosen automatically from among those currently registered. An
InputStream
is obtained from the URL
,
which is wrapped in an ImageInputStream
. If no
registered ImageReader
claims to be able to read
the resulting stream, null
is returned.
The current cache settings from getUseCache
and
getCacheDirectory
will be used to control caching in the
ImageInputStream
that is created.
This method does not attempt to locate
ImageReader
s that can read directly from a
URL
; that may be accomplished using
IIORegistry
and ImageReaderSpi
.
input
- a URL
to read from.readMetadata
- if true
, 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 through PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded
contents of the input, or null
.java.lang.IllegalArgumentException
- if input
is null
.java.io.IOException
- if an error occurs during reading.public static java.awt.image.BufferedImage read(javax.imageio.stream.ImageInputStream stream, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
BufferedImage
as the result of decoding
a supplied ImageInputStream
with an
ImageReader
chosen automatically from among those
currently registered. If no registered
ImageReader
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, unless null
is returned,
in which case this method does not close the stream.
stream
- an ImageInputStream
to read from.readMetadata
- if true
, 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 through PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded
contents of the input, or null
.java.lang.IllegalArgumentException
- if stream
is null
.java.io.IOException
- if an error occurs during reading.public static java.awt.Color getTransparentColor(java.awt.image.ColorModel model, javax.imageio.ImageReader reader) throws java.io.IOException
TransparentColor
defined in image reader metadata.model
- The image color modelreader
- The image readerTransparentColor
defined in image reader metadata, or null
java.io.IOException
- if an error occurs during readingprivate static java.awt.Color parseRGB(java.lang.String[] s)
public static java.awt.image.BufferedImage makeImageTransparent(java.awt.image.BufferedImage bi, java.awt.Color color)
bi
- The image to convertcolor
- The transparent colorbi
where all pixels of the given color are transparent.
This resulting image has also the special property PROP_TRANSPARENCY_FORCED
set to color
BufferedImage.getProperty(java.lang.String, java.awt.image.ImageObserver)
,
isTransparencyForced(java.awt.image.BufferedImage)
public static boolean isTransparencyForced(java.awt.image.BufferedImage bi)
BufferedImage
has been enforced by a previous call to makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.bi
- The BufferedImage
to testtrue
if the transparency of bi
has been enforced by a previous call to makeImageTransparent
.makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
public static boolean hasTransparentColor(java.awt.image.BufferedImage bi)
BufferedImage
has a transparent color determiend by a previous call to read(java.io.File, boolean, boolean)
.bi
- The BufferedImage
to testtrue
if bi
has a transparent color determined by a previous call to read
.read(java.io.File, boolean, boolean)