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
26package java.awt;
27
28import java.awt.geom.Rectangle2D;
29import java.awt.geom.AffineTransform;
30import java.awt.image.BufferedImage;
31import 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
51public 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
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}.
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}
152