author | henryjen |
Tue, 10 Jun 2014 16:18:54 -0700 | |
changeset 24865 | 09b1d992ca72 |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
|
436
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
48 |
* @version 1.48, 06/05/07 |
2 | 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 |
/** |
|
436
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
97 |
* Creates and returns a {@link PaintContext} used to |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
98 |
* generate a tiled image pattern. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
99 |
* See the {@link Paint#createContext specification} of the |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
100 |
* method in the {@link Paint} interface for information |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
101 |
* on null parameter handling. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
102 |
* |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
103 |
* @param cm the preferred {@link ColorModel} which represents the most convenient |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
104 |
* format for the caller to receive the pixel data, or {@code null} |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
105 |
* if there is no preference. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
106 |
* @param deviceBounds the device space bounding box |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
107 |
* of the graphics primitive being rendered. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
108 |
* @param userBounds the user space bounding box |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
109 |
* of the graphics primitive being rendered. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
110 |
* @param xform the {@link AffineTransform} from user |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
111 |
* space into device space. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
112 |
* @param hints the set of hints that the context object can use to |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
113 |
* choose between rendering alternatives. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
114 |
* @return the {@code PaintContext} for |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
115 |
* generating color patterns. |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
116 |
* @see Paint |
2 | 117 |
* @see PaintContext |
436
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
118 |
* @see ColorModel |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
119 |
* @see Rectangle |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
120 |
* @see Rectangle2D |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
121 |
* @see AffineTransform |
1cc586a58a3e
6573289: api/java_awt/Color/index.html#CreateContextTesttestCase4,5,6,7 fail since JDK 7 b14
dav
parents:
2
diff
changeset
|
122 |
* @see RenderingHints |
2 | 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 |
} |