1 /*
   2  * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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
 109      *                   of the graphics primitive being rendered.
 110      * @param xform the {@link AffineTransform} from user
 111      *              space into device space.
 112      * @param hints the set of hints that the context object can use to
 113      *              choose between rendering alternatives.
 114      * @return the {@code PaintContext} for
 115      *         generating color patterns.
 116      * @see Paint
 117      * @see PaintContext
 118      * @see ColorModel
 119      * @see Rectangle
 120      * @see Rectangle2D
 121      * @see AffineTransform
 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 }
--- EOF ---