Class NamedIcon

java.lang.Object
javax.swing.ImageIcon
jmri.jmrit.catalog.NamedIcon
All Implemented Interfaces:
Serializable, Accessible, Icon

public class NamedIcon extends ImageIcon
Extend an ImageIcon to remember the name from which it was created and provide rotation and scaling services.

We store both a "URL" for finding the file this was made from (so we can load this later), plus a shorter (localized) "name" for display in GUI.

These can be persisted by storing their name and rotation.

See Also:
  • Field Details

  • Constructor Details

    • NamedIcon

      public NamedIcon(NamedIcon pOld)
      Create a NamedIcon that is a complete copy of an existing NamedIcon
      Parameters:
      pOld - Object to copy i.e. copy of the original icon, but NOT a complete copy of pOld (no transformations done)
    • NamedIcon

      public NamedIcon(NamedIcon pOld, Component comp)
      Create a NamedIcon that is really a complete copy of an existing NamedIcon
      Parameters:
      pOld - Object to copy
      comp - the container the new icon is embedded in
    • NamedIcon

      public NamedIcon(String pUrl, String pName)
      Create a named icon that includes an image to be loaded from a URL.

      The default access form is "file:", so a bare pathname to an icon file will also work for the URL argument.

      Parameters:
      pUrl - URL of image file to load
      pName - Human-readable name for the icon
    • NamedIcon

      public NamedIcon(String pUrl, String pName, jmri.jmrit.catalog.NamedIcon.GIFMetadataImages pGifState)
      Create a named icon that includes an image to be loaded from a URL.

      The default access form is "file:", so a bare pathname to an icon file will also work for the URL argument.

      Parameters:
      pUrl - URL of image file to load
      pName - Human-readable name for the icon
      pGifState - Breakdown of GIF Image metadata and frames
    • NamedIcon

      public NamedIcon(URL pUrl, String pName)
      Create a named icon that includes an image to be loaded from a URL.
      Parameters:
      pUrl - String-form URL of image file to load
      pName - Human-readable name for the icon
    • NamedIcon

      public NamedIcon(Image im)
      Create a named icon from an Image. N.B. NamedIcon's create using this constructor can NOT be animated GIFs
      Parameters:
      im - Image to use
  • Method Details

    • getIconByName

      public static NamedIcon getIconByName(String path)
      Find the NamedIcon corresponding to a file path. Understands the standard portable filename prefixes.
      Parameters:
      path - The path to the file, either absolute or portable
      Returns:
      the desired icon with this same name as its path
    • getName

      Return the human-readable name of this icon.
      Returns:
      the name or null if not set
    • setName

      public void setName(@CheckForNull String name)
      Set the human-readable name for this icon.
      Parameters:
      name - the new name, can be null
    • getURL

      Get the URL of this icon.
      Returns:
      the path to this icon in JMRI portable format or null if not set
    • setURL

      public void setURL(@CheckForNull String url)
      Set URL of original icon image. Setting this after initial construction does not change the icon.
      Parameters:
      url - the URL associated with this icon
    • getRotation

      public int getRotation()
      Get the number of 90-degree rotations needed to properly display this icon.
      Returns:
      0 (no rotation), 1 (rotated 90 degrees), 2 (180 degrees), or 3 (270 degrees)
    • setRotation

      public void setRotation(int pRotation, Component comp)
      Set the number of 90-degree rotations needed to properly display this icon.
      Parameters:
      pRotation - 0 (no rotation), 1 (rotated 90 degrees), 2 (180 degrees), or 3 (270 degrees)
      comp - the component containing this icon
    • createRotatedImage

      public Image createRotatedImage(Image pImage, Component pComponent, int pRotation)
      The following was based on a text-rotating applet from David Risner, available at http://www.risner.org/java/rotate_text.html Page unavailable as at April 2019
      Parameters:
      pImage - Image to transform
      pComponent - Component containing the image, needed to obtain a MediaTracker to process the image consistently with display
      pRotation - 0-3 number of 90-degree rotations needed
      Returns:
      new Image object containing the rotated input image
    • getDegrees

      public int getDegrees()
    • getScale

      public double getScale()
    • setLoad

      public void setLoad(int d, double s, Component comp)
    • transformImage

      public void transformImage(int w, int h, AffineTransform t, Component comp)
    • scale

      public void scale(double scale, Component comp)
      Scale as a percentage.
      Parameters:
      scale - the scale to set the image
      comp - the containing component
    • rotate

      public void rotate(int degree, Component comp)
      Rotate from anchor point (upper left corner) and shift into place.
      Parameters:
      degree - the distance to rotate
      comp - containing component
    • reduceTo

      public double reduceTo(int width, int height, double limit)
      Reduce this image size to within the given dimensions, with a limit on the reduction in size.
      Parameters:
      width - new width
      height - new height
      limit - limit on the reduction in size
      Returns:
      the scale by which this image was resized
    • flip

      public void flip(int flip, Component comp)