Some JavaDoc improvements.
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
33 public final class GraphicsContext {
35 public static void getAccssr() {
39 GraphicsEnvironment graphicsEnvironmentImpl;
42 CnvsAccssr cnvsAccssr = new CnvsAccssr() {
44 public GraphicsContext create(GraphicsEnvironment environment) {
45 return new GraphicsContext(environment);
50 GraphicsContext(GraphicsEnvironment graphicsEnvironment) {
51 this.graphicsEnvironmentImpl = graphicsEnvironment;
55 * Adds path elements to the current path to make an arc.
57 * @param centerX the center x position of the arc.
58 * @param centerY the center y position of the arc.
59 * @param radius the radius of the arc.
60 * @param endAngle teh endAngle of the arc
61 * @param ccw the direction of the arc (counterclockwise)
63 public void arc(double centerX,
69 graphicsEnvironmentImpl.arc(centerX, centerY, startAngle, radius, endAngle, ccw);
73 * Adds segments to the current path to make an arc.
75 * @param x1 the X coordinate of the first point of the arc.
76 * @param y1 the Y coordinate of the first point of the arc.
77 * @param x2 the X coordinate of the second point of the arc.
78 * @param y2 the Y coordinate of the second point of the arc.
79 * @param radius the radius of the arc in the range {0.0-positive infinity}.
81 public void arcTo(double x1,
86 graphicsEnvironmentImpl.arcTo(x1, y1, x2, y2, radius);
90 * Returns true if the the given x,y point is inside the path.
92 * @param x the X coordinate to use for the check.
93 * @param y the Y coordinate to use for the check.
94 * @return true if the point given is inside the path, false otherwise.
96 public boolean isPointInPath(double x, double y) {
97 return graphicsEnvironmentImpl.isPointInPath(x, y);
101 * Fills the path with the current fill paint.
104 graphicsEnvironmentImpl.fill();
108 * Strokes the path with the current stroke paint.
110 public void stroke() {
111 graphicsEnvironmentImpl.stroke();
117 public void beginPath() {
118 graphicsEnvironmentImpl.beginPath();
124 public void closePath() {
125 graphicsEnvironmentImpl.closePath();
129 * Clips using the current path
132 graphicsEnvironmentImpl.clip();
136 * Issues a move command for the current path to the given x,y coordinate.
138 * @param x0 the X position for the move to command.
139 * @param y0 the Y position for the move to command.
141 public void moveTo(double x, double y) {
142 graphicsEnvironmentImpl.moveTo(x, y);
146 * Adds segments to the current path to make a line at the given x,y
149 * @param x1 the X coordinate of the ending point of the line.
150 * @param y1 the Y coordinate of the ending point of the line.
152 public void lineTo(double x, double y) {
153 graphicsEnvironmentImpl.lineTo(x, y);
157 * Adds segments to the current path to make a quadratic curve.
159 * @param cpx the X coordinate of the control point
160 * @param cpy the Y coordinate of the control point
161 * @param x the X coordinate of the end point
162 * @param y the Y coordinate of the end point
164 public void quadraticCurveTo(double cpx, double cpy, double x, double y) {
165 graphicsEnvironmentImpl.quadraticCurveTo(cpx, cpy, x, y);
169 * Adds segments to the current path to make a cubic bezier curve.
171 * @param cp1x the X coordinate of first bezier control point.
172 * @param cp1y the Y coordinate of the first bezier control point.
173 * @param cp2x the X coordinate of the second bezier control point.
174 * @param cp2y the Y coordinate of the second bezier control point.
175 * @param x the X coordinate of the end point.
176 * @param y the Y coordinate of the end point.
178 public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y) {
179 graphicsEnvironmentImpl.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
183 * Fills a rectangle using the current fill paint.
185 * @param x the X position of the upper left corner of the rectangle.
186 * @param y the Y position of the upper left corner of the rectangle.
187 * @param w the width of the rectangle.
188 * @param h the height of the rectangle.
190 public void fillRect(double x, double y, double width, double height) {
191 graphicsEnvironmentImpl.fillRect(x, y, width, height);
195 * Strokes a rectangle using the current stroke paint.
197 * @param x the X position of the upper left corner of the rectangle.
198 * @param y the Y position of the upper left corner of the rectangle.
199 * @param width the width of the rectangle.
200 * @param height the height of the rectangle.
202 public void strokeRect(double x, double y, double width, double height) {
203 graphicsEnvironmentImpl.strokeRect(x, y, width, height);
207 * Clears a portion of the canvas with a transparent color value.
209 * @param x X position of the upper left corner of the rectangle.
210 * @param y Y position of the upper left corner of the rectangle.
211 * @param width width of the rectangle.
212 * @param height height of the rectangle.
214 public void clearRect(double x, double y, double width, double height) {
215 graphicsEnvironmentImpl.clearRect(x, y, width, height);
219 * Clears a portion of the canvas with a transparent color value.
221 * @param x X position of the upper left corner of the rectangle.
222 * @param y Y position of the upper left corner of the rectangle.
223 * @param width width of the rectangle.
224 * @param height height of the rectangle.
226 public void rect(double x, double y, double width, double height) {
227 graphicsEnvironmentImpl.rect(x, y, width, height);
231 * Saves the following attributes onto a stack.
233 * <li>Global Alpha</li>
234 * <li>Global Blend Operation</li>
236 * <li>Fill Paint</li>
237 * <li>Stroke Paint</li>
238 * <li>Line Width</li>
241 * <li>Miter Limit</li>
242 * <li>Number of Clip Paths</li>
244 * <li>Text Align</li>
245 * <li>Text Baseline</li>
249 * This method does NOT alter the current state in any way. Also, not that
250 * the current path is not saved.
253 graphicsEnvironmentImpl.save();
257 * Pops the state off of the stack, setting the following attributes to
258 * their value at the time when that state was pushed onto the stack. If the
259 * stack is empty then nothing is changed.
262 * <li>Global Alpha</li>
263 * <li>Global Blend Operation</li>
265 * <li>Fill Paint</li>
266 * <li>Stroke Paint</li>
267 * <li>Line Width</li>
270 * <li>Miter Limit</li>
271 * <li>Number of Clip Paths</li>
273 * <li>Text Align</li>
274 * <li>Text Baseline</li>
279 public void restore() {
280 graphicsEnvironmentImpl.restore();
284 * Rotates the current transform in degrees.
286 * @param angle value in degrees to rotate the current transform.
288 public void rotate(double angle) {
289 graphicsEnvironmentImpl.rotate(angle);
293 * Concatenates the input with the current transform.
295 * @param mxx - the X coordinate scaling element of the 3x4 matrix
296 * @param myx - the Y coordinate shearing element of the 3x4 matrix
297 * @param mxy - the X coordinate shearing element of the 3x4 matrix
298 * @param myy - the Y coordinate scaling element of the 3x4 matrix
299 * @param mxt - the X coordinate translation element of the 3x4 matrix
300 * @param myt - the Y coordinate translation element of the 3x4 matrix
302 public void transform(double mxx, double myx, double mxy, double myy, double mxt, double myt) {
303 graphicsEnvironmentImpl.transform(mxx, myx, mxy, myy, mxt, myt);
307 * Concatenates the input with the current transform.
309 * @param mxx - the X coordinate scaling element of the 3x4 matrix
310 * @param myx - the Y coordinate shearing element of the 3x4 matrix
311 * @param mxy - the X coordinate shearing element of the 3x4 matrix
312 * @param myy - the Y coordinate scaling element of the 3x4 matrix
313 * @param mxt - the X coordinate translation element of the 3x4 matrix
314 * @param myt - the Y coordinate translation element of the 3x4 matrix
316 public void setTransform(double mxx, double myx, double mxy, double myy, double mxt, double myt) {
317 graphicsEnvironmentImpl.setTransform(mxx, myx, mxy, myy, mxt, myt);
321 * Translates the current transform by x, y.
323 * @param x value to translate along the x axis.
324 * @param y value to translate along the y axis.
326 public void translate(double x, double y) {
327 graphicsEnvironmentImpl.translate(x, y);
331 * Scales the current transform by x, y.
333 * @param x value to scale in the x axis.
334 * @param y value to scale in the y axis.
336 public void scale(double x, double y) {
337 graphicsEnvironmentImpl.scale(x, y);
341 * Draws an image at the given x, y position using the width and height of
344 * @param img the image to be drawn.
345 * @param x the X coordinate on the destination for the upper left of the
347 * @param y the Y coordinate on the destination for the upper left of the
350 public void drawImage(Image image, double x, double y) {
351 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, image.getCached());
352 image.cache(nativeImage);
356 * Draws an image into the given destination rectangle of the canvas. The
357 * Image is scaled to fit into the destination rectagnle.
359 * @param img the image to be drawn.
360 * @param x the X coordinate on the destination for the upper left of the
362 * @param y the Y coordinate on the destination for the upper left of the
364 * @param width the width of the destination rectangle.
365 * @param height the height of the destination rectangle.
367 public void drawImage(Image image, double x, double y, double width, double height) {
368 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, width, height, image.getCached());
369 image.cache(nativeImage);
373 * Draws the current source rectangle of the given image to the given
374 * destination rectangle of the Canvas.
376 * @param img the image to be drawn.
377 * @param sx the source rectangle's X coordinate position.
378 * @param sy the source rectangle's Y coordinate position.
379 * @param sw the source rectangle's width.
380 * @param sh the source rectangle's height.
381 * @param dx the destination rectangle's X coordinate position.
382 * @param dy the destination rectangle's Y coordinate position.
383 * @param dw the destination rectangle's width.
384 * @param dh the destination rectangle's height.
386 public void drawImage(Image image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh) {
387 Object nativeImage = graphicsEnvironmentImpl.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh, image.getCached());
388 image.cache(nativeImage);
392 * Merges two images drawing one on top of the other and returning the
395 * @param a the lower Image
396 * @param b the upper Image
399 public Image merge(Image a, Image b) {
400 if (a.getCached() == null) {
403 if (b.getCached() == null) {
406 Object nativeImage = graphicsEnvironmentImpl.mergeImages(a, b, a.getCached(), b.getCached());
407 Image merged = Image.create("should add real path here");
408 merged.cache(nativeImage);
412 // public void setShadowColor(String color) {
413 // graphicsEnvironmentImpl.setShadowColor(color);
416 // public void setShadowBlur(double blur) {
417 // graphicsEnvironmentImpl.setShadowBlur(blur);
420 // public void setShadowOffsetX(double x) {
421 // graphicsEnvironmentImpl.setShadowOffsetX(x);
424 // public void setShadowOffsetY(double y) {
425 // graphicsEnvironmentImpl.setShadowOffsetY(y);
428 // public String getShadowColor() {
429 // return graphicsEnvironmentImpl.getShadowColor();
432 // public double getShadowBlur() {
433 // return graphicsEnvironmentImpl.getShadowBlur();
436 // public double getShadowOffsetX() {
437 // return graphicsEnvironmentImpl.getShadowOffsetX();
440 // public double getShadowOffsetY() {
441 // return graphicsEnvironmentImpl.getShadowOffsetY();
445 public String getLineCap() {
446 return graphicsEnvironmentImpl.getLineCap();
449 public void setLineCap(String style) {
450 graphicsEnvironmentImpl.setLineCap(style);
453 public String getLineJoin() {
454 return graphicsEnvironmentImpl.getLineJoin();
457 public void setLineJoin(String style) {
458 graphicsEnvironmentImpl.setLineJoin(style);
461 public double getLineWidth() {
462 return graphicsEnvironmentImpl.getLineWidth();
465 public void setLineWidth(double width) {
466 graphicsEnvironmentImpl.setLineWidth(width);
469 public double getMiterLimit() {
470 return graphicsEnvironmentImpl.getMiterLimit();
473 public void setMiterLimit(double limit) {
474 graphicsEnvironmentImpl.setMiterLimit(limit);
478 * Sets the fill style. Will be used when rendering something, e.g. calling one
479 * of the fillText Methods.
482 public void setFillStyle(Style style) {
483 Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached());
484 style.cache(nativeFillStyle);
488 * get the current font
490 * @return current Font.
491 * of the fillText Methods.
493 public String getFont() {
494 return graphicsEnvironmentImpl.getFont();
498 * Set the Font. Will be used when rendering Text, e.g. by calling one
499 * of the fillText Methods.
503 public void setFont(String font) {
504 graphicsEnvironmentImpl.setFont(font);
508 * sets the Style of the Stroke.
512 public void setStrokeStyle(Style style) {
513 Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached());
514 style.cache(nativeStrokeStyle);
518 * Gets the current TextAlignment attribute
520 * @return TextAlignment with values of left, center, right, or justify.
522 public String getTextAlign() {
523 return graphicsEnvironmentImpl.getTextAlign();
527 * Defines horizontal text alignment, relative to the text {@code x} origin.
529 * Let horizontal bounds represent the logical width of a single line of
530 * text. Where each line of text has a separate horizontal bounds.
532 * Then TextAlignment is specified as:
534 * <li>left: the left edge of the horizontal bounds will be at {@code x}.
535 * <li>center: the center, halfway between left and right edge, of the
536 * horizontal bounds will be at {@code x}.
537 * <li>right: the right edge of the horizontal bounds will be at {@code x}.
541 * Note: Canvas does not support line wrapping, therefore the text alignment
542 * Justify is identical to left aligned text.
545 * @param textAlign with values of left, center, right.
547 public void setTextAlign(String textAlign) {
548 graphicsEnvironmentImpl.setTextAlign(textAlign);
552 * Gets the current Text Baseline attribute.
554 * @return baseline with values of top, center, baseline, or bottom
556 public String getTextBaseline() {
557 return graphicsEnvironmentImpl.getTextBaseline();
561 * Sets the current Text Baseline attribute.
563 * @param baseline with values of top, center, baseline, or bottom
565 public void setTextBaseline(String textbaseline) {
566 graphicsEnvironmentImpl.setTextBaseline(textbaseline);
570 * Renders the indicated String with current fill. default is black.
572 * @param text the text to stroke
573 * @param x x coordinate of start position
574 * @param y y coordinate of start position
576 public void fillText(String text, double x, double y) {
577 graphicsEnvironmentImpl.fillText(text, x, y);
581 * Renders the indicated String with current fill. default is black.
583 * @param text the text to stroke
584 * @param x x coordinate of start position
585 * @param y y coordinate of start position
586 * @param maxWidth maximum width of text
588 public void fillText(String text, double x, double y, double maxWidth) {
589 graphicsEnvironmentImpl.fillText(text, x, y, maxWidth);
593 * Check the length of a text before writing it to the Canvas. Takes into
594 * account the current Font.
596 * @param text the text to measure
597 * @return the length in pixels
599 public Dimension measureText(String text) {
600 return graphicsEnvironmentImpl.measureText(text);
604 * Renders the indicated String (with no fill)
606 * @param text the text to stroke
607 * @param x x coordinate of start position
608 * @param y y coordinate of start position
610 public void strokeText(String text, double x, double y) {
611 graphicsEnvironmentImpl.strokeText(text, x, y);
615 * Renders the indicated String (with no fill)
617 * @param text the text to stroke
618 * @param x x coordinate of start position
619 * @param y y coordinate of start position
620 * @param maxWidth maximum width of text
622 public void strokeText(String text, double x, double y, double maxWidth) {
623 graphicsEnvironmentImpl.strokeText(text, x, y, maxWidth);
626 // public ImageData createPixelMap(double x, double y) {
627 // return graphicsEnvironmentImpl.createPixelMap(x, y);
630 // public ImageData createPixelMap(ImageData pixelMap) {
631 // return graphicsEnvironmentImpl.createPixelMap(pixelMap);
634 // public ImageData getSnapshot(double x, double y, double width, double height) {
635 // return graphicsEnvironmentImpl.getPixelMap(x, y, width, height);
638 // public void drawPixelMap(ImageData pixelMap, double x, double y) {
639 // graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y);
642 // public void drawPixelMap(ImageData pixelMap, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
643 // graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
646 * Sets the global alpha of the current state.
648 * @param alpha value in the range {@code 0.0-1.0}. The value is clamped if
649 * it is out of range.
651 public void setGlobalAlpha(double alpha) {
652 graphicsEnvironmentImpl.setGlobalAlpha(alpha);
656 * Gets the current global alpha.
658 * @return the current global alpha.
660 public double getGlobalAlpha() {
661 return graphicsEnvironmentImpl.getGlobalAlpha();
665 * Sets the global blend mode.
667 * @param op the BlendMode that will be set.
669 public void setGlobalCompositeOperation(String operation) {
670 graphicsEnvironmentImpl.setGlobalCompositeOperation(operation);
674 * Gets the global blend mode.
676 * @return the global BlendMode of the current state.
678 public String getGlobalCompositeOperation() {
679 return graphicsEnvironmentImpl.getGlobalCompositeOperation();
683 * Create a LinearGradient to use in Canvas.
685 * @param x0 x coordinate of start point
686 * @param y0 y coordinate of start point
687 * @param x1 x coordinate of end point
688 * @param y1 y coordinate of end point
689 * @return the gradient
691 public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
692 return new Style.LinearGradient(x0, y0, x1, y1);
696 * Create an Image Pattern from a source Image and a repeat style. Possible
697 * Styles are repeat, repeat-x, repeat-y, or no-repeat. defaults to repeat
699 * @param image the Image
700 * @param repeat the repeat style
701 * @return the Pattern
703 public Pattern createPattern(Image image, String repeat) {
704 return new Pattern(image, repeat);
708 * Create a RadialGradient
710 * @param x0 x Coordinate of starting circle
711 * @param y0 y Coordinate of starting circle
712 * @param r0 radius of starting circle
713 * @param x1 x coordinate of ending circle
714 * @param y1 y coordinate of ending circle
715 * @param r1 radius of ending circle
716 * @return the Gradient
718 public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
719 return new RadialGradient(x0, y0, r0, x1, y1, r1);
723 * Convert this String Representation of a Color to a Color Object.
726 * @return The Color represented by the input
728 public Color getWebColor(String webColor) {
729 return new Style.Color(webColor);
733 * Get the height of this GraphicsContext (which should be the same as the
734 * enclosing canvas height)
736 * @return the height of this GraphicsContext
738 public int getHeight() {
739 return graphicsEnvironmentImpl.getHeight();
743 * Get the width of this GraphicsContext (which should be the same as the
744 * enclosing canvas height)
746 * @return the width of this GraphicsContext
748 public int getWidth() {
749 return graphicsEnvironmentImpl.getWidth();
752 // public void setHeight(int height) {
753 // graphicsEnvironmentImpl.setHeight(height);
756 // public void setWidth(int width) {
757 // graphicsEnvironmentImpl.setWidth(width);
760 * Fill a circle with a center position of centerX, centerY and the
767 public void fillCircle(float centerX, float centerY, float radius) {
768 graphicsEnvironmentImpl.arc(centerX, centerY, radius, 0, Math.PI * 2, false);
772 * Fills a polygon with the given points using the currently set fill paint.
774 * @param x_coord array containing the x coordinates of the polygon's
776 * @param y_coord array containing the y coordinates of the polygon's
778 * @param vertexCount the number of points that make the polygon.
780 public void fillPolygon(double[] x_coord, double[] y_coord, int vertexCount) {
781 if (vertexCount >= 1 && x_coord != null && x_coord.length >= vertexCount && y_coord != null && y_coord.length >= vertexCount) {
782 graphicsEnvironmentImpl.beginPath();
784 graphicsEnvironmentImpl.moveTo(x_coord[0], y_coord[0]);
785 for (int i = 1; i < vertexCount; i++) {
786 graphicsEnvironmentImpl.lineTo(x_coord[i], y_coord[i]);
789 graphicsEnvironmentImpl.closePath();
790 graphicsEnvironmentImpl.fill();
791 graphicsEnvironmentImpl.stroke();