com.alee.utils.ninepatch
Class NinePatchIcon

java.lang.Object
  extended by com.alee.utils.ninepatch.NinePatchIcon
All Implemented Interfaces:
javax.swing.Icon

@XStreamConverter(value=NinePatchIconConverter.class)
public class NinePatchIcon
extends java.lang.Object
implements javax.swing.Icon

This class allows you to create and use nine-patch icons within Swing applications.

Basically it parses nine-patch image data (patches at the side of .9.png image) into understandable values and uses them to stretch the image properly when it is painted anywhere.

Author:
Mikle Garin
See Also:
NinePatchIconPainter, NinePatchStatePainter

Field Summary
protected  java.lang.Integer cachedHeight0
          Cached fixed areas height of the nine-patch image with additional 1px for each stretchable area.
protected  java.lang.Integer cachedHeight1
          Cached fixed areas height of the nine-patch image.
protected  java.lang.Integer cachedWidth0
          Cached fixed areas width of the nine-patch image with additional 1px for each stretchable area.
protected  java.lang.Integer cachedWidth1
          Cached fixed areas width of the nine-patch image.
protected  java.awt.Component component
          Component onto which this nine-patch icon will be stretched.
protected  java.util.List<NinePatchInterval> horizontalStretch
          Horizontal stretch intervals taken from image patches (top image patches).
protected  java.awt.Insets margin
          Content margin taken from image patches (right and bottom patches).
protected  java.awt.image.BufferedImage rawImage
          Raw image without patches.
protected  java.util.List<NinePatchInterval> verticalStretch
          Vertical stretch intervals taken from image patches (left image patches).
 
Constructor Summary
  NinePatchIcon(java.awt.image.BufferedImage bufferedImage)
          Constructs new NinePatchIcon using the specified nine-patch image.
  NinePatchIcon(java.awt.image.BufferedImage bufferedImage, java.awt.Component component)
          Constructs new NinePatchIcon using the specified nine-patch image.
protected NinePatchIcon(java.awt.image.BufferedImage bufferedImage, java.awt.Component component, boolean parsePatches)
          Constructs new NinePatchIcon using the specified nine-patch image.
  NinePatchIcon(java.awt.Image image)
          Constructs new NinePatchIcon using the specified nine-patch image.
  NinePatchIcon(java.awt.Image image, java.awt.Component component)
          Constructs new NinePatchIcon using the specified nine-patch image.
  NinePatchIcon(javax.swing.ImageIcon imageIcon)
          Constructs new NinePatchIcon using the specified nine-patch image.
  NinePatchIcon(javax.swing.ImageIcon imageIcon, java.awt.Component component)
          Constructs new NinePatchIcon using the specified nine-patch image.
  NinePatchIcon(java.lang.String iconSrc)
          Constructs new NinePatchIcon using the nine-patch image from the specified path.
  NinePatchIcon(java.lang.String iconSrc, java.awt.Component component)
          Constructs new NinePatchIcon using the nine-patch image from the specified path.
  NinePatchIcon(java.net.URL url)
          Constructs new NinePatchIcon using the nine-patch image from the specified URL.
  NinePatchIcon(java.net.URL url, java.awt.Component component)
          Constructs new NinePatchIcon using the nine-patch image from the specified URL.
 
Method Summary
 void addHorizontalStretch(int start, int end, boolean pixel)
          Adds horizontal stretch interval.
 void addHorizontalStretch(NinePatchInterval interval)
          Adds horizontal stretch interval.
 void addVerticalStretch(int start, int end, boolean pixel)
          Adds vertical stretch interval.
 void addVerticalStretch(NinePatchInterval interval)
          Adds vertical stretch interval.
protected  int calculateFixedPixelsHeight(boolean addUnfixedSpaces)
          Returns fixed minimum height for this icon.
protected  int calculateFixedPixelsWidth(boolean addUnfixedSpaces)
          Returns fixed minimum width for this icon.
static NinePatchIcon create(java.awt.image.BufferedImage rawImage)
          Returns newly created NinePatchIcon with empty patches.
 java.awt.Component getComponent()
          Returns component atop of which icon will be stretched.
 int getFixedPixelsHeight(boolean addUnfixedSpaces)
          Returns cached fixed minimum height for this icon.
 int getFixedPixelsWidth(boolean addUnfixedSpaces)
          Returns cached fixed minimum width for this icon.
 java.util.List<NinePatchInterval> getHorizontalStretch()
          Returns list of horizontal stretch intervals taken from image patches.
 int getIconHeight()
          
 int getIconWidth()
          
 java.awt.Insets getMargin()
          Returns margin taken from image content patches.
 java.awt.Dimension getPreferredSize()
          Returns preferred icon size.
 java.awt.image.BufferedImage getRawImage()
          Returns raw image without patches.
 java.awt.Dimension getRealImageSize()
          Returns raw image size.
 java.awt.Insets getStretchMargin()
          Returns margin taken from image stretch patches.
 java.util.List<NinePatchInterval> getVerticalStretch()
          Returns list of vertical stretch intervals taken from image patches.
 void paintIcon(java.awt.Component c, java.awt.Graphics g)
          Paints icon for the specified component.
 void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)
          Paints icon for the specified component at the specified location.
 void paintIcon(java.awt.Graphics2D g2d, int x, int y, int width, int height)
          Paints icon at the specified bounds.
 void paintIcon(java.awt.Graphics2D g2d, java.awt.Rectangle bounds)
          Paints icon at the specified bounds.
 void setComponent(java.awt.Component component)
          Sets component atop of which icon will be stretched.
 void setHorizontalStretch(java.util.List<NinePatchInterval> horizontalStretch)
          Sets list of horizontal stretch intervals.
 void setMargin(java.awt.Insets margin)
          Sets content margin.
 void setMargin(int spacing)
          Sets content margin.
 void setMargin(int top, int left, int bottom, int right)
          Sets content margin.
 void setVerticalStretch(java.util.List<NinePatchInterval> verticalStretch)
          Sets list of vertical stretch intervals.
protected  void updateCachedHeightData()
          Clears fixed pixels height caches.
protected  void updateCachedWidthData()
          Clears fixed pixels width caches.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

component

protected java.awt.Component component
Component onto which this nine-patch icon will be stretched.


rawImage

protected java.awt.image.BufferedImage rawImage
Raw image without patches.


horizontalStretch

protected java.util.List<NinePatchInterval> horizontalStretch
Horizontal stretch intervals taken from image patches (top image patches). Note that pixel parts are also included here but marked with "pixel=true" boolean value.


verticalStretch

protected java.util.List<NinePatchInterval> verticalStretch
Vertical stretch intervals taken from image patches (left image patches). Note that pixel parts are also included here but marked with "pixel=true" boolean value.


margin

protected java.awt.Insets margin
Content margin taken from image patches (right and bottom patches). This margin is generally valuable for components which uses this icon as a background to set their style margins properly.


cachedWidth0

protected java.lang.Integer cachedWidth0
Cached fixed areas width of the nine-patch image with additional 1px for each stretchable area.


cachedWidth1

protected java.lang.Integer cachedWidth1
Cached fixed areas width of the nine-patch image.


cachedHeight0

protected java.lang.Integer cachedHeight0
Cached fixed areas height of the nine-patch image with additional 1px for each stretchable area.


cachedHeight1

protected java.lang.Integer cachedHeight1
Cached fixed areas height of the nine-patch image.

Constructor Detail

NinePatchIcon

public NinePatchIcon(java.net.URL url)
Constructs new NinePatchIcon using the nine-patch image from the specified URL.

Parameters:
url - nine-patch image URL

NinePatchIcon

public NinePatchIcon(java.net.URL url,
                     java.awt.Component component)
Constructs new NinePatchIcon using the nine-patch image from the specified URL.

Parameters:
url - nine-patch image URL
component - component atop of which icon will be stretched

NinePatchIcon

public NinePatchIcon(java.lang.String iconSrc)
Constructs new NinePatchIcon using the nine-patch image from the specified path.

Parameters:
iconSrc - nine-patch image path

NinePatchIcon

public NinePatchIcon(java.lang.String iconSrc,
                     java.awt.Component component)
Constructs new NinePatchIcon using the nine-patch image from the specified path.

Parameters:
iconSrc - nine-patch image path
component - component atop of which icon will be stretched

NinePatchIcon

public NinePatchIcon(javax.swing.ImageIcon imageIcon)
Constructs new NinePatchIcon using the specified nine-patch image.

Parameters:
imageIcon - nine-patch image

NinePatchIcon

public NinePatchIcon(javax.swing.ImageIcon imageIcon,
                     java.awt.Component component)
Constructs new NinePatchIcon using the specified nine-patch image.

Parameters:
imageIcon - nine-patch image
component - component atop of which icon will be stretched

NinePatchIcon

public NinePatchIcon(java.awt.Image image)
Constructs new NinePatchIcon using the specified nine-patch image.

Parameters:
image - nine-patch image

NinePatchIcon

public NinePatchIcon(java.awt.Image image,
                     java.awt.Component component)
Constructs new NinePatchIcon using the specified nine-patch image.

Parameters:
image - nine-patch image
component - component atop of which icon will be stretched

NinePatchIcon

public NinePatchIcon(java.awt.image.BufferedImage bufferedImage)
Constructs new NinePatchIcon using the specified nine-patch image.

Parameters:
bufferedImage - nine-patch image

NinePatchIcon

public NinePatchIcon(java.awt.image.BufferedImage bufferedImage,
                     java.awt.Component component)
Constructs new NinePatchIcon using the specified nine-patch image.

Parameters:
bufferedImage - nine-patch image
component - component atop of which icon will be stretched

NinePatchIcon

protected NinePatchIcon(java.awt.image.BufferedImage bufferedImage,
                        java.awt.Component component,
                        boolean parsePatches)
Constructs new NinePatchIcon using the specified nine-patch image.

Parameters:
bufferedImage - nine-patch image
component - component atop of which icon will be stretched
parsePatches - whether should parse image patches or not
Method Detail

create

public static NinePatchIcon create(java.awt.image.BufferedImage rawImage)
Returns newly created NinePatchIcon with empty patches.

Parameters:
rawImage - raw image without patches
Returns:
newly created NinePatchIcon with empty patches

getRawImage

public java.awt.image.BufferedImage getRawImage()
Returns raw image without patches.

Returns:
raw image without patches

getComponent

public java.awt.Component getComponent()
Returns component atop of which icon will be stretched.

Returns:
component atop of which icon will be stretched

setComponent

public void setComponent(java.awt.Component component)
Sets component atop of which icon will be stretched.

Parameters:
component - component atop of which icon will be stretched

getHorizontalStretch

public java.util.List<NinePatchInterval> getHorizontalStretch()
Returns list of horizontal stretch intervals taken from image patches.

Returns:
list of horizontal stretch intervals taken from image patches

setHorizontalStretch

public void setHorizontalStretch(java.util.List<NinePatchInterval> horizontalStretch)
Sets list of horizontal stretch intervals.

Parameters:
horizontalStretch - list of horizontal stretch intervals

addHorizontalStretch

public void addHorizontalStretch(NinePatchInterval interval)
Adds horizontal stretch interval.

Parameters:
interval - horizontal stretch interval to add

addHorizontalStretch

public void addHorizontalStretch(int start,
                                 int end,
                                 boolean pixel)
Adds horizontal stretch interval.

Parameters:
start - interval start
end - interval end
pixel - whether fixed interval or not

getVerticalStretch

public java.util.List<NinePatchInterval> getVerticalStretch()
Returns list of vertical stretch intervals taken from image patches.

Returns:
list of vertical stretch intervals taken from image patches

setVerticalStretch

public void setVerticalStretch(java.util.List<NinePatchInterval> verticalStretch)
Sets list of vertical stretch intervals.

Parameters:
verticalStretch - list of vertical stretch intervals

addVerticalStretch

public void addVerticalStretch(NinePatchInterval interval)
Adds vertical stretch interval.

Parameters:
interval - vertical stretch interval to add

addVerticalStretch

public void addVerticalStretch(int start,
                               int end,
                               boolean pixel)
Adds vertical stretch interval.

Parameters:
start - interval start
end - interval end
pixel - whether fixed interval or not

getMargin

public java.awt.Insets getMargin()
Returns margin taken from image content patches.

Returns:
margin taken from image content patches

getStretchMargin

public java.awt.Insets getStretchMargin()
Returns margin taken from image stretch patches.

Returns:
margin taken from image stretch patches

setMargin

public void setMargin(java.awt.Insets margin)
Sets content margin.

Parameters:
margin - content margin

setMargin

public void setMargin(int top,
                      int left,
                      int bottom,
                      int right)
Sets content margin.

Parameters:
top - top margin
left - left margin
bottom - bottom margin
right - right margin

setMargin

public void setMargin(int spacing)
Sets content margin.

Parameters:
spacing - sides margin

paintIcon

public void paintIcon(java.awt.Component c,
                      java.awt.Graphics g)
Paints icon for the specified component.

Parameters:
c - component to process
g - graphics context

paintIcon

public void paintIcon(java.awt.Component c,
                      java.awt.Graphics g,
                      int x,
                      int y)
Paints icon for the specified component at the specified location.

Specified by:
paintIcon in interface javax.swing.Icon
Parameters:
c - component to process
g - graphics context
x - location X coordinate
y - location Y coordinate

paintIcon

public void paintIcon(java.awt.Graphics2D g2d,
                      java.awt.Rectangle bounds)
Paints icon at the specified bounds.

Parameters:
g2d - graphics context
bounds - icon bounds

paintIcon

public void paintIcon(java.awt.Graphics2D g2d,
                      int x,
                      int y,
                      int width,
                      int height)
Paints icon at the specified bounds.

Parameters:
g2d - graphics context
x - location X coordinate
y - location Y coordinate
width - icon width
height - icon height

getFixedPixelsWidth

public int getFixedPixelsWidth(boolean addUnfixedSpaces)
Returns cached fixed minimum width for this icon.

Parameters:
addUnfixedSpaces - whether to add 1px for each stretchable area or not
Returns:
cached fixed minimum width for this icon

calculateFixedPixelsWidth

protected int calculateFixedPixelsWidth(boolean addUnfixedSpaces)
Returns fixed minimum width for this icon.

Parameters:
addUnfixedSpaces - whether to add 1px for each stretchable area or not
Returns:
fixed minimum width for this icon

updateCachedWidthData

protected void updateCachedWidthData()
Clears fixed pixels width caches.


getFixedPixelsHeight

public int getFixedPixelsHeight(boolean addUnfixedSpaces)
Returns cached fixed minimum height for this icon.

Parameters:
addUnfixedSpaces - whether to add 1px for each stretchable area or not
Returns:
cached fixed minimum height for this icon

calculateFixedPixelsHeight

protected int calculateFixedPixelsHeight(boolean addUnfixedSpaces)
Returns fixed minimum height for this icon.

Parameters:
addUnfixedSpaces - swhether to add 1px for each stretchable area or not
Returns:
fixed minimum height for this icon

updateCachedHeightData

protected void updateCachedHeightData()
Clears fixed pixels height caches.


getIconWidth

public int getIconWidth()

Specified by:
getIconWidth in interface javax.swing.Icon

getIconHeight

public int getIconHeight()

Specified by:
getIconHeight in interface javax.swing.Icon

getPreferredSize

public java.awt.Dimension getPreferredSize()
Returns preferred icon size.

Returns:
preferred icon size

getRealImageSize

public java.awt.Dimension getRealImageSize()
Returns raw image size.

Returns:
raw image size