Class NamedIcon

  • All Implemented Interfaces:
    java.io.Serializable, javax.accessibility.Accessible, javax.swing.Icon

    public class NamedIcon
    extends javax.swing.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:
    PositionableLabelXml, Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class javax.swing.ImageIcon

        javax.swing.ImageIcon.AccessibleImageIcon
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int HORIZONTALFLIP  
      (package private) int mRotation
      Valid values are 0 - no rotation 1 - 90 degrees counter-clockwise 2 - 180 degrees counter-clockwise 3 - 270 degrees counter-clockwise
      static int NOFLIP  
      static int VERTICALFLIP  
      • Fields inherited from class javax.swing.ImageIcon

        component, tracker
    • Constructor Summary

      Constructors 
      Constructor Description
      NamedIcon​(java.awt.Image im)
      Create a named icon from an Image.
      NamedIcon​(java.lang.String pUrl, java.lang.String pName)
      Create a named icon that includes an image to be loaded from a URL.
      NamedIcon​(java.lang.String pUrl, java.lang.String pName, jmri.jmrit.catalog.NamedIcon.GIFMetadataImages pGifState)
      Create a named icon that includes an image to be loaded from a URL.
      NamedIcon​(java.net.URL pUrl, java.lang.String pName)
      Create a named icon that includes an image to be loaded from a URL.
      NamedIcon​(NamedIcon pOld)
      Create a NamedIcon that is a complete copy of an existing NamedIcon
      NamedIcon​(NamedIcon pOld, java.awt.Component comp)
      Create a NamedIcon that is really a complete copy of an existing NamedIcon
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.awt.Image createRotatedImage​(java.awt.Image pImage, java.awt.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
      void flip​(int flip, java.awt.Component comp)  
      int getDegrees()  
      static NamedIcon getIconByName​(java.lang.String path)
      Find the NamedIcon corresponding to a file path.
      java.lang.String getName()
      Return the human-readable name of this icon.
      int getRotation()
      Get the number of 90-degree rotations needed to properly display this icon.
      double getScale()  
      java.lang.String getURL()
      Get the URL of this icon.
      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.
      void rotate​(int degree, java.awt.Component comp)
      Rotate from anchor point (upper left corner) and shift into place.
      void scale​(double scale, java.awt.Component comp)
      Scale as a percentage.
      void setLoad​(int d, double s, java.awt.Component comp)  
      void setName​(java.lang.String name)
      Set the human-readable name for this icon.
      void setRotation​(int pRotation, java.awt.Component comp)
      Set the number of 90-degree rotations needed to properly display this icon.
      void setURL​(java.lang.String url)
      Set URL of original icon image.
      void transformImage​(int w, int h, java.awt.geom.AffineTransform t, java.awt.Component comp)  
      • Methods inherited from class javax.swing.ImageIcon

        getAccessibleContext, getDescription, getIconHeight, getIconWidth, getImage, getImageLoadStatus, getImageObserver, loadImage, paintIcon, setDescription, setImage, setImageObserver, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • 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,
                         java.awt.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​(java.lang.String pUrl,
                         java.lang.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​(java.lang.String pUrl,
                         java.lang.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​(java.net.URL pUrl,
                         java.lang.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​(java.awt.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 Detail

      • getIconByName

        public static NamedIcon getIconByName​(java.lang.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

        @CheckForNull
        public java.lang.String getName()
        Return the human-readable name of this icon.
        Returns:
        the name or null if not set
      • setName

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

        @CheckForNull
        public java.lang.String 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
                           java.lang.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,
                                java.awt.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 java.awt.Image createRotatedImage​(java.awt.Image pImage,
                                                 java.awt.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
      • setLoad

        public void setLoad​(int d,
                            double s,
                            java.awt.Component comp)
      • transformImage

        public void transformImage​(int w,
                                   int h,
                                   java.awt.geom.AffineTransform t,
                                   java.awt.Component comp)
      • scale

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

        public void rotate​(int degree,
                           java.awt.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,
                         java.awt.Component comp)