Interface ImageResource

All Superinterfaces:
NamedResource

public interface ImageResource extends NamedResource

Interface to be implemented by named image resources. This indicates (for testing) that the associated resource can be opened as an image (by ImageJ). Extends interface NamedResource by adding method getImagePlus(), which returns an ImagePlus instance. By default, image files are assumed to reside in a directory at the same level and with exactly the same name as the defining enum class itself. E.g., in a standard Maven-setup this is:

 .../java/    .../bar/MyImageResource.java          = enum class implementing 'ImageResource'
 .../resource/.../bar/MyImageResource/image1.png    = associated image files
 .../resource/.../bar/MyImageResource/image2.tif
 .../resource/.../bar/MyImageResource/...

For example, given a named resource MyImages.image1, this can be used simply in the form

 ImagePlus im = MyImages.image1.getImage();
 im.show();

By default, resource file names are derived automatically from the enum item's name (by method autoName()). If some other behavior is needed, method getFileName() should be overridden.

  • Field Details

  • Method Details

    • getImagePlus

      Opens end returns a ImagePlus instance for this ImageResource.
      Returns:
      a ImagePlus instance
    • getFileName

      default String getFileName()
      Description copied from interface: NamedResource
      Returns the file name for the associated resource (to be implemented by concrete classes).
      Specified by:
      getFileName in interface NamedResource
      Returns:
      the name of the resource file
    • autoName

      default String autoName()

      Derives and returns a filename for this resource item to be used in parameterless enum constructors. By default the file name is identical to the name of the enum constant supplemented with a ".png" extension. No separate file name needs to be supplied. A different file extension may be specified by having the enum name end with an underscore followed by a valid image file extension, that is, "png", "tif", "tiff", "jpg", "jpeg" or "gif". In this case, the last underscore of the enum name is replaced by a '.' character to form the file name. (Note that '.' is no legal character in a Java identifier, thus cannot be used for the enum name directly.) If the last underscore in a item's name is not followed by a valid extension, the default case is assumed ("png").

      Examples:

       enum DummyNamedResource implements ImageResource {
              a,                  // file "a.png"
              A_png,              // file "A.png"
              foo_tif,            // file "foo.tif"
              foo_tiff,           // file "foo.tiff"
              The_File_jpg,       // file "The_File.jpg"
              The_File_jpeg,      // file "The_File.jpeg"
              _Some____File_bla;  // file "_Some____File_bla.png"
       }
      Returns:
      the image filename derived from the enum item's name
    • getResourceFileNames

      static String[] getResourceFileNames(Class<? extends ImageResource> clazz)
      Returns the names of the actual files contained in the associated resource directory of the specified class, which must implement the ImageResource interface. This can be used to check if a given named resource has a matching file in a case-sensitive way.
      Parameters:
      clazz - the resource class
      Returns:
      an array of strings
    • getImageIcon

      Returns an icon for this ImageResource with the default icon size. All icons are cached, i.e., are only created once when first requested for a particular size.
      Returns:
      an ImagePlus instance containing the icon image
      See Also:
    • getImageIcon

      default ImagePlus getImageIcon(int iconSize)
      Returns an icon for this ImageResource with the specified size. All icons are cached, i.e., are only created once when first requested for a particular size.
      Parameters:
      iconSize -
      Returns:
      an ImagePlus instance containing the icon image
      See Also: