KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > TexturePaint


1 /*
2  * @(#)TexturePaint.java 1.39 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 import java.awt.geom.Rectangle2D JavaDoc;
11 import java.awt.geom.AffineTransform JavaDoc;
12 import java.awt.image.AffineTransformOp JavaDoc;
13 import java.awt.image.BufferedImage JavaDoc;
14 import java.awt.image.ColorModel JavaDoc;
15
16 /**
17  * The <code>TexturePaint</code> class provides a way to fill a
18  * {@link Shape} with a texture that is specified as
19  * a {@link BufferedImage}. The size of the <code>BufferedImage</code>
20  * object should be small because the <code>BufferedImage</code> data
21  * is copied by the <code>TexturePaint</code> object.
22  * At construction time, the texture is anchored to the upper
23  * left corner of a {@link Rectangle2D} that is
24  * specified in user space. Texture is computed for
25  * locations in the device space by conceptually replicating the
26  * specified <code>Rectangle2D</code> infinitely in all directions
27  * in user space and mapping the <code>BufferedImage</code> to each
28  * replicated <code>Rectangle2D</code>.
29  * @see Paint
30  * @see Graphics2D#setPaint
31  * @version 1.39, 12/19/03
32  */

33
34 public class TexturePaint implements Paint JavaDoc {
35
36     BufferedImage JavaDoc bufImg;
37     double tx;
38     double ty;
39     double sx;
40     double sy;
41
42     /**
43      * Constructs a <code>TexturePaint</code> object.
44      * @param txtr the <code>BufferedImage</code> object with the texture
45      * used for painting
46      * @param anchor the <code>Rectangle2D</code> in user space used to
47      * anchor and replicate the texture
48      */

49     public TexturePaint(BufferedImage JavaDoc txtr,
50             Rectangle2D JavaDoc anchor) {
51         this.bufImg = txtr;
52     this.tx = anchor.getX();
53     this.ty = anchor.getY();
54     this.sx = anchor.getWidth() / bufImg.getWidth();
55     this.sy = anchor.getHeight() / bufImg.getHeight();
56     }
57
58     /**
59      * Returns the <code>BufferedImage</code> texture used to
60      * fill the shapes.
61      * @return a <code>BufferedImage</code>.
62      */

63     public BufferedImage JavaDoc getImage() {
64     return bufImg;
65     }
66
67     /**
68      * Returns a copy of the anchor rectangle which positions and
69      * sizes the textured image.
70      * @return the <code>Rectangle2D</code> used to anchor and
71      * size this <code>TexturePaint</code>.
72      */

73     public Rectangle2D JavaDoc getAnchorRect() {
74     return new Rectangle2D.Double JavaDoc(tx, ty,
75                       sx * bufImg.getWidth(),
76                       sy * bufImg.getHeight());
77     }
78
79     /**
80      * Creates and returns a context used to generate the color pattern.
81      * @param cm the {@link ColorModel} that receives the
82      * <code>Paint</code> data. This is used only as a hint.
83      * @param deviceBounds the device space bounding box of the graphics
84      * primitive being rendered
85      * @param userBounds the user space bounding box of the graphics
86      * primitive being rendered
87      * @param xform the {@link AffineTransform} from user space
88      * into device space
89      * @param hints a {@link RenderingHints} object that can be used to
90      * specify how the pattern is ultimately rendered
91      * @return the {@link PaintContext} used for generating color
92      * patterns.
93      * @see PaintContext
94      */

95     public PaintContext JavaDoc createContext(ColorModel JavaDoc cm,
96                       Rectangle JavaDoc deviceBounds,
97                       Rectangle2D JavaDoc userBounds,
98                       AffineTransform JavaDoc xform,
99                                       RenderingHints JavaDoc hints) {
100     if (xform == null) {
101         xform = new AffineTransform JavaDoc();
102     } else {
103         xform = (AffineTransform JavaDoc) xform.clone();
104     }
105     xform.translate(tx, ty);
106     xform.scale(sx, sy);
107
108     return TexturePaintContext.getContext(bufImg, xform, hints,
109                           deviceBounds);
110     }
111
112     /**
113      * Returns the transparency mode for this <code>TexturePaint</code>.
114      * @return the transparency mode for this <code>TexturePaint</code>
115      * as an integer value.
116      * @see Transparency
117      */

118     public int getTransparency() {
119         return (bufImg.getColorModel()).getTransparency();
120     }
121
122 }
123
124
Popular Tags