2 * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
3 * <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program. Look for COPYING file in the top folder. If not, see
16 * http://opensource.org/licenses/GPL-2.0.
18 package net.java.html.canvas;
20 import net.java.html.canvas.Style.Color;
21 import net.java.html.canvas.Style.LinearGradient;
22 import net.java.html.canvas.Style.Pattern;
23 import net.java.html.canvas.Style.RadialGradient;
24 import net.java.html.canvas.spi.GraphicsEnvironment;
27 * A 2D Graphics Context similar to HTML5 or JavaFX GraphicsContext.
28 * Use this to paint on your Canvas.s
31 public final class GraphicsContext {
33 GraphicsEnvironment graphicsEnvironmentImpl;
35 public GraphicsContext(GraphicsEnvironment graphicsEnvironment) {
36 this.graphicsEnvironmentImpl = graphicsEnvironment;
39 public void arc(double centerX,
45 graphicsEnvironmentImpl.arc(centerX, centerY, startAngle, radius, endAngle, ccw);
48 public void arcTo(double x1,
53 graphicsEnvironmentImpl.arcTo(x1, y1, x2, y2, r);
56 public boolean isPointInPath(double x, double y) {
57 return graphicsEnvironmentImpl.isPointInPath(x, y);
61 graphicsEnvironmentImpl.fill();
64 public void stroke() {
65 graphicsEnvironmentImpl.stroke();
68 public void beginPath() {
69 graphicsEnvironmentImpl.beginPath();
72 public void closePath(){
73 graphicsEnvironmentImpl.closePath();
77 graphicsEnvironmentImpl.clip();
80 public void moveTo(double x, double y){
81 graphicsEnvironmentImpl.moveTo(x, y);
84 public void lineTo(double x, double y){
85 graphicsEnvironmentImpl.lineTo(x, y);
88 public void quadraticCurveTo(double cpx, double cpy, double x, double y){
89 graphicsEnvironmentImpl.quadraticCurveTo(cpx, cpy, x, y);
92 public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y){
93 graphicsEnvironmentImpl.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
96 public void fillRect(double x, double y, double width, double height){
97 graphicsEnvironmentImpl.fillRect(x, y, width, height);
100 public void strokeRect(double x, double y, double width, double height){
101 graphicsEnvironmentImpl.strokeRect(x, y, width, height);
104 public void clearRect(double x, double y, double width, double height){
105 graphicsEnvironmentImpl.clearRect(x, y, width, height);
108 public void rect(double x, double y, double width, double height){
109 graphicsEnvironmentImpl.rect(x, y, width, height);
113 graphicsEnvironmentImpl.save();
116 public void restore(){
117 graphicsEnvironmentImpl.restore();
120 public void rotate(double angle){
121 graphicsEnvironmentImpl.rotate(angle);
124 public void transform(double a, double b, double c, double d, double e, double f){
125 graphicsEnvironmentImpl.transform(a, b, c, d, e, f);
128 public void setTransform(double a, double b, double c, double d, double e, double f){
129 graphicsEnvironmentImpl.setTransform(a, b, c, d, e, f);
132 public void translate(double x, double y){
133 graphicsEnvironmentImpl.translate(x, y);
136 public void scale(double x, double y){
137 graphicsEnvironmentImpl.scale(x, y);
140 public void drawImage(Image image, double x, double y){
141 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, image.getCached());
142 image.cache(nativeImage);
145 public void drawImage(Image image, double x, double y, double width, double height){
146 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, width, height, image.getCached());
147 image.cache(nativeImage);
150 public void drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height){
151 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, sx, sy, sWidth, sHeight, x, y, width, height, image.getCached());
152 image.cache(nativeImage);
155 public void setShadowColor(String color){
156 graphicsEnvironmentImpl.setShadowColor(color);
159 public void setShadowBlur(double blur){
160 graphicsEnvironmentImpl.setShadowBlur(blur);
163 public void setShadowOffsetX(double x){
164 graphicsEnvironmentImpl.setShadowOffsetX(x);
167 public void setShadowOffsetY(double y){
168 graphicsEnvironmentImpl.setShadowOffsetY(y);
171 public String getShadowColor(){
172 return graphicsEnvironmentImpl.getShadowColor();
175 public double getShadowBlur(){
176 return graphicsEnvironmentImpl.getShadowBlur();
179 public double getShadowOffsetX(){
180 return graphicsEnvironmentImpl.getShadowOffsetX();
183 public double getShadowOffsetY(){
184 return graphicsEnvironmentImpl.getShadowOffsetY();
187 public String getLineCap(){
188 return graphicsEnvironmentImpl.getLineCap();
191 public void setLineCap(String style){
192 graphicsEnvironmentImpl.setLineCap(style);
195 public String getLineJoin(){
196 return graphicsEnvironmentImpl.getLineJoin();
199 public void setLineJoin(String style){
200 graphicsEnvironmentImpl.setLineJoin(style);
203 public double getLineWidth(){
204 return graphicsEnvironmentImpl.getLineWidth();
207 public void setLineWidth(double width){
208 graphicsEnvironmentImpl.setLineWidth(width);
211 public double getMiterLimit(){
212 return graphicsEnvironmentImpl.getMiterLimit();
215 public void setMiterLimit(double limit){
216 graphicsEnvironmentImpl.setMiterLimit(limit);
219 public void setFillStyle(Style style){
220 Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached());
221 style.cache(nativeFillStyle);
224 public String getFont(){
225 return graphicsEnvironmentImpl.getFont();
228 public void setFont(String font){
229 graphicsEnvironmentImpl.setFont(font);
232 public void setStrokeStyle(Style style){
233 Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached());
234 style.cache(nativeStrokeStyle);
237 public String getTextAlign(){
238 return graphicsEnvironmentImpl.getTextAlign();
241 public void setTextAlign(String textAlign){
242 graphicsEnvironmentImpl.setTextAlign(textAlign);
245 public String getTextBaseline(){
246 return graphicsEnvironmentImpl.getTextBaseline();
249 public void setTextBaseline(String textbaseline){
250 graphicsEnvironmentImpl.setTextBaseline(textbaseline);
253 public void fillText(String text, double x, double y){
254 graphicsEnvironmentImpl.fillText(text, x, y);
257 public void fillText(String text, double x, double y, double maxWidth){
258 graphicsEnvironmentImpl.fillText(text, x, y, maxWidth);
261 public Dimension measureText(String text){
262 return graphicsEnvironmentImpl.measureText(text);
265 public void strokeText(String text, double x, double y){
266 graphicsEnvironmentImpl.strokeText(text, x, y);
269 public void strokeText(String text, double x, double y, double maxWidth){
270 graphicsEnvironmentImpl.strokeText(text, x, y, maxWidth);
273 public ImageData createPixelMap(double x, double y){
274 return graphicsEnvironmentImpl.createPixelMap(x, y);
277 public ImageData createPixelMap(ImageData pixelMap){
278 return graphicsEnvironmentImpl.createPixelMap(pixelMap);
281 public ImageData getSnapshot(double x, double y, double width, double height){
282 return graphicsEnvironmentImpl.getPixelMap(x, y, width, height);
285 public void drawPixelMap(ImageData pixelMap, double x, double y){
286 graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y);
289 public void drawPixelMap(ImageData pixelMap, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight){
290 graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
293 public void setGlobalAlpha(double alpha){
294 graphicsEnvironmentImpl.setGlobalAlpha(alpha);
297 public double getGlobalAlpha(){
298 return graphicsEnvironmentImpl.getGlobalAlpha();
301 public void setGlobalCompositeOperation(String operation){
302 graphicsEnvironmentImpl.setGlobalCompositeOperation(operation);
305 public String getGlobalCompositeOperation(){
306 return graphicsEnvironmentImpl.getGlobalCompositeOperation();
309 public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1){
310 return new Style.LinearGradient(x0, y0, x1, y1);
313 public Pattern createPattern(Image image, String repeat){
314 return new Pattern(image, repeat);
317 public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1){
318 return new RadialGradient(x0, y0, r0, x1, y1, r1);
321 public Color getWebColor(String webColor){
322 return new Style.Color(webColor);
325 public Image getImageForPath(String path){
326 return new net.java.html.canvas.Image(path);
329 public int getHeight(){
330 return graphicsEnvironmentImpl.getHeight();
333 public int getWidth(){
334 return graphicsEnvironmentImpl.getWidth();
337 public void setHeight(int height){
338 graphicsEnvironmentImpl.setHeight(height);
341 public void setWidth(int width){
342 graphicsEnvironmentImpl.setWidth(width);