toni@1111
|
1 |
/**
|
toni@1111
|
2 |
* Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
|
toni@1111
|
3 |
* <jaroslav.tulach@apidesign.org>
|
toni@1111
|
4 |
*
|
toni@1111
|
5 |
* This program is free software: you can redistribute it and/or modify it under
|
toni@1111
|
6 |
* the terms of the GNU General Public License as published by the Free Software
|
toni@1111
|
7 |
* Foundation, version 2 of the License.
|
toni@1111
|
8 |
*
|
toni@1111
|
9 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
toni@1111
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
toni@1111
|
11 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
toni@1111
|
12 |
* details.
|
toni@1111
|
13 |
*
|
toni@1111
|
14 |
* You should have received a copy of the GNU General Public License along with
|
toni@1111
|
15 |
* this program. Look for COPYING file in the top folder. If not, see
|
toni@1111
|
16 |
* http://opensource.org/licenses/GPL-2.0.
|
toni@521
|
17 |
*/
|
toni@1109
|
18 |
package net.java.html.canvas;
|
toni@521
|
19 |
|
toni@1150
|
20 |
import net.java.html.canvas.Style.Color;
|
toni@1150
|
21 |
import net.java.html.canvas.Style.LinearGradient;
|
toni@1150
|
22 |
import net.java.html.canvas.Style.Pattern;
|
toni@1150
|
23 |
import net.java.html.canvas.Style.RadialGradient;
|
toni@1136
|
24 |
import net.java.html.canvas.spi.GraphicsEnvironment;
|
toni@521
|
25 |
|
toni@521
|
26 |
/**
|
toni@1154
|
27 |
* A 2D Graphics Context similar to HTML5 or JavaFX GraphicsContext.
|
toni@1154
|
28 |
* Use this to paint on your Canvas.s
|
toni@1109
|
29 |
* @author antonepple
|
toni@521
|
30 |
*/
|
toni@1128
|
31 |
public final class GraphicsContext {
|
toni@1128
|
32 |
|
toni@1128
|
33 |
GraphicsEnvironment graphicsEnvironmentImpl;
|
toni@1128
|
34 |
|
toni@1128
|
35 |
public GraphicsContext(GraphicsEnvironment graphicsEnvironment) {
|
toni@1128
|
36 |
this.graphicsEnvironmentImpl = graphicsEnvironment;
|
toni@1128
|
37 |
}
|
toni@521
|
38 |
|
toni@1111
|
39 |
public void arc(double centerX,
|
toni@1111
|
40 |
double centerY,
|
toni@1111
|
41 |
double startAngle,
|
toni@1111
|
42 |
double radius,
|
toni@1111
|
43 |
double endAngle,
|
toni@1128
|
44 |
boolean ccw) {
|
toni@1128
|
45 |
graphicsEnvironmentImpl.arc(centerX, centerY, startAngle, radius, endAngle, ccw);
|
toni@1128
|
46 |
}
|
toni@521
|
47 |
|
toni@1111
|
48 |
public void arcTo(double x1,
|
toni@1111
|
49 |
double y1,
|
toni@1111
|
50 |
double x2,
|
toni@1111
|
51 |
double y2,
|
toni@1128
|
52 |
double r) {
|
toni@1128
|
53 |
graphicsEnvironmentImpl.arcTo(x1, y1, x2, y2, r);
|
toni@1128
|
54 |
}
|
toni@521
|
55 |
|
toni@1128
|
56 |
public boolean isPointInPath(double x, double y) {
|
toni@1128
|
57 |
return graphicsEnvironmentImpl.isPointInPath(x, y);
|
toni@1128
|
58 |
}
|
toni@521
|
59 |
|
toni@1128
|
60 |
public void fill() {
|
toni@1128
|
61 |
graphicsEnvironmentImpl.fill();
|
toni@1128
|
62 |
}
|
toni@521
|
63 |
|
toni@1128
|
64 |
public void stroke() {
|
toni@1128
|
65 |
graphicsEnvironmentImpl.stroke();
|
toni@1128
|
66 |
}
|
toni@1109
|
67 |
|
toni@1128
|
68 |
public void beginPath() {
|
toni@1128
|
69 |
graphicsEnvironmentImpl.beginPath();
|
toni@1128
|
70 |
}
|
toni@1109
|
71 |
|
toni@1128
|
72 |
public void closePath(){
|
toni@1128
|
73 |
graphicsEnvironmentImpl.closePath();
|
toni@1128
|
74 |
}
|
toni@521
|
75 |
|
toni@1128
|
76 |
public void clip(){
|
toni@1128
|
77 |
graphicsEnvironmentImpl.clip();
|
toni@1128
|
78 |
}
|
toni@1109
|
79 |
|
toni@1128
|
80 |
public void moveTo(double x, double y){
|
toni@1128
|
81 |
graphicsEnvironmentImpl.moveTo(x, y);
|
toni@1128
|
82 |
}
|
toni@1109
|
83 |
|
toni@1128
|
84 |
public void lineTo(double x, double y){
|
toni@1128
|
85 |
graphicsEnvironmentImpl.lineTo(x, y);
|
toni@1128
|
86 |
}
|
toni@1109
|
87 |
|
toni@1128
|
88 |
public void quadraticCurveTo(double cpx, double cpy, double x, double y){
|
toni@1128
|
89 |
graphicsEnvironmentImpl.quadraticCurveTo(cpx, cpy, x, y);
|
toni@1128
|
90 |
}
|
toni@1109
|
91 |
|
toni@1128
|
92 |
public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y){
|
toni@1128
|
93 |
graphicsEnvironmentImpl.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
|
toni@1128
|
94 |
}
|
toni@1109
|
95 |
|
toni@1128
|
96 |
public void fillRect(double x, double y, double width, double height){
|
toni@1128
|
97 |
graphicsEnvironmentImpl.fillRect(x, y, width, height);
|
toni@1128
|
98 |
}
|
toni@1109
|
99 |
|
toni@1128
|
100 |
public void strokeRect(double x, double y, double width, double height){
|
toni@1128
|
101 |
graphicsEnvironmentImpl.strokeRect(x, y, width, height);
|
toni@1128
|
102 |
}
|
toni@1109
|
103 |
|
toni@1128
|
104 |
public void clearRect(double x, double y, double width, double height){
|
toni@1128
|
105 |
graphicsEnvironmentImpl.clearRect(x, y, width, height);
|
toni@1128
|
106 |
}
|
toni@1109
|
107 |
|
toni@1128
|
108 |
public void rect(double x, double y, double width, double height){
|
toni@1128
|
109 |
graphicsEnvironmentImpl.rect(x, y, width, height);
|
toni@1128
|
110 |
}
|
toni@1109
|
111 |
|
toni@1128
|
112 |
public void save(){
|
toni@1128
|
113 |
graphicsEnvironmentImpl.save();
|
toni@1128
|
114 |
}
|
toni@1109
|
115 |
|
toni@1128
|
116 |
public void restore(){
|
toni@1128
|
117 |
graphicsEnvironmentImpl.restore();
|
toni@1128
|
118 |
}
|
toni@1109
|
119 |
|
toni@1128
|
120 |
public void rotate(double angle){
|
toni@1128
|
121 |
graphicsEnvironmentImpl.rotate(angle);
|
toni@1128
|
122 |
}
|
toni@1109
|
123 |
|
toni@1128
|
124 |
public void transform(double a, double b, double c, double d, double e, double f){
|
toni@1128
|
125 |
graphicsEnvironmentImpl.transform(a, b, c, d, e, f);
|
toni@1128
|
126 |
}
|
toni@1109
|
127 |
|
toni@1128
|
128 |
public void setTransform(double a, double b, double c, double d, double e, double f){
|
toni@1128
|
129 |
graphicsEnvironmentImpl.setTransform(a, b, c, d, e, f);
|
toni@1128
|
130 |
}
|
toni@1109
|
131 |
|
toni@1128
|
132 |
public void translate(double x, double y){
|
toni@1128
|
133 |
graphicsEnvironmentImpl.translate(x, y);
|
toni@1128
|
134 |
}
|
toni@1109
|
135 |
|
toni@1128
|
136 |
public void scale(double x, double y){
|
toni@1128
|
137 |
graphicsEnvironmentImpl.scale(x, y);
|
toni@1128
|
138 |
}
|
toni@1109
|
139 |
|
toni@1160
|
140 |
public void drawImage(Image image, double x, double y){
|
toni@1144
|
141 |
Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, image.getCached());
|
toni@1144
|
142 |
image.cache(nativeImage);
|
toni@1144
|
143 |
}
|
toni@1144
|
144 |
|
toni@1160
|
145 |
public void drawImage(Image image, double x, double y, double width, double height){
|
toni@1144
|
146 |
Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, width, height, image.getCached());
|
toni@1144
|
147 |
image.cache(nativeImage);
|
toni@1144
|
148 |
}
|
toni@1144
|
149 |
|
toni@1160
|
150 |
public void drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height){
|
toni@1144
|
151 |
Object nativeImage = graphicsEnvironmentImpl.drawImage(image, sx, sy, sWidth, sHeight, x, y, width, height, image.getCached());
|
toni@1144
|
152 |
image.cache(nativeImage);
|
toni@1144
|
153 |
}
|
toni@1263
|
154 |
|
toni@1263
|
155 |
public Image merge(Image a, Image b){
|
toni@1263
|
156 |
if(a.getCached()==null){
|
toni@1263
|
157 |
drawImage(a, 0, 0);
|
toni@1263
|
158 |
}
|
toni@1263
|
159 |
if(b.getCached()==null){
|
toni@1263
|
160 |
drawImage(b, 0, 0);
|
toni@1263
|
161 |
}
|
toni@1263
|
162 |
Object nativeImage = graphicsEnvironmentImpl.mergeImages(a,b,a.getCached(),b.getCached());
|
toni@1263
|
163 |
Image merged = Image.create("should add real path here");
|
toni@1263
|
164 |
merged.cache(nativeImage);
|
toni@1263
|
165 |
return merged;
|
toni@1263
|
166 |
}
|
toni@1109
|
167 |
|
toni@1128
|
168 |
public void setShadowColor(String color){
|
toni@1128
|
169 |
graphicsEnvironmentImpl.setShadowColor(color);
|
toni@1128
|
170 |
}
|
toni@1109
|
171 |
|
toni@1128
|
172 |
public void setShadowBlur(double blur){
|
toni@1128
|
173 |
graphicsEnvironmentImpl.setShadowBlur(blur);
|
toni@1128
|
174 |
}
|
toni@1109
|
175 |
|
toni@1128
|
176 |
public void setShadowOffsetX(double x){
|
toni@1128
|
177 |
graphicsEnvironmentImpl.setShadowOffsetX(x);
|
toni@1128
|
178 |
}
|
toni@1109
|
179 |
|
toni@1128
|
180 |
public void setShadowOffsetY(double y){
|
toni@1128
|
181 |
graphicsEnvironmentImpl.setShadowOffsetY(y);
|
toni@1128
|
182 |
}
|
toni@1109
|
183 |
|
toni@1128
|
184 |
public String getShadowColor(){
|
toni@1128
|
185 |
return graphicsEnvironmentImpl.getShadowColor();
|
toni@1128
|
186 |
}
|
toni@1109
|
187 |
|
toni@1128
|
188 |
public double getShadowBlur(){
|
toni@1128
|
189 |
return graphicsEnvironmentImpl.getShadowBlur();
|
toni@1128
|
190 |
}
|
toni@1109
|
191 |
|
toni@1128
|
192 |
public double getShadowOffsetX(){
|
toni@1128
|
193 |
return graphicsEnvironmentImpl.getShadowOffsetX();
|
toni@1128
|
194 |
}
|
toni@1109
|
195 |
|
toni@1128
|
196 |
public double getShadowOffsetY(){
|
toni@1128
|
197 |
return graphicsEnvironmentImpl.getShadowOffsetY();
|
toni@1128
|
198 |
}
|
toni@1109
|
199 |
|
toni@1128
|
200 |
public String getLineCap(){
|
toni@1128
|
201 |
return graphicsEnvironmentImpl.getLineCap();
|
toni@1128
|
202 |
}
|
toni@1109
|
203 |
|
toni@1128
|
204 |
public void setLineCap(String style){
|
toni@1128
|
205 |
graphicsEnvironmentImpl.setLineCap(style);
|
toni@1128
|
206 |
}
|
toni@1109
|
207 |
|
toni@1128
|
208 |
public String getLineJoin(){
|
toni@1128
|
209 |
return graphicsEnvironmentImpl.getLineJoin();
|
toni@1128
|
210 |
}
|
toni@1109
|
211 |
|
toni@1128
|
212 |
public void setLineJoin(String style){
|
toni@1128
|
213 |
graphicsEnvironmentImpl.setLineJoin(style);
|
toni@1128
|
214 |
}
|
toni@1109
|
215 |
|
toni@1128
|
216 |
public double getLineWidth(){
|
toni@1128
|
217 |
return graphicsEnvironmentImpl.getLineWidth();
|
toni@1128
|
218 |
}
|
toni@1109
|
219 |
|
toni@1128
|
220 |
public void setLineWidth(double width){
|
toni@1128
|
221 |
graphicsEnvironmentImpl.setLineWidth(width);
|
toni@1128
|
222 |
}
|
toni@1109
|
223 |
|
toni@1128
|
224 |
public double getMiterLimit(){
|
toni@1128
|
225 |
return graphicsEnvironmentImpl.getMiterLimit();
|
toni@1128
|
226 |
}
|
toni@1109
|
227 |
|
toni@1128
|
228 |
public void setMiterLimit(double limit){
|
toni@1128
|
229 |
graphicsEnvironmentImpl.setMiterLimit(limit);
|
toni@1128
|
230 |
}
|
toni@1141
|
231 |
|
toni@1141
|
232 |
public void setFillStyle(Style style){
|
toni@1141
|
233 |
Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached());
|
toni@1141
|
234 |
style.cache(nativeFillStyle);
|
toni@1141
|
235 |
}
|
toni@1109
|
236 |
|
toni@1128
|
237 |
public String getFont(){
|
toni@1128
|
238 |
return graphicsEnvironmentImpl.getFont();
|
toni@1128
|
239 |
}
|
toni@1109
|
240 |
|
toni@1128
|
241 |
public void setFont(String font){
|
toni@1128
|
242 |
graphicsEnvironmentImpl.setFont(font);
|
toni@1128
|
243 |
}
|
toni@1141
|
244 |
|
toni@1141
|
245 |
public void setStrokeStyle(Style style){
|
toni@1141
|
246 |
Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached());
|
toni@1141
|
247 |
style.cache(nativeStrokeStyle);
|
toni@1141
|
248 |
}
|
toni@1109
|
249 |
|
toni@1128
|
250 |
public String getTextAlign(){
|
toni@1128
|
251 |
return graphicsEnvironmentImpl.getTextAlign();
|
toni@1128
|
252 |
}
|
toni@521
|
253 |
|
toni@1128
|
254 |
public void setTextAlign(String textAlign){
|
toni@1128
|
255 |
graphicsEnvironmentImpl.setTextAlign(textAlign);
|
toni@1128
|
256 |
}
|
toni@521
|
257 |
|
toni@1128
|
258 |
public String getTextBaseline(){
|
toni@1128
|
259 |
return graphicsEnvironmentImpl.getTextBaseline();
|
toni@1128
|
260 |
}
|
toni@521
|
261 |
|
toni@1128
|
262 |
public void setTextBaseline(String textbaseline){
|
toni@1128
|
263 |
graphicsEnvironmentImpl.setTextBaseline(textbaseline);
|
toni@1128
|
264 |
}
|
toni@521
|
265 |
|
toni@1128
|
266 |
public void fillText(String text, double x, double y){
|
toni@1128
|
267 |
graphicsEnvironmentImpl.fillText(text, x, y);
|
toni@1128
|
268 |
}
|
toni@521
|
269 |
|
toni@1128
|
270 |
public void fillText(String text, double x, double y, double maxWidth){
|
toni@1128
|
271 |
graphicsEnvironmentImpl.fillText(text, x, y, maxWidth);
|
toni@1128
|
272 |
}
|
toni@521
|
273 |
|
toni@1128
|
274 |
public Dimension measureText(String text){
|
toni@1128
|
275 |
return graphicsEnvironmentImpl.measureText(text);
|
toni@1128
|
276 |
}
|
toni@1111
|
277 |
|
toni@1128
|
278 |
public void strokeText(String text, double x, double y){
|
toni@1128
|
279 |
graphicsEnvironmentImpl.strokeText(text, x, y);
|
toni@1128
|
280 |
}
|
toni@1111
|
281 |
|
toni@1128
|
282 |
public void strokeText(String text, double x, double y, double maxWidth){
|
toni@1128
|
283 |
graphicsEnvironmentImpl.strokeText(text, x, y, maxWidth);
|
toni@1128
|
284 |
}
|
toni@1111
|
285 |
|
toni@1144
|
286 |
public ImageData createPixelMap(double x, double y){
|
toni@1144
|
287 |
return graphicsEnvironmentImpl.createPixelMap(x, y);
|
toni@1144
|
288 |
}
|
toni@1144
|
289 |
|
toni@1144
|
290 |
public ImageData createPixelMap(ImageData pixelMap){
|
toni@1144
|
291 |
return graphicsEnvironmentImpl.createPixelMap(pixelMap);
|
toni@1144
|
292 |
}
|
toni@1144
|
293 |
|
toni@1144
|
294 |
public ImageData getSnapshot(double x, double y, double width, double height){
|
toni@1144
|
295 |
return graphicsEnvironmentImpl.getPixelMap(x, y, width, height);
|
toni@1144
|
296 |
}
|
toni@1144
|
297 |
|
toni@1160
|
298 |
public void drawPixelMap(ImageData pixelMap, double x, double y){
|
toni@1144
|
299 |
graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y);
|
toni@1144
|
300 |
}
|
toni@1144
|
301 |
|
toni@1160
|
302 |
public void drawPixelMap(ImageData pixelMap, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight){
|
toni@1144
|
303 |
graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
|
toni@1144
|
304 |
}
|
toni@1111
|
305 |
|
toni@1128
|
306 |
public void setGlobalAlpha(double alpha){
|
toni@1128
|
307 |
graphicsEnvironmentImpl.setGlobalAlpha(alpha);
|
toni@1128
|
308 |
}
|
toni@1111
|
309 |
|
toni@1128
|
310 |
public double getGlobalAlpha(){
|
toni@1128
|
311 |
return graphicsEnvironmentImpl.getGlobalAlpha();
|
toni@1128
|
312 |
}
|
toni@1111
|
313 |
|
toni@1128
|
314 |
public void setGlobalCompositeOperation(String operation){
|
toni@1128
|
315 |
graphicsEnvironmentImpl.setGlobalCompositeOperation(operation);
|
toni@1128
|
316 |
}
|
toni@1111
|
317 |
|
toni@1128
|
318 |
public String getGlobalCompositeOperation(){
|
toni@1128
|
319 |
return graphicsEnvironmentImpl.getGlobalCompositeOperation();
|
toni@1128
|
320 |
}
|
toni@1111
|
321 |
|
toni@1128
|
322 |
public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1){
|
toni@1150
|
323 |
return new Style.LinearGradient(x0, y0, x1, y1);
|
toni@1128
|
324 |
}
|
toni@1111
|
325 |
|
toni@1144
|
326 |
public Pattern createPattern(Image image, String repeat){
|
toni@1144
|
327 |
return new Pattern(image, repeat);
|
toni@1144
|
328 |
}
|
toni@1111
|
329 |
|
toni@1128
|
330 |
public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1){
|
toni@1131
|
331 |
return new RadialGradient(x0, y0, r0, x1, y1, r1);
|
toni@1128
|
332 |
}
|
toni@1150
|
333 |
|
toni@1150
|
334 |
public Color getWebColor(String webColor){
|
toni@1150
|
335 |
return new Style.Color(webColor);
|
toni@1150
|
336 |
}
|
toni@1111
|
337 |
|
toni@1128
|
338 |
public int getHeight(){
|
toni@1128
|
339 |
return graphicsEnvironmentImpl.getHeight();
|
toni@1128
|
340 |
}
|
toni@1111
|
341 |
|
toni@1128
|
342 |
public int getWidth(){
|
toni@1128
|
343 |
return graphicsEnvironmentImpl.getWidth();
|
toni@1128
|
344 |
}
|
toni@1111
|
345 |
|
toni@1128
|
346 |
public void setHeight(int height){
|
toni@1128
|
347 |
graphicsEnvironmentImpl.setHeight(height);
|
toni@1128
|
348 |
}
|
toni@1128
|
349 |
|
toni@1128
|
350 |
public void setWidth(int width){
|
toni@1128
|
351 |
graphicsEnvironmentImpl.setWidth(width);
|
toni@1128
|
352 |
}
|
toni@1263
|
353 |
|
toni@1263
|
354 |
public void fillCircle(float centerX, float centerY, float radius) {
|
toni@1263
|
355 |
graphicsEnvironmentImpl.arc(centerX, centerY, radius, 0, Math.PI*2, false);
|
toni@1263
|
356 |
}
|
toni@1263
|
357 |
|
toni@1263
|
358 |
public void fillPolygon(double[] x_coord, double[] y_coord, int vertexCount) {
|
toni@1263
|
359 |
if (vertexCount >=1&&x_coord!=null && x_coord.length>=vertexCount && y_coord!=null && y_coord.length>=vertexCount)
|
toni@1263
|
360 |
graphicsEnvironmentImpl.beginPath();
|
toni@1263
|
361 |
graphicsEnvironmentImpl.moveTo(x_coord[0], y_coord[0]);
|
toni@1263
|
362 |
for (int i = 1; i < vertexCount; i++) {
|
toni@1263
|
363 |
graphicsEnvironmentImpl.lineTo(x_coord[i], y_coord[i]);
|
toni@1263
|
364 |
|
toni@1263
|
365 |
}
|
toni@1263
|
366 |
graphicsEnvironmentImpl.closePath();
|
toni@1263
|
367 |
graphicsEnvironmentImpl.fill();
|
toni@1263
|
368 |
graphicsEnvironmentImpl.stroke();
|
toni@1263
|
369 |
|
toni@1263
|
370 |
|
toni@1263
|
371 |
}
|
toni@521
|
372 |
}
|