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;
25 import org.apidesign.html.canvas.impl.CnvsAccssr;
28 * A 2D Graphics Context similar to HTML5 or JavaFX GraphicsContext. Use this to
29 * paint on your Canvas.s
33 public final class GraphicsContext {
35 GraphicsEnvironment graphicsEnvironmentImpl;
38 CnvsAccssr cnvsAccssr = new CnvsAccssr() {
40 public GraphicsContext create(GraphicsEnvironment environment) {
41 return new GraphicsContext(environment);
46 GraphicsContext(GraphicsEnvironment graphicsEnvironment) {
47 this.graphicsEnvironmentImpl = graphicsEnvironment;
51 * Adds path elements to the current path to make an arc.
53 * @param centerX the center x position of the arc.
54 * @param centerY the center y position of the arc.
55 * @param radius the radius of the arc.
56 * @param endAngle teh endAngle of the arc
57 * @param ccw the direction of the arc (counterclockwise)
59 public void arc(double centerX,
65 graphicsEnvironmentImpl.arc(centerX, centerY, startAngle, radius, endAngle, ccw);
69 * Adds segments to the current path to make an arc.
71 * @param x1 the X coordinate of the first point of the arc.
72 * @param y1 the Y coordinate of the first point of the arc.
73 * @param x2 the X coordinate of the second point of the arc.
74 * @param y2 the Y coordinate of the second point of the arc.
75 * @param radius the radius of the arc in the range {0.0-positive infinity}.
77 public void arcTo(double x1,
82 graphicsEnvironmentImpl.arcTo(x1, y1, x2, y2, radius);
86 * Returns true if the the given x,y point is inside the path.
88 * @param x the X coordinate to use for the check.
89 * @param y the Y coordinate to use for the check.
90 * @return true if the point given is inside the path, false otherwise.
92 public boolean isPointInPath(double x, double y) {
93 return graphicsEnvironmentImpl.isPointInPath(x, y);
97 * Fills the path with the current fill paint.
100 graphicsEnvironmentImpl.fill();
104 * Strokes the path with the current stroke paint.
106 public void stroke() {
107 graphicsEnvironmentImpl.stroke();
113 public void beginPath() {
114 graphicsEnvironmentImpl.beginPath();
120 public void closePath() {
121 graphicsEnvironmentImpl.closePath();
125 * Clips using the current path
128 graphicsEnvironmentImpl.clip();
132 * Issues a move command for the current path to the given x,y coordinate.
134 * @param x0 the X position for the move to command.
135 * @param y0 the Y position for the move to command.
137 public void moveTo(double x, double y) {
138 graphicsEnvironmentImpl.moveTo(x, y);
142 * Adds segments to the current path to make a line at the given x,y
145 * @param x1 the X coordinate of the ending point of the line.
146 * @param y1 the Y coordinate of the ending point of the line.
148 public void lineTo(double x, double y) {
149 graphicsEnvironmentImpl.lineTo(x, y);
153 * Adds segments to the current path to make a quadratic curve.
155 * @param cpx the X coordinate of the control point
156 * @param cpy the Y coordinate of the control point
157 * @param x the X coordinate of the end point
158 * @param y the Y coordinate of the end point
160 public void quadraticCurveTo(double cpx, double cpy, double x, double y) {
161 graphicsEnvironmentImpl.quadraticCurveTo(cpx, cpy, x, y);
165 * Adds segments to the current path to make a cubic bezier curve.
167 * @param cp1x the X coordinate of first bezier control point.
168 * @param cp1y the Y coordinate of the first bezier control point.
169 * @param cp2x the X coordinate of the second bezier control point.
170 * @param cp2y the Y coordinate of the second bezier control point.
171 * @param x the X coordinate of the end point.
172 * @param y the Y coordinate of the end point.
174 public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y) {
175 graphicsEnvironmentImpl.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
179 * Fills a rectangle using the current fill paint.
181 * @param x the X position of the upper left corner of the rectangle.
182 * @param y the Y position of the upper left corner of the rectangle.
183 * @param w the width of the rectangle.
184 * @param h the height of the rectangle.
186 public void fillRect(double x, double y, double width, double height) {
187 graphicsEnvironmentImpl.fillRect(x, y, width, height);
191 * Strokes a rectangle using the current stroke paint.
193 * @param x the X position of the upper left corner of the rectangle.
194 * @param y the Y position of the upper left corner of the rectangle.
195 * @param width the width of the rectangle.
196 * @param height the height of the rectangle.
198 public void strokeRect(double x, double y, double width, double height) {
199 graphicsEnvironmentImpl.strokeRect(x, y, width, height);
203 * Clears a portion of the canvas with a transparent color value.
205 * @param x X position of the upper left corner of the rectangle.
206 * @param y Y position of the upper left corner of the rectangle.
207 * @param width width of the rectangle.
208 * @param height height of the rectangle.
210 public void clearRect(double x, double y, double width, double height) {
211 graphicsEnvironmentImpl.clearRect(x, y, width, height);
215 * Clears a portion of the canvas with a transparent color value.
217 * @param x X position of the upper left corner of the rectangle.
218 * @param y Y position of the upper left corner of the rectangle.
219 * @param width width of the rectangle.
220 * @param height height of the rectangle.
222 public void rect(double x, double y, double width, double height) {
223 graphicsEnvironmentImpl.rect(x, y, width, height);
227 * Saves the following attributes onto a stack.
229 * <li>Global Alpha</li>
230 * <li>Global Blend Operation</li>
232 * <li>Fill Paint</li>
233 * <li>Stroke Paint</li>
234 * <li>Line Width</li>
237 * <li>Miter Limit</li>
238 * <li>Number of Clip Paths</li>
240 * <li>Text Align</li>
241 * <li>Text Baseline</li>
245 * This method does NOT alter the current state in any way. Also, not that
246 * the current path is not saved.
249 graphicsEnvironmentImpl.save();
253 * Pops the state off of the stack, setting the following attributes to
254 * their value at the time when that state was pushed onto the stack. If the
255 * stack is empty then nothing is changed.
258 * <li>Global Alpha</li>
259 * <li>Global Blend Operation</li>
261 * <li>Fill Paint</li>
262 * <li>Stroke Paint</li>
263 * <li>Line Width</li>
266 * <li>Miter Limit</li>
267 * <li>Number of Clip Paths</li>
269 * <li>Text Align</li>
270 * <li>Text Baseline</li>
275 public void restore() {
276 graphicsEnvironmentImpl.restore();
280 * Rotates the current transform in degrees.
282 * @param angle value in degrees to rotate the current transform.
284 public void rotate(double angle) {
285 graphicsEnvironmentImpl.rotate(angle);
289 * Concatenates the input with the current transform.
291 * @param mxx - the X coordinate scaling element of the 3x4 matrix
292 * @param myx - the Y coordinate shearing element of the 3x4 matrix
293 * @param mxy - the X coordinate shearing element of the 3x4 matrix
294 * @param myy - the Y coordinate scaling element of the 3x4 matrix
295 * @param mxt - the X coordinate translation element of the 3x4 matrix
296 * @param myt - the Y coordinate translation element of the 3x4 matrix
298 public void transform(double mxx, double myx, double mxy, double myy, double mxt, double myt) {
299 graphicsEnvironmentImpl.transform(mxx, myx, mxy, myy, mxt, myt);
303 * Concatenates the input with the current transform.
305 * @param mxx - the X coordinate scaling element of the 3x4 matrix
306 * @param myx - the Y coordinate shearing element of the 3x4 matrix
307 * @param mxy - the X coordinate shearing element of the 3x4 matrix
308 * @param myy - the Y coordinate scaling element of the 3x4 matrix
309 * @param mxt - the X coordinate translation element of the 3x4 matrix
310 * @param myt - the Y coordinate translation element of the 3x4 matrix
312 public void setTransform(double mxx, double myx, double mxy, double myy, double mxt, double myt) {
313 graphicsEnvironmentImpl.setTransform(mxx, myx, mxy, myy, mxt, myt);
317 * Translates the current transform by x, y.
319 * @param x value to translate along the x axis.
320 * @param y value to translate along the y axis.
322 public void translate(double x, double y) {
323 graphicsEnvironmentImpl.translate(x, y);
327 * Scales the current transform by x, y.
329 * @param x value to scale in the x axis.
330 * @param y value to scale in the y axis.
332 public void scale(double x, double y) {
333 graphicsEnvironmentImpl.scale(x, y);
337 * Draws an image at the given x, y position using the width and height of
340 * @param img the image to be drawn.
341 * @param x the X coordinate on the destination for the upper left of the
343 * @param y the Y coordinate on the destination for the upper left of the
346 public void drawImage(Image image, double x, double y) {
347 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, image.getCached());
348 image.cache(nativeImage);
352 * Draws an image into the given destination rectangle of the canvas. The
353 * Image is scaled to fit into the destination rectagnle.
355 * @param img the image to be drawn.
356 * @param x the X coordinate on the destination for the upper left of the
358 * @param y the Y coordinate on the destination for the upper left of the
360 * @param width the width of the destination rectangle.
361 * @param height the height of the destination rectangle.
363 public void drawImage(Image image, double x, double y, double width, double height) {
364 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, width, height, image.getCached());
365 image.cache(nativeImage);
369 * Draws the current source rectangle of the given image to the given
370 * destination rectangle of the Canvas.
372 * @param img the image to be drawn.
373 * @param sx the source rectangle's X coordinate position.
374 * @param sy the source rectangle's Y coordinate position.
375 * @param sw the source rectangle's width.
376 * @param sh the source rectangle's height.
377 * @param dx the destination rectangle's X coordinate position.
378 * @param dy the destination rectangle's Y coordinate position.
379 * @param dw the destination rectangle's width.
380 * @param dh the destination rectangle's height.
382 public void drawImage(Image image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh) {
383 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh, image.getCached());
384 image.cache(nativeImage);
388 * Merges two images drawing one on top of the other and returning the
391 * @param a the lower Image
392 * @param b the upper Image
395 public Image merge(Image a, Image b) {
396 if (a.getCached() == null) {
399 if (b.getCached() == null) {
402 Object nativeImage = graphicsEnvironmentImpl.mergeImages(a, b, a.getCached(), b.getCached());
403 Image merged = Image.create("should add real path here");
404 merged.cache(nativeImage);
408 // public void setShadowColor(String color) {
409 // graphicsEnvironmentImpl.setShadowColor(color);
412 // public void setShadowBlur(double blur) {
413 // graphicsEnvironmentImpl.setShadowBlur(blur);
416 // public void setShadowOffsetX(double x) {
417 // graphicsEnvironmentImpl.setShadowOffsetX(x);
420 // public void setShadowOffsetY(double y) {
421 // graphicsEnvironmentImpl.setShadowOffsetY(y);
424 // public String getShadowColor() {
425 // return graphicsEnvironmentImpl.getShadowColor();
428 // public double getShadowBlur() {
429 // return graphicsEnvironmentImpl.getShadowBlur();
432 // public double getShadowOffsetX() {
433 // return graphicsEnvironmentImpl.getShadowOffsetX();
436 // public double getShadowOffsetY() {
437 // return graphicsEnvironmentImpl.getShadowOffsetY();
439 public String getLineCap() {
440 return graphicsEnvironmentImpl.getLineCap();
443 public void setLineCap(String style) {
444 graphicsEnvironmentImpl.setLineCap(style);
447 public String getLineJoin() {
448 return graphicsEnvironmentImpl.getLineJoin();
451 public void setLineJoin(String style) {
452 graphicsEnvironmentImpl.setLineJoin(style);
455 public double getLineWidth() {
456 return graphicsEnvironmentImpl.getLineWidth();
459 public void setLineWidth(double width) {
460 graphicsEnvironmentImpl.setLineWidth(width);
463 public double getMiterLimit() {
464 return graphicsEnvironmentImpl.getMiterLimit();
467 public void setMiterLimit(double limit) {
468 graphicsEnvironmentImpl.setMiterLimit(limit);
471 public void setFillStyle(Style style) {
472 Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached());
473 style.cache(nativeFillStyle);
476 public String getFont() {
477 return graphicsEnvironmentImpl.getFont();
480 public void setFont(String font) {
481 graphicsEnvironmentImpl.setFont(font);
484 public void setStrokeStyle(Style style) {
485 Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached());
486 style.cache(nativeStrokeStyle);
489 public String getTextAlign() {
490 return graphicsEnvironmentImpl.getTextAlign();
493 public void setTextAlign(String textAlign) {
494 graphicsEnvironmentImpl.setTextAlign(textAlign);
497 public String getTextBaseline() {
498 return graphicsEnvironmentImpl.getTextBaseline();
501 public void setTextBaseline(String textbaseline) {
502 graphicsEnvironmentImpl.setTextBaseline(textbaseline);
505 public void fillText(String text, double x, double y) {
506 graphicsEnvironmentImpl.fillText(text, x, y);
509 public void fillText(String text, double x, double y, double maxWidth) {
510 graphicsEnvironmentImpl.fillText(text, x, y, maxWidth);
513 public Dimension measureText(String text) {
514 return graphicsEnvironmentImpl.measureText(text);
517 public void strokeText(String text, double x, double y) {
518 graphicsEnvironmentImpl.strokeText(text, x, y);
521 public void strokeText(String text, double x, double y, double maxWidth) {
522 graphicsEnvironmentImpl.strokeText(text, x, y, maxWidth);
525 // public ImageData createPixelMap(double x, double y) {
526 // return graphicsEnvironmentImpl.createPixelMap(x, y);
529 // public ImageData createPixelMap(ImageData pixelMap) {
530 // return graphicsEnvironmentImpl.createPixelMap(pixelMap);
533 // public ImageData getSnapshot(double x, double y, double width, double height) {
534 // return graphicsEnvironmentImpl.getPixelMap(x, y, width, height);
537 // public void drawPixelMap(ImageData pixelMap, double x, double y) {
538 // graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y);
541 // public void drawPixelMap(ImageData pixelMap, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
542 // graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
545 * Sets the global alpha of the current state.
547 * @param alpha value in the range {@code 0.0-1.0}. The value is clamped if
548 * it is out of range.
550 public void setGlobalAlpha(double alpha) {
551 graphicsEnvironmentImpl.setGlobalAlpha(alpha);
555 * Gets the current global alpha.
557 * @return the current global alpha.
559 public double getGlobalAlpha() {
560 return graphicsEnvironmentImpl.getGlobalAlpha();
564 * Sets the global blend mode.
566 * @param op the BlendMode that will be set.
568 public void setGlobalCompositeOperation(String operation) {
569 graphicsEnvironmentImpl.setGlobalCompositeOperation(operation);
573 * Gets the global blend mode.
575 * @return the global BlendMode of the current state.
577 public String getGlobalCompositeOperation() {
578 return graphicsEnvironmentImpl.getGlobalCompositeOperation();
581 public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
582 return new Style.LinearGradient(x0, y0, x1, y1);
585 public Pattern createPattern(Image image, String repeat) {
586 return new Pattern(image, repeat);
589 public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
590 return new RadialGradient(x0, y0, r0, x1, y1, r1);
593 public Color getWebColor(String webColor) {
594 return new Style.Color(webColor);
598 * Get the height of this GraphicsContext (which should be the same as the
599 * enclosing canvas height)
601 * @return the height of this GraphicsContext
603 public int getHeight() {
604 return graphicsEnvironmentImpl.getHeight();
608 * Get the width of this GraphicsContext (which should be the same as the
609 * enclosing canvas height)
611 * @return the width of this GraphicsContext
613 public int getWidth() {
614 return graphicsEnvironmentImpl.getWidth();
617 // public void setHeight(int height) {
618 // graphicsEnvironmentImpl.setHeight(height);
621 // public void setWidth(int width) {
622 // graphicsEnvironmentImpl.setWidth(width);
625 * Fill a circle with a center position of centerX, centerY and the
632 public void fillCircle(float centerX, float centerY, float radius) {
633 graphicsEnvironmentImpl.arc(centerX, centerY, radius, 0, Math.PI * 2, false);
637 * Fills a polygon with the given points using the currently set fill paint.
639 * @param x_coord array containing the x coordinates of the polygon's
641 * @param y_coord array containing the y coordinates of the polygon's
643 * @param vertexCount the number of points that make the polygon.
645 public void fillPolygon(double[] x_coord, double[] y_coord, int vertexCount) {
646 if (vertexCount >= 1 && x_coord != null && x_coord.length >= vertexCount && y_coord != null && y_coord.length >= vertexCount) {
647 graphicsEnvironmentImpl.beginPath();
649 graphicsEnvironmentImpl.moveTo(x_coord[0], y_coord[0]);
650 for (int i = 1; i < vertexCount; i++) {
651 graphicsEnvironmentImpl.lineTo(x_coord[i], y_coord[i]);
654 graphicsEnvironmentImpl.closePath();
655 graphicsEnvironmentImpl.fill();
656 graphicsEnvironmentImpl.stroke();