public class IconsFactory
extends java.lang.Object
IconsFactory
provides a consistent way to access icon resource in any application.
Any application usually need to access image files. One way to do it is to put those image files in the installation and access them use direct file access. However this is not a good way because you have to know the full path to the image file. So a better way that most Java applications take is to bundle the image files with in the jar and use class loader to load them.
For example, if a class Foo needs to access image files foo.gif and bar.png, we put the image files right below the source code under icons subfolder. See an example directory structure below.
/src/com/jidesoft/Foo.java /icons/foo.gif /icons/bar.pngWhen you compile the java class, you copy those images to class output directory like this.
/classes/com/jidesoft/Foo.class /icons/foo.gif /icons/bar.pngNotes: In IntelliJ IDEA's "Compile" tab of "Project Property" dialog, there is a way to set "Resource Pattern". Here is the setting on my computer - "?*.properties;?*.xml;?*.html;?*.tree;?*.gif;?*.png;?*.jpeg;?*.jpg;?*.vm;?*.xsd;?*.ilayout;?*.gz;?*.txt" for your reference. If so, all your images will get copies automatically to class output folder. Although I haven't tried, I believe most Java IDEs have the same or similar feature. This feature will make the usage of IconsFactory much easier.
If you setup directory structure as above, you can now use IconsFactory to access the images like this.
ImageIcon icon = IconsFactory.get(Foo.class, "icons/foo.gif");
IconsFactory will cache the icon for you. So next time if you get the same icon, it will get from cache instead of
reading from disk again.
There are a few methods on IconsFactory to create difference variation from the original icon. For example, getDisabledImageIcon(Class, String)
will get the image icon with disabled effect.
We also suggest you to use the template below to create a number of IconsFactory classes in your application. The idea is that you should have one for each functional area so that all your image files can be grouped into each functional area. All images used in that functional area should be put under the folder where this IconsFactory is. Here is an template.
class TemplateIconsFactory {
public static class Group1 {
public static final String IMAGE1 = "icons/image11.png";
public static final String IMAGE2 = "icons/image12.png";
public static final String IMAGE3 = "icons/image13.png";
}
public static class Group2 {
public static final String IMAGE1 = "icons/image21.png";
public static final String IMAGE2 = "icons/image22.png";
public static final String IMAGE3 = "icons/image23.png";
}
public static ImageIcon getImageIcon(String name) {
if (name != null)
return IconsFactory.getImageIcon(TemplateIconsFactory.class, name);
else
return null;
}
public static void main(String[] argv) {
IconsFactory.generateHTML(TemplateIconsFactory.class);
}
}
In your own IconsFactory, you can further divide images into different groups. The example above has two groups.
There is also a convenient method getImageIcon() which takes just the icon name.
In the template, we defined the image names as constants. When you have a lot of images, it's hard to remember all of them when writing code. If using the IconsFactory above, you can use
ImageIcon icon = TemplateIconsFactory.getImageIcon(TemplateIconsFactory.Group1.IMAGE1);
without saying the actual image file name. With the help of intelli-sense (or code completion) feature in most Java
IDE, you will find it is much easier to find the icons you want. You can refer to JIDE Components Developer Guide to
see a screenshot of what it looks like in IntelliJ IDEA.
You probably also notice this is a main() method in this template. You can run it. When you run, you will see a message printed out like this.
"File is generated at "... some directory ...\com.jidesoft.icons.TemplateIconsFactory.html".
Please copy it to the same directory as TemplateIconsFactory.java"
if you follow the instruction and copy the html file to the same location as the source code and open the html, you
will see the all image files defined in this IconsFactory are listed nicely in the page.
By default, all image files are loaded using ImageIO. However if you set system property "jide.useImageIO" to "false", we will disable the usage of ImageIO and use Toolkit.getDefaultToolkit().createImage method to create the image file.
Modifier and Type | Field | Description |
---|---|---|
static javax.swing.ImageIcon |
EMPTY_ICON |
Constructor | Description |
---|---|
IconsFactory() |
Modifier and Type | Method | Description |
---|---|---|
static javax.swing.ImageIcon |
createBrighterImage(java.awt.Component c,
javax.swing.Icon icon) |
Creates a gray version from an input image.
|
static javax.swing.ImageIcon |
createBrighterImage(java.awt.Component c,
javax.swing.Icon icon,
int percent) |
Creates a gray version from an input image with a given percentage of brightness.
|
static javax.swing.ImageIcon |
createBrighterImage(java.awt.Image image) |
Creates a brighter image from an input image.
|
static javax.swing.ImageIcon |
createBrighterImage(java.awt.Image image,
int percent) |
Creates a brighter image from an input image with a given percentage of brightness.
|
static javax.swing.ImageIcon |
createBrighterImage(javax.swing.ImageIcon icon) |
Creates a brighten version from an input ImageIcon.
|
static javax.swing.ImageIcon |
createBrighterImage(javax.swing.ImageIcon icon,
int percent) |
Creates a brighter image from an input image with a given percentage of brightness.
|
static javax.swing.ImageIcon |
createGrayImage(java.awt.Component c,
javax.swing.Icon icon) |
Creates a gray version from an input image.
|
static javax.swing.ImageIcon |
createGrayImage(java.awt.Image image) |
Creates a gray version from an input image.
|
static java.awt.image.BufferedImage |
createImage(java.awt.Component component) |
Creates a buffered image of type TYPE_INT_RGB from the supplied component.
|
static java.awt.image.BufferedImage |
createImage(java.awt.Component component,
int imageType) |
Creates a buffered image (of the specified type) from the supplied component.
|
static java.awt.image.BufferedImage |
createImage(java.awt.Component component,
java.awt.Rectangle bounds,
int imageType) |
Creates a buffered image (of the specified type) from the supplied component.
|
static java.awt.image.BufferedImage |
createImage(java.awt.Component component,
java.awt.Rectangle bounds,
int imageType,
int scale) |
Creates a buffered image (of the specified type) from the supplied component.
|
static java.awt.Image |
createImage(java.lang.String path) |
Creates an image from a file on the classpath
|
static javax.swing.ImageIcon |
createMaskImage(java.awt.Component c,
javax.swing.Icon icon,
java.awt.Color oldColor,
java.awt.Color newColor) |
Creates a version from an input image which replaces one color with another color.
|
static javax.swing.ImageIcon |
createNegativeImage(java.awt.Component c,
javax.swing.Icon icon) |
Creates a negative version from an input black image which basically replaces black pixel with white pixel.
|
static javax.swing.ImageIcon |
createNegativeImage(java.awt.Image image) |
Creates a gray version from an input image.
|
static javax.swing.ImageIcon |
createRotatedImage(java.awt.Component c,
javax.swing.Icon icon,
double rotatedAngle) |
Creates a rotated version of the input image.
|
static java.awt.TexturePaint |
createTexture(javax.swing.JComponent observer,
java.lang.String fileName) |
Utility method to create a texture paint from a graphics file
|
static javax.swing.ImageIcon |
createThumbnail(java.awt.Component component,
int width,
int height) |
Creates a thumbnail from the supplied component (such as a chart).
|
static java.awt.Image |
createThumbnailImage(java.awt.Component component,
int width,
int height) |
Creates a thumbnail from the supplied component (such as a chart).
|
static javax.swing.ImageIcon |
createTintedImage(javax.swing.ImageIcon icon,
java.awt.Color color) |
Creates a tinted image from an input image with a given color.
|
static javax.swing.ImageIcon |
findImageIcon(java.lang.Class<?> clazz,
java.lang.String fileName) |
Gets ImageIcon by passing class and a relative image file path.
|
static void |
generateHTML(java.lang.Class<?> clazz) |
Generates HTML that lists all icons in IconsFactory.
|
static javax.swing.ImageIcon |
getBrighterImageIcon(java.lang.Class<?> clazz,
java.lang.String fileName) |
Gets a brighter ImageIcon by passing class and a relative image file path.
|
static javax.swing.ImageIcon |
getBrighterImageIcon(java.lang.Class<?> clazz,
java.lang.String fileName,
int percent) |
Gets a brighter ImageIcon by passing class, a relative image file path and a percentage of brightness.
|
static javax.swing.ImageIcon |
getCombinedIcon(java.awt.Component c,
javax.swing.ImageIcon icon1,
javax.swing.ImageIcon icon2,
int orientation,
int gap) |
Gets a new icon with the icon2 painting right or down to the icon1.
|
static javax.swing.ImageIcon |
getDisabledImageIcon(java.lang.Class<?> clazz,
java.lang.String fileName) |
Gets a disabled version of ImageIcon by passing class and a relative image file path.
|
static javax.swing.ImageIcon |
getIcon(java.awt.Component c,
javax.swing.ImageIcon icon,
int x,
int y,
int width,
int height) |
Gets part of the image from input image icon.
|
static javax.swing.ImageIcon |
getIcon(java.awt.Component c,
javax.swing.ImageIcon icon,
int x,
int y,
int width,
int height,
int imageType) |
Gets part of the image from input image icon.
|
static javax.swing.ImageIcon |
getIcon(java.awt.Component c,
javax.swing.ImageIcon icon,
int x,
int y,
int width,
int height,
int destWidth,
int destHeight) |
Gets part of the image from input image icon.
|
static javax.swing.ImageIcon |
getIcon(java.awt.Component c,
javax.swing.ImageIcon icon,
int x,
int y,
int width,
int height,
int imageType,
int destWidth,
int destHeight) |
Gets part of the image from input image icon.
|
static javax.swing.ImageIcon |
getImageIcon(java.lang.Class<?> clazz,
java.lang.String fileName) |
Gets ImageIcon by passing class and a relative image file path.
|
static javax.swing.ImageIcon |
getOverlayIcon(java.awt.Component c,
javax.swing.ImageIcon icon,
javax.swing.ImageIcon overlayIcon,
int location) |
Gets a new icon with the overlayIcon paints over the original icon.
|
static javax.swing.ImageIcon |
getOverlayIcon(java.awt.Component c,
javax.swing.ImageIcon icon,
javax.swing.ImageIcon overlayIcon,
int x,
int y) |
Gets a new icon with the overlayIcon paints over the original icon.
|
static javax.swing.ImageIcon |
getOverlayIcon(java.awt.Component c,
javax.swing.ImageIcon icon,
javax.swing.ImageIcon overlayIcon,
int location,
java.awt.Insets insets) |
Gets a new icon with the overlayIcon paints over the original icon.
|
static javax.swing.ImageIcon |
getScaledImage(java.awt.Component c,
javax.swing.ImageIcon icon,
int w,
int h) |
Gets a scaled version of the existing icon.
|
static javax.swing.ImageIcon |
getTintedImageIcon(java.lang.Class<?> clazz,
java.lang.String fileName,
java.awt.Color color) |
Gets a tinted ImageIcon by passing class, a relative image file path and a color.
|
static void |
writeGifToFile(java.awt.Component c,
java.io.File file) |
Writes a GIF image of the supplied component to the given file.
|
static void |
writeJpegToFile(java.awt.Component c,
java.io.File file) |
Writes a JPEG image of the supplied component to the given file.
|
static void |
writePngToFile(java.awt.Component c,
java.io.File file) |
Writes a PNG image of the supplied component to the given file.
|
static void |
writeToStream(java.awt.Component c,
java.io.OutputStream stream) |
Paints the component as a PNG image to the supplied output stream
|
public static javax.swing.ImageIcon getImageIcon(java.lang.Class<?> clazz, java.lang.String fileName)
Please note, getImageIcon will print out error message to stderr if image is not found. The reason we did so is
because we want you to make sure all image files are there in your application. If you ever see the error
message, you should correct it before shipping the product. But if you just want to test if the image file is
there, you don't want any error message print out. If so, you can use findImageIcon(Class, String)
method. It will throw IOException when image is not found.
We used this method to create all the icons we used in JIDE's code. If you ever want to use your own icon instead
of JIDE's default icon, you just need to put it onto UIManager. For example, AutoFilterTableHeader uses an icon
on the table header. This is how it was called.
IconsFactory.getImageIcon(AutoFilterTableHeader.class,
"icons/filterYes_over.png")
The key for this icon is "com.jidesoft.grid.AutoFilterTableHeader:icons/filterYes_over.png". So you can call the code below to register your own icon.
UIManager.put("com.jidesoft.grid.AutoFilterTableHeader:icons/filterYes_over.png",
your_new_icon);
If you don't know what key to use, just put a breakpoint at this method, run it to inspect the id variable below.
clazz
- the Class>fileName
- relative file namepublic static javax.swing.ImageIcon findImageIcon(java.lang.Class<?> clazz, java.lang.String fileName) throws java.io.IOException
clazz
- the Class>fileName
- relative file namejava.io.IOException
- when image file is not found.public static javax.swing.ImageIcon getDisabledImageIcon(java.lang.Class<?> clazz, java.lang.String fileName)
clazz
- the Class>fileName
- relative file namepublic static javax.swing.ImageIcon getBrighterImageIcon(java.lang.Class<?> clazz, java.lang.String fileName)
clazz
- the Class>fileName
- relative file namepublic static javax.swing.ImageIcon getBrighterImageIcon(java.lang.Class<?> clazz, java.lang.String fileName, int percent)
clazz
- the Class>fileName
- relative file namepercent
- percentage of brightnesspublic static javax.swing.ImageIcon getTintedImageIcon(java.lang.Class<?> clazz, java.lang.String fileName, java.awt.Color color)
clazz
- the Class>fileName
- relative file namecolor
- the colorpublic static javax.swing.ImageIcon createGrayImage(java.awt.Image image)
image
- imagepublic static javax.swing.ImageIcon createGrayImage(java.awt.Component c, javax.swing.Icon icon)
c
- The component to get properties useful for painting, e.g. the foreground or background color.icon
- iconpublic static javax.swing.ImageIcon createBrighterImage(java.awt.Image image)
image
- imagepublic static javax.swing.ImageIcon createBrighterImage(java.awt.Image image, int percent)
image
- imagepercent
- percentage of brightnesspublic static javax.swing.ImageIcon createBrighterImage(java.awt.Component c, javax.swing.Icon icon)
c
- The component to get properties useful for painting, e.g. the foreground or background color.icon
- iconpublic static javax.swing.ImageIcon createBrighterImage(java.awt.Component c, javax.swing.Icon icon, int percent)
c
- The component to get properties useful for painting, e.g. the foreground or background color.icon
- iconpercent
- percentage of brightnesspublic static javax.swing.ImageIcon createBrighterImage(javax.swing.ImageIcon icon)
icon
- imagepublic static javax.swing.ImageIcon createBrighterImage(javax.swing.ImageIcon icon, int percent)
icon
- imagepercent
- percentage of brightnesspublic static javax.swing.ImageIcon createTintedImage(javax.swing.ImageIcon icon, java.awt.Color color)
icon
- imagecolor
- the colorpublic static javax.swing.ImageIcon createNegativeImage(java.awt.Image image)
image
- imagepublic static javax.swing.ImageIcon createMaskImage(java.awt.Component c, javax.swing.Icon icon, java.awt.Color oldColor, java.awt.Color newColor)
c
- The component to get properties useful for painting, e.g. the foreground or background color.icon
- iconoldColor
- the old color to be replaced.newColor
- the new color that will replace the old color.public static javax.swing.ImageIcon createRotatedImage(java.awt.Component c, javax.swing.Icon icon, double rotatedAngle)
c
- The component to get properties useful for painting, e.g. the foreground or background
color.icon
- 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.public static javax.swing.ImageIcon createNegativeImage(java.awt.Component c, javax.swing.Icon icon)
c
- The component to get properties useful for painting, e.g. the foreground or background color.icon
- iconpublic static void generateHTML(java.lang.Class<?> clazz)
clazz
- the IconsFactory classpublic static javax.swing.ImageIcon getIcon(java.awt.Component c, javax.swing.ImageIcon icon, int x, int y, int width, int height)
c
- the component where the returned icon will be used. The component is used as the ImageObserver. It
could be null.icon
- the original icon. This is the larger icon where a sub-image will be created using this method.x
- the x location of the sub-image, relative to the original icon.y
- the y location of the sub-image, relative to the original icon.width
- the width of the sub-image. It should be less than the width of the original icon.height
- the height of the sub-image. It should be less than the height of the original icon.public static javax.swing.ImageIcon getIcon(java.awt.Component c, javax.swing.ImageIcon icon, int x, int y, int width, int height, int destWidth, int destHeight)
c
- the component where the returned icon will be used. The component is used as the ImageObserver.
It could be null.icon
- the original icon. This is the larger icon where a sub-image will be created using this
method.x
- the x location of the sub-image, relative to the original icon.y
- the y location of the sub-image, relative to the original icon.width
- the width of the sub-image. It should be less than the width of the original icon.height
- the height of the sub-image. It should be less than the height of the original icon.destWidth
- the width of the returned icon. The sub-image will be resize if the destWidth is not the same
as the width.destHeight
- the height of the returned icon. The sub-image will be resize if the destHeight is not the same
as the height.public static javax.swing.ImageIcon getIcon(java.awt.Component c, javax.swing.ImageIcon icon, int x, int y, int width, int height, int imageType)
c
- the component where the returned icon will be used. The component is used as the ImageObserver.
It could be null.icon
- the original icon. This is the larger icon where a small icon will be created using this
method.x
- the x location of the smaller icon, relative to the original icon.y
- the y location of the smaller icon, relative to the original icon.width
- the width of the smaller icon. It should be less than the width of the original icon.height
- the height of the smaller icon. It should be less than the height of the original icon.imageType
- image type is defined in BufferedImage
, such as BufferedImage.TYPE_INT_ARGB
,
BufferedImage.TYPE_INT_RGB
etc.public static javax.swing.ImageIcon getIcon(java.awt.Component c, javax.swing.ImageIcon icon, int x, int y, int width, int height, int imageType, int destWidth, int destHeight)
c
- the component where the returned icon will be used. The component is used as the ImageObserver.
It could be null.icon
- the original icon. This is the larger icon where a sub-image will be created using this
method.x
- the x location of the sub-image, relative to the original icon.y
- the y location of the sub-image, relative to the original icon.width
- the width of the sub-image. It should be less than the width of the original icon.height
- the height of the sub-image. It should be less than the height of the original icon.imageType
- image type is defined in BufferedImage
, such as BufferedImage.TYPE_INT_ARGB
,
BufferedImage.TYPE_INT_RGB
etc.destWidth
- the width of the returned icon. The sub-image will be resize if the destWidth is not the same
as the width.destHeight
- the height of the returned icon. The sub-image will be resize if the destHeight is not the same
as the height.public static javax.swing.ImageIcon getOverlayIcon(java.awt.Component c, javax.swing.ImageIcon icon, javax.swing.ImageIcon overlayIcon, int location)
c
- the component where the returned icon will be used. The component is used as the
ImageObserver. It could be null.icon
- the original iconoverlayIcon
- the overlay icon.location
- the location as defined in SwingConstants - CENTER, NORTH, SOUTH, WEST, EAST, NORTH_EAST,
NORTH_WEST, SOUTH_WEST and SOUTH_EAST.public static javax.swing.ImageIcon getOverlayIcon(java.awt.Component c, javax.swing.ImageIcon icon, javax.swing.ImageIcon overlayIcon, int location, java.awt.Insets insets)
c
- the component where the returned icon will be used. The component is used as the
ImageObserver. It could be null.icon
- the original iconoverlayIcon
- the overlay icon.location
- the location as defined in SwingConstants - CENTER, NORTH, SOUTH, WEST, EAST, NORTH_EAST,
NORTH_WEST, SOUTH_WEST and SOUTH_EAST.insets
- the insets to the border. This parameter has no effect if the location is CENTER. For example,
if the location is WEST, insets.left will be the gap of the left side of the original icon and
the left side of the overlay icon.public static javax.swing.ImageIcon getOverlayIcon(java.awt.Component c, javax.swing.ImageIcon icon, javax.swing.ImageIcon overlayIcon, int x, int y)
c
- the component where the returned icon will be used. The component is used as the
ImageObserver. It could be null.icon
- the original iconoverlayIcon
- the overlay icon.x
- the x location relative to the original icon where the overlayIcon will be pained.y
- the y location relative to the original icon where the overlayIcon will be pained.public static javax.swing.ImageIcon getCombinedIcon(java.awt.Component c, javax.swing.ImageIcon icon1, javax.swing.ImageIcon icon2, int orientation, int gap)
c
- the component where the returned icon will be used. The component is used as the
ImageObserver. It could be nullicon1
- the left side or up side iconicon2
- the right side or down side iconorientation
- the orientation as defined in SwingConstants - HORIZONTAL, VERTICALgap
- the gap between the two iconspublic static javax.swing.ImageIcon getScaledImage(java.awt.Component c, javax.swing.ImageIcon icon, int w, int h)
c
- the component where the returned icon will be used. The component is used as the ImageObserver. It
could be null.icon
- the original iconw
- the new widthh
- the new heightpublic static void writeGifToFile(java.awt.Component c, java.io.File file) throws java.io.FileNotFoundException
c
- the component to save as an imagefile
- the file to save it tojava.io.FileNotFoundException
- if the file exists but is a directory rather than a regular file, does not exist
but cannot be created, or cannot be opened for any other reasonpublic static void writeJpegToFile(java.awt.Component c, java.io.File file) throws java.io.FileNotFoundException
c
- the component to save as an imagefile
- the file to save it tojava.io.FileNotFoundException
- if the file exists but is a directory rather than a regular file, does not exist
but cannot be created, or cannot be opened for any other reasonpublic static void writePngToFile(java.awt.Component c, java.io.File file) throws java.io.FileNotFoundException
c
- the component to save as an imagefile
- the file to save it tojava.io.FileNotFoundException
- if the file exists but is a directory rather than a regular file, does not exist
but cannot be created, or cannot be opened for any other reasonpublic static void writeToStream(java.awt.Component c, java.io.OutputStream stream)
c
- the component to capture as a PNG imagestream
- the stream to write the PNG data topublic static java.awt.image.BufferedImage createImage(java.awt.Component component)
component
- the component to drawpublic static java.awt.image.BufferedImage createImage(java.awt.Component component, java.awt.Rectangle bounds, int imageType)
component
- the component to drawbounds
- the area relative to the component where the image will be created.imageType
- the type of buffered image to drawpublic static java.awt.image.BufferedImage createImage(java.awt.Component component, int imageType)
component
- the component to drawimageType
- the type of buffered image to drawpublic static java.awt.image.BufferedImage createImage(java.awt.Component component, java.awt.Rectangle bounds, int imageType, int scale)
This method will consider the scale factor for hi-def display such as retina display. If the scale factor is 2, the returned image size will double the size of the bounds. When you paint the returned image, you should call g.scale(0.5, 0.5) or SystemInfo.endScale(g, SystemInfo.getDisplayScale()) to paint the image to its normal size. For performance consideration, you may want to keep the return value of SystemInfo.getDisplayScale() instead of calling it all the time.
component
- the component to drawbounds
- the area relative to the component where the image will be created.imageType
- the type of buffered image to drawscale
- the scale factor of the displaypublic static java.awt.Image createThumbnailImage(java.awt.Component component, int width, int height)
component
- the component from which we would like to generate a thumbnailwidth
- the width of the thumbnailheight
- the height of the thumbnailpublic static javax.swing.ImageIcon createThumbnail(java.awt.Component component, int width, int height)
component
- the component from which we would like to generate a thumbnailwidth
- the width of the thumbnailheight
- the height of the thumbnailpublic static java.awt.TexturePaint createTexture(javax.swing.JComponent observer, java.lang.String fileName)
observer
- the observer to be informed when the texture image has been drawnfileName
- the name of a file on the classpath, e.g., com/mycompany/project/images/widget.gifpublic static java.awt.Image createImage(java.lang.String path)
path
- the path to the file