Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PaintUtils |
|
| 1.0;1 |
1 | package org.jtheque.utils.ui; | |
2 | ||
3 | import java.awt.AlphaComposite; | |
4 | import java.awt.Color; | |
5 | import java.awt.Component; | |
6 | import java.awt.Composite; | |
7 | import java.awt.Font; | |
8 | import java.awt.Graphics; | |
9 | import java.awt.Graphics2D; | |
10 | import java.awt.Image; | |
11 | import java.awt.Insets; | |
12 | import java.awt.RenderingHints; | |
13 | import java.awt.image.BufferedImage; | |
14 | ||
15 | /* | |
16 | * This file is part of JTheque. | |
17 | * | |
18 | * JTheque is free software: you can redistribute it and/or modify | |
19 | * it under the terms of the GNU General Public License as published by | |
20 | * the Free Software Foundation, either version 3 of the License. | |
21 | * | |
22 | * JTheque is distributed in the hope that it will be useful, | |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | * GNU General Public License for more details. | |
26 | * | |
27 | * You should have received a copy of the GNU General Public License | |
28 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
29 | */ | |
30 | ||
31 | /** | |
32 | * Utility class to paint with Java2D. | |
33 | * <p/> | |
34 | * The method tileStretchPaint has been recuperated from the Aerith Project. | |
35 | * | |
36 | * @author Baptiste Wicht | |
37 | * @author Romain Guy/Chet Haase | |
38 | */ | |
39 | public final class PaintUtils { | |
40 | /** | |
41 | * Utility class, cannot be instantiated. | |
42 | */ | |
43 | private PaintUtils() { | |
44 | 0 | super(); |
45 | 0 | } |
46 | ||
47 | /** | |
48 | * Init the base hints for painting. | |
49 | * | |
50 | * @param g2 The graphics to init. | |
51 | */ | |
52 | public static void initHints(Graphics2D g2) { | |
53 | 0 | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
54 | 0 | g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
55 | 0 | g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
56 | 0 | } |
57 | ||
58 | /** | |
59 | * Draw a translucent image. | |
60 | * | |
61 | * @param g2 The graphics to paint with. | |
62 | * @param image The image to paint. | |
63 | * @param x The x position of the image. | |
64 | * @param y The y position of the image. | |
65 | * @param alpha The alpha value of the image painting. | |
66 | */ | |
67 | public static void drawAlphaImage(Graphics2D g2, BufferedImage image, int x, int y, float alpha) { | |
68 | 0 | drawAlphaImage(g2, image, x, y, image.getWidth(), image.getHeight(), alpha); |
69 | 0 | } |
70 | ||
71 | /** | |
72 | * Draw a translucent image with specified size. | |
73 | * | |
74 | * @param g2 The graphics to paint with. | |
75 | * @param image The image to paint. | |
76 | * @param x The x position of the image. | |
77 | * @param y The y position of the image. | |
78 | * @param width The width of the image. | |
79 | * @param height The height of the image. | |
80 | * @param alpha The alpha value of the image painting. | |
81 | */ | |
82 | public static void drawAlphaImage(Graphics2D g2, Image image, int x, int y, int width, int height, float alpha) { | |
83 | 0 | Composite oldComposite = g2.getComposite(); |
84 | ||
85 | 0 | g2.setComposite(AlphaComposite.SrcOver.derive(alpha)); |
86 | ||
87 | 0 | g2.drawImage(image, x, y, width, height, null); |
88 | ||
89 | 0 | g2.setComposite(oldComposite); |
90 | 0 | } |
91 | ||
92 | /** | |
93 | * Draw a translucent image with specified source and destination rectangles. | |
94 | * | |
95 | * @param g2 The graphics to paint with. | |
96 | * @param image The image to paint. | |
97 | * @param dx1 the <i>x</i> coordinate of the first corner of the destination rectangle. | |
98 | * @param dy1 the <i>y</i> coordinate of the first corner of the destination rectangle. | |
99 | * @param dx2 the <i>x</i> coordinate of the second corner of the destination rectangle. | |
100 | * @param dy2 the <i>y</i> coordinate of the second corner of the destination rectangle. | |
101 | * @param sx1 the <i>x</i> coordinate of the first corner of the source rectangle. | |
102 | * @param sy1 the <i>y</i> coordinate of the first corner of the source rectangle. | |
103 | * @param sx2 the <i>x</i> coordinate of the second corner of the source rectangle. | |
104 | * @param sy2 the <i>y</i> coordinate of the second corner of the source rectangle. | |
105 | * @param alpha The alpha value of the image painting. | |
106 | */ | |
107 | public static void drawAlphaImage(Graphics2D g2, Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, float alpha) { | |
108 | 0 | Composite oldComposite = g2.getComposite(); |
109 | ||
110 | 0 | g2.setComposite(AlphaComposite.SrcOver.derive(alpha)); |
111 | ||
112 | 0 | g2.drawImage(image, //Image |
113 | dx1, dy1, dx2, dy2, //Destination | |
114 | sx1, sy1, sx2, sy2, null); //Source | |
115 | ||
116 | 0 | g2.setComposite(oldComposite); |
117 | 0 | } |
118 | ||
119 | /** | |
120 | * Draw a string. | |
121 | * | |
122 | * @param g2 The graphics to paint with. | |
123 | * @param string The string to paint. | |
124 | * @param x The x position to paint from. | |
125 | * @param y The y position to paint from. | |
126 | * @param font The font to use. | |
127 | */ | |
128 | public static void drawString(Graphics g2, String string, int x, int y, Font font) { | |
129 | 0 | Font oldFont = g2.getFont(); |
130 | ||
131 | 0 | g2.setFont(font); |
132 | ||
133 | 0 | g2.drawString(string, x, y); |
134 | ||
135 | 0 | g2.setFont(oldFont); |
136 | 0 | } |
137 | ||
138 | /** | |
139 | * Draw a string. | |
140 | * | |
141 | * @param g2 The graphics to paint with. | |
142 | * @param string The string to paint. | |
143 | * @param x The x position to paint from. | |
144 | * @param y The y position to paint from. | |
145 | * @param font The font to use. | |
146 | * @param color The color to use. | |
147 | */ | |
148 | public static void drawString(Graphics g2, String string, int x, int y, Font font, Color color) { | |
149 | 0 | Color oldColor = g2.getColor(); |
150 | ||
151 | 0 | g2.setColor(color); |
152 | ||
153 | 0 | drawString(g2, string, x, y, font); |
154 | ||
155 | 0 | g2.setColor(oldColor); |
156 | 0 | } |
157 | ||
158 | /** | |
159 | * Fill a rectangle. | |
160 | * | |
161 | * @param g2 The graphics to paint with. | |
162 | * @param x The x position to paint from. | |
163 | * @param y The y position to paint from. | |
164 | * @param width The width of the rectangle. | |
165 | * @param height The height of the rectangle. | |
166 | * @param color The color to use. | |
167 | */ | |
168 | public static void fillRect(Graphics g2, int x, int y, int width, int height, Color color) { | |
169 | 0 | Color oldColor = g2.getColor(); |
170 | ||
171 | 0 | g2.setColor(color); |
172 | ||
173 | 0 | g2.fillRect(x, y, width, height); |
174 | ||
175 | 0 | g2.setColor(oldColor); |
176 | 0 | } |
177 | ||
178 | /** | |
179 | * Draws an image on top of a component by doing a 3x3 grid stretch of the image | |
180 | * using the specified insets. | |
181 | * | |
182 | * @param g The graphics object to use. | |
183 | * @param component The component. | |
184 | * @param image The image. | |
185 | * @param insets The insets. | |
186 | */ | |
187 | public static void tileStretchPaint(Graphics g, Component component, BufferedImage image, Insets insets) { | |
188 | 0 | int left = insets.left; |
189 | 0 | int right = insets.right; |
190 | 0 | int top = insets.top; |
191 | 0 | int bottom = insets.bottom; |
192 | ||
193 | // top | |
194 | 0 | g.drawImage(image, |
195 | 0, 0, left, top, | |
196 | 0, 0, left, top, | |
197 | null); | |
198 | 0 | g.drawImage(image, |
199 | left, 0, | |
200 | component.getWidth() - right, top, | |
201 | left, 0, | |
202 | image.getWidth() - right, top, | |
203 | null); | |
204 | 0 | g.drawImage(image, |
205 | component.getWidth() - right, 0, | |
206 | component.getWidth(), top, | |
207 | image.getWidth() - right, 0, | |
208 | image.getWidth(), top, | |
209 | null); | |
210 | ||
211 | // middle | |
212 | 0 | g.drawImage(image, |
213 | 0, top, | |
214 | left, component.getHeight() - bottom, | |
215 | 0, top, | |
216 | left, image.getHeight() - bottom, | |
217 | null); | |
218 | 0 | g.drawImage(image, |
219 | left, top, | |
220 | component.getWidth() - right, component.getHeight() - bottom, | |
221 | left, top, | |
222 | image.getWidth() - right, image.getHeight() - bottom, | |
223 | null); | |
224 | 0 | g.drawImage(image, |
225 | component.getWidth() - right, top, | |
226 | component.getWidth(), component.getHeight() - bottom, | |
227 | image.getWidth() - right, top, | |
228 | image.getWidth(), image.getHeight() - bottom, | |
229 | null); | |
230 | ||
231 | // bottom | |
232 | 0 | g.drawImage(image, |
233 | 0, component.getHeight() - bottom, | |
234 | left, component.getHeight(), | |
235 | 0, image.getHeight() - bottom, | |
236 | left, image.getHeight(), | |
237 | null); | |
238 | 0 | g.drawImage(image, |
239 | left, component.getHeight() - bottom, | |
240 | component.getWidth() - right, component.getHeight(), | |
241 | left, image.getHeight() - bottom, | |
242 | image.getWidth() - right, image.getHeight(), | |
243 | null); | |
244 | 0 | g.drawImage(image, |
245 | component.getWidth() - right, component.getHeight() - bottom, | |
246 | component.getWidth(), component.getHeight(), | |
247 | image.getWidth() - right, image.getHeight() - bottom, | |
248 | image.getWidth(), image.getHeight(), | |
249 | null); | |
250 | 0 | } |
251 | } |