< prev index next >

src/java.desktop/share/classes/java/awt/TexturePaint.java

Print this page




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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 
  26 package java.awt;
  27 
  28 import java.awt.geom.Rectangle2D;
  29 import java.awt.geom.AffineTransform;
  30 import java.awt.image.BufferedImage;
  31 import java.awt.image.ColorModel;
  32 
  33 /**
  34  * The <code>TexturePaint</code> class provides a way to fill a
  35  * {@link Shape} with a texture that is specified as
  36  * a {@link BufferedImage}. The size of the <code>BufferedImage</code>
  37  * object should be small because the <code>BufferedImage</code> data
  38  * is copied by the <code>TexturePaint</code> object.
  39  * At construction time, the texture is anchored to the upper
  40  * left corner of a {@link Rectangle2D} that is
  41  * specified in user space.  Texture is computed for
  42  * locations in the device space by conceptually replicating the
  43  * specified <code>Rectangle2D</code> infinitely in all directions
  44  * in user space and mapping the <code>BufferedImage</code> to each
  45  * replicated <code>Rectangle2D</code>.
  46  * @see Paint
  47  * @see Graphics2D#setPaint
  48  * @version 1.48, 06/05/07
  49  */
  50 
  51 public class TexturePaint implements Paint {
  52 
  53     BufferedImage bufImg;
  54     double tx;
  55     double ty;
  56     double sx;
  57     double sy;
  58 
  59     /**
  60      * Constructs a <code>TexturePaint</code> object.
  61      * @param txtr the <code>BufferedImage</code> object with the texture
  62      * used for painting
  63      * @param anchor the <code>Rectangle2D</code> in user space used to
  64      * anchor and replicate the texture
  65      */
  66     public TexturePaint(BufferedImage txtr,
  67                         Rectangle2D anchor) {
  68         this.bufImg = txtr;
  69         this.tx = anchor.getX();
  70         this.ty = anchor.getY();
  71         this.sx = anchor.getWidth() / bufImg.getWidth();
  72         this.sy = anchor.getHeight() / bufImg.getHeight();
  73     }
  74 
  75     /**
  76      * Returns the <code>BufferedImage</code> texture used to
  77      * fill the shapes.
  78      * @return a <code>BufferedImage</code>.
  79      */
  80     public BufferedImage getImage() {
  81         return bufImg;
  82     }
  83 
  84     /**
  85      * Returns a copy of the anchor rectangle which positions and
  86      * sizes the textured image.
  87      * @return the <code>Rectangle2D</code> used to anchor and
  88      * size this <code>TexturePaint</code>.
  89      */
  90     public Rectangle2D getAnchorRect() {
  91         return new Rectangle2D.Double(tx, ty,
  92                                       sx * bufImg.getWidth(),
  93                                       sy * bufImg.getHeight());
  94     }
  95 
  96     /**
  97      * Creates and returns a {@link PaintContext} used to
  98      * generate a tiled image pattern.
  99      * See the {@link Paint#createContext specification} of the
 100      * method in the {@link Paint} interface for information
 101      * on null parameter handling.
 102      *
 103      * @param cm the preferred {@link ColorModel} which represents the most convenient
 104      *           format for the caller to receive the pixel data, or {@code null}
 105      *           if there is no preference.
 106      * @param deviceBounds the device space bounding box
 107      *                     of the graphics primitive being rendered.
 108      * @param userBounds the user space bounding box


 122      * @see RenderingHints
 123      */
 124     public PaintContext createContext(ColorModel cm,
 125                                       Rectangle deviceBounds,
 126                                       Rectangle2D userBounds,
 127                                       AffineTransform xform,
 128                                       RenderingHints hints) {
 129         if (xform == null) {
 130             xform = new AffineTransform();
 131         } else {
 132             xform = (AffineTransform) xform.clone();
 133         }
 134         xform.translate(tx, ty);
 135         xform.scale(sx, sy);
 136 
 137         return TexturePaintContext.getContext(bufImg, xform, hints,
 138                                               deviceBounds);
 139     }
 140 
 141     /**
 142      * Returns the transparency mode for this <code>TexturePaint</code>.
 143      * @return the transparency mode for this <code>TexturePaint</code>
 144      * as an integer value.
 145      * @see Transparency
 146      */
 147     public int getTransparency() {
 148         return (bufImg.getColorModel()).getTransparency();
 149     }
 150 
 151 }


  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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 
  26 package java.awt;
  27 
  28 import java.awt.geom.Rectangle2D;
  29 import java.awt.geom.AffineTransform;
  30 import java.awt.image.BufferedImage;
  31 import java.awt.image.ColorModel;
  32 
  33 /**
  34  * The {@code TexturePaint} class provides a way to fill a
  35  * {@link Shape} with a texture that is specified as
  36  * a {@link BufferedImage}. The size of the {@code BufferedImage}
  37  * object should be small because the {@code BufferedImage} data
  38  * is copied by the {@code TexturePaint} object.
  39  * At construction time, the texture is anchored to the upper
  40  * left corner of a {@link Rectangle2D} that is
  41  * specified in user space.  Texture is computed for
  42  * locations in the device space by conceptually replicating the
  43  * specified {@code Rectangle2D} infinitely in all directions
  44  * in user space and mapping the {@code BufferedImage} to each
  45  * replicated {@code Rectangle2D}.
  46  * @see Paint
  47  * @see Graphics2D#setPaint
  48  * @version 1.48, 06/05/07
  49  */
  50 
  51 public class TexturePaint implements Paint {
  52 
  53     BufferedImage bufImg;
  54     double tx;
  55     double ty;
  56     double sx;
  57     double sy;
  58 
  59     /**
  60      * Constructs a {@code TexturePaint} object.
  61      * @param txtr the {@code BufferedImage} object with the texture
  62      * used for painting
  63      * @param anchor the {@code Rectangle2D} in user space used to
  64      * anchor and replicate the texture
  65      */
  66     public TexturePaint(BufferedImage txtr,
  67                         Rectangle2D anchor) {
  68         this.bufImg = txtr;
  69         this.tx = anchor.getX();
  70         this.ty = anchor.getY();
  71         this.sx = anchor.getWidth() / bufImg.getWidth();
  72         this.sy = anchor.getHeight() / bufImg.getHeight();
  73     }
  74 
  75     /**
  76      * Returns the {@code BufferedImage} texture used to
  77      * fill the shapes.
  78      * @return a {@code BufferedImage}.
  79      */
  80     public BufferedImage getImage() {
  81         return bufImg;
  82     }
  83 
  84     /**
  85      * Returns a copy of the anchor rectangle which positions and
  86      * sizes the textured image.
  87      * @return the {@code Rectangle2D} used to anchor and
  88      * size this {@code TexturePaint}.
  89      */
  90     public Rectangle2D getAnchorRect() {
  91         return new Rectangle2D.Double(tx, ty,
  92                                       sx * bufImg.getWidth(),
  93                                       sy * bufImg.getHeight());
  94     }
  95 
  96     /**
  97      * Creates and returns a {@link PaintContext} used to
  98      * generate a tiled image pattern.
  99      * See the {@link Paint#createContext specification} of the
 100      * method in the {@link Paint} interface for information
 101      * on null parameter handling.
 102      *
 103      * @param cm the preferred {@link ColorModel} which represents the most convenient
 104      *           format for the caller to receive the pixel data, or {@code null}
 105      *           if there is no preference.
 106      * @param deviceBounds the device space bounding box
 107      *                     of the graphics primitive being rendered.
 108      * @param userBounds the user space bounding box


 122      * @see RenderingHints
 123      */
 124     public PaintContext createContext(ColorModel cm,
 125                                       Rectangle deviceBounds,
 126                                       Rectangle2D userBounds,
 127                                       AffineTransform xform,
 128                                       RenderingHints hints) {
 129         if (xform == null) {
 130             xform = new AffineTransform();
 131         } else {
 132             xform = (AffineTransform) xform.clone();
 133         }
 134         xform.translate(tx, ty);
 135         xform.scale(sx, sy);
 136 
 137         return TexturePaintContext.getContext(bufImg, xform, hints,
 138                                               deviceBounds);
 139     }
 140 
 141     /**
 142      * Returns the transparency mode for this {@code TexturePaint}.
 143      * @return the transparency mode for this {@code TexturePaint}
 144      * as an integer value.
 145      * @see Transparency
 146      */
 147     public int getTransparency() {
 148         return (bufImg.getColorModel()).getTransparency();
 149     }
 150 
 151 }
< prev index next >