< prev index next >

src/java.desktop/share/classes/javax/swing/plaf/nimbus/ImageScalingHelper.java

Print this page




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.plaf.nimbus;
  26 
  27 import java.awt.Graphics;
  28 import java.awt.Image;
  29 import java.awt.Insets;
  30 
  31 /**
  32  * ImageScalingHelper
  33  *
  34  * @author Created by Jasper Potts (Aug 8, 2007)
  35  */
  36 class ImageScalingHelper {
  37 
  38     /** Enumeration for the types of painting this class can handle. */
  39     enum PaintType {
  40         /**
  41          * Painting type indicating the image should be centered in the space provided.  When used the <code>mask</code>
  42          * is ignored.
  43          */
  44         CENTER,
  45 
  46         /**
  47          * Painting type indicating the image should be tiled across the specified width and height.  When used the
  48          * <code>mask</code> is ignored.
  49          */
  50         TILE,
  51 
  52         /**
  53          * Painting type indicating the image should be split into nine regions with the top, left, bottom and right
  54          * areas stretched.
  55          */
  56         PAINT9_STRETCH,
  57 
  58         /**
  59          * Painting type indicating the image should be split into nine regions with the top, left, bottom and right
  60          * areas tiled.
  61          */
  62         PAINT9_TILE
  63     }
  64 
  65     ;
  66 
  67     private static final Insets EMPTY_INSETS = new Insets(0, 0, 0, 0);
  68 
  69     static final int PAINT_TOP_LEFT = 1;
  70     static final int PAINT_TOP = 2;
  71     static final int PAINT_TOP_RIGHT = 4;
  72     static final int PAINT_LEFT = 8;
  73     static final int PAINT_CENTER = 16;
  74     static final int PAINT_RIGHT = 32;
  75     static final int PAINT_BOTTOM_RIGHT = 64;
  76     static final int PAINT_BOTTOM = 128;
  77     static final int PAINT_BOTTOM_LEFT = 256;
  78     /**
  79      * Specifies that all regions should be painted.  If this is set any other regions specified will not be painted.
  80      * For example PAINT_ALL | PAINT_CENTER will paint all but the center.
  81      */
  82     static final int PAINT_ALL = 512;
  83 
  84     /**
  85      * Paints using the algorightm specified by <code>paintType</code>.
  86      *
  87      * @param g         Graphics to render to
  88      * @param x         X-coordinate
  89      * @param y         Y-coordinate
  90      * @param w         Width to render to
  91      * @param h         Height to render to
  92      * @param image     Image to render from, if <code>null</code> this method will do nothing
  93      * @param sInsets   Insets specifying the portion of the image that will be stretched or tiled, if <code>null</code>
  94      *                  empty <code>Insets</code> will be used.
  95      * @param dInsets   Destination insets specifying the portion of the image will be stretched or tiled, if
  96      *                  <code>null</code> empty <code>Insets</code> will be used.
  97      * @param paintType Specifies what type of algorithm to use in painting
  98      * @param mask      Specifies portion of image to render, if <code>PAINT_ALL</code> is specified, any other regions
  99      *                  specified will not be painted, for example PAINT_ALL | PAINT_CENTER paints everything but the
 100      *                  center.
 101      */
 102     public static void paint(Graphics g, int x, int y, int w, int h,
 103                       Image image, Insets sInsets,
 104                       Insets dInsets, PaintType paintType, int mask) {
 105         if (image == null || image.getWidth(null) <= 0 || image.getHeight(null) <= 0) {
 106             return;
 107         }
 108         if (sInsets == null) {
 109             sInsets = EMPTY_INSETS;
 110         }
 111         if (dInsets == null) {
 112             dInsets = EMPTY_INSETS;
 113         }
 114         int iw = image.getWidth(null);
 115         int ih = image.getHeight(null);
 116 
 117         if (paintType == PaintType.CENTER) {
 118             // Center the image




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.plaf.nimbus;
  26 
  27 import java.awt.Graphics;
  28 import java.awt.Image;
  29 import java.awt.Insets;
  30 
  31 /**
  32  * ImageScalingHelper
  33  *
  34  * @author Created by Jasper Potts (Aug 8, 2007)
  35  */
  36 class ImageScalingHelper {
  37 
  38     /** Enumeration for the types of painting this class can handle. */
  39     enum PaintType {
  40         /**
  41          * Painting type indicating the image should be centered in the space provided.  When used the {@code mask}
  42          * is ignored.
  43          */
  44         CENTER,
  45 
  46         /**
  47          * Painting type indicating the image should be tiled across the specified width and height.  When used the
  48          * {@code mask} is ignored.
  49          */
  50         TILE,
  51 
  52         /**
  53          * Painting type indicating the image should be split into nine regions with the top, left, bottom and right
  54          * areas stretched.
  55          */
  56         PAINT9_STRETCH,
  57 
  58         /**
  59          * Painting type indicating the image should be split into nine regions with the top, left, bottom and right
  60          * areas tiled.
  61          */
  62         PAINT9_TILE
  63     }
  64 
  65     ;
  66 
  67     private static final Insets EMPTY_INSETS = new Insets(0, 0, 0, 0);
  68 
  69     static final int PAINT_TOP_LEFT = 1;
  70     static final int PAINT_TOP = 2;
  71     static final int PAINT_TOP_RIGHT = 4;
  72     static final int PAINT_LEFT = 8;
  73     static final int PAINT_CENTER = 16;
  74     static final int PAINT_RIGHT = 32;
  75     static final int PAINT_BOTTOM_RIGHT = 64;
  76     static final int PAINT_BOTTOM = 128;
  77     static final int PAINT_BOTTOM_LEFT = 256;
  78     /**
  79      * Specifies that all regions should be painted.  If this is set any other regions specified will not be painted.
  80      * For example PAINT_ALL | PAINT_CENTER will paint all but the center.
  81      */
  82     static final int PAINT_ALL = 512;
  83 
  84     /**
  85      * Paints using the algorightm specified by {@code paintType}.
  86      *
  87      * @param g         Graphics to render to
  88      * @param x         X-coordinate
  89      * @param y         Y-coordinate
  90      * @param w         Width to render to
  91      * @param h         Height to render to
  92      * @param image     Image to render from, if {@code null} this method will do nothing
  93      * @param sInsets   Insets specifying the portion of the image that will be stretched or tiled, if {@code null}
  94      *                  empty {@code Insets} will be used.
  95      * @param dInsets   Destination insets specifying the portion of the image will be stretched or tiled, if
  96      *                  {@code null} empty {@code Insets} will be used.
  97      * @param paintType Specifies what type of algorithm to use in painting
  98      * @param mask      Specifies portion of image to render, if {@code PAINT_ALL} is specified, any other regions
  99      *                  specified will not be painted, for example PAINT_ALL | PAINT_CENTER paints everything but the
 100      *                  center.
 101      */
 102     public static void paint(Graphics g, int x, int y, int w, int h,
 103                       Image image, Insets sInsets,
 104                       Insets dInsets, PaintType paintType, int mask) {
 105         if (image == null || image.getWidth(null) <= 0 || image.getHeight(null) <= 0) {
 106             return;
 107         }
 108         if (sInsets == null) {
 109             sInsets = EMPTY_INSETS;
 110         }
 111         if (dInsets == null) {
 112             dInsets = EMPTY_INSETS;
 113         }
 114         int iw = image.getWidth(null);
 115         int ih = image.getHeight(null);
 116 
 117         if (paintType == PaintType.CENTER) {
 118             // Center the image


< prev index next >