diff -r 088331d4cb76 -r e67363288df1 javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java --- a/javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java Sat Sep 07 18:25:09 2013 +0200 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java Thu Sep 26 14:20:18 2013 -0700 @@ -22,20 +22,40 @@ import net.java.html.canvas.Style.Pattern; import net.java.html.canvas.Style.RadialGradient; import net.java.html.canvas.spi.GraphicsEnvironment; +import org.apidesign.html.canvas.impl.CnvsAccssr; /** - * A 2D Graphics Context similar to HTML5 or JavaFX GraphicsContext. - * Use this to paint on your Canvas.s + * A 2D Graphics Context similar to HTML5 or JavaFX GraphicsContext. Use this to + * paint on your Canvas.s + * * @author antonepple */ public final class GraphicsContext { GraphicsEnvironment graphicsEnvironmentImpl; - public GraphicsContext(GraphicsEnvironment graphicsEnvironment) { + static { + CnvsAccssr cnvsAccssr = new CnvsAccssr() { + @Override + public GraphicsContext create(GraphicsEnvironment environment) { + return new GraphicsContext(environment); + } + }; + } + + GraphicsContext(GraphicsEnvironment graphicsEnvironment) { this.graphicsEnvironmentImpl = graphicsEnvironment; } + /** + * Adds path elements to the current path to make an arc. + * + * @param centerX the center x position of the arc. + * @param centerY the center y position of the arc. + * @param radius the radius of the arc. + * @param endAngle teh endAngle of the arc + * @param ccw the direction of the arc (counterclockwise) + */ public void arc(double centerX, double centerY, double startAngle, @@ -45,328 +65,594 @@ graphicsEnvironmentImpl.arc(centerX, centerY, startAngle, radius, endAngle, ccw); } + /** + * Adds segments to the current path to make an arc. + * + * @param x1 the X coordinate of the first point of the arc. + * @param y1 the Y coordinate of the first point of the arc. + * @param x2 the X coordinate of the second point of the arc. + * @param y2 the Y coordinate of the second point of the arc. + * @param radius the radius of the arc in the range {0.0-positive infinity}. + */ public void arcTo(double x1, double y1, double x2, double y2, - double r) { - graphicsEnvironmentImpl.arcTo(x1, y1, x2, y2, r); + double radius) { + graphicsEnvironmentImpl.arcTo(x1, y1, x2, y2, radius); } + /** + * Returns true if the the given x,y point is inside the path. + * + * @param x the X coordinate to use for the check. + * @param y the Y coordinate to use for the check. + * @return true if the point given is inside the path, false otherwise. + */ public boolean isPointInPath(double x, double y) { return graphicsEnvironmentImpl.isPointInPath(x, y); } + /** + * Fills the path with the current fill paint. + */ public void fill() { graphicsEnvironmentImpl.fill(); } + /** + * Strokes the path with the current stroke paint. + */ public void stroke() { graphicsEnvironmentImpl.stroke(); } + /** + * Starts a Path + */ public void beginPath() { graphicsEnvironmentImpl.beginPath(); } - public void closePath(){ + /** + * Closes the path. + */ + public void closePath() { graphicsEnvironmentImpl.closePath(); } - public void clip(){ + /** + * Clips using the current path + */ + public void clip() { graphicsEnvironmentImpl.clip(); } - public void moveTo(double x, double y){ + /** + * Issues a move command for the current path to the given x,y coordinate. + * + * @param x0 the X position for the move to command. + * @param y0 the Y position for the move to command. + */ + public void moveTo(double x, double y) { graphicsEnvironmentImpl.moveTo(x, y); } - public void lineTo(double x, double y){ + /** + * Adds segments to the current path to make a line at the given x,y + * coordinate. + * + * @param x1 the X coordinate of the ending point of the line. + * @param y1 the Y coordinate of the ending point of the line. + */ + public void lineTo(double x, double y) { graphicsEnvironmentImpl.lineTo(x, y); } - public void quadraticCurveTo(double cpx, double cpy, double x, double y){ - graphicsEnvironmentImpl.quadraticCurveTo(cpx, cpy, x, y); + /** + * Adds segments to the current path to make a quadratic curve. + * + * @param cpx the X coordinate of the control point + * @param cpy the Y coordinate of the control point + * @param x the X coordinate of the end point + * @param y the Y coordinate of the end point + */ + public void quadraticCurveTo(double cpx, double cpy, double x, double y) { + graphicsEnvironmentImpl.quadraticCurveTo(cpx, cpy, x, y); } - public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y){ + /** + * Adds segments to the current path to make a cubic bezier curve. + * + * @param cp1x the X coordinate of first bezier control point. + * @param cp1y the Y coordinate of the first bezier control point. + * @param cp2x the X coordinate of the second bezier control point. + * @param cp2y the Y coordinate of the second bezier control point. + * @param x the X coordinate of the end point. + * @param y the Y coordinate of the end point. + */ + public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y) { graphicsEnvironmentImpl.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y); } - public void fillRect(double x, double y, double width, double height){ + /** + * Fills a rectangle using the current fill paint. + * + * @param x the X position of the upper left corner of the rectangle. + * @param y the Y position of the upper left corner of the rectangle. + * @param w the width of the rectangle. + * @param h the height of the rectangle. + */ + public void fillRect(double x, double y, double width, double height) { graphicsEnvironmentImpl.fillRect(x, y, width, height); } - public void strokeRect(double x, double y, double width, double height){ - graphicsEnvironmentImpl.strokeRect(x, y, width, height); + /** + * Strokes a rectangle using the current stroke paint. + * + * @param x the X position of the upper left corner of the rectangle. + * @param y the Y position of the upper left corner of the rectangle. + * @param width the width of the rectangle. + * @param height the height of the rectangle. + */ + public void strokeRect(double x, double y, double width, double height) { + graphicsEnvironmentImpl.strokeRect(x, y, width, height); } - public void clearRect(double x, double y, double width, double height){ + /** + * Clears a portion of the canvas with a transparent color value. + * + * @param x X position of the upper left corner of the rectangle. + * @param y Y position of the upper left corner of the rectangle. + * @param width width of the rectangle. + * @param height height of the rectangle. + */ + public void clearRect(double x, double y, double width, double height) { graphicsEnvironmentImpl.clearRect(x, y, width, height); } - public void rect(double x, double y, double width, double height){ + /** + * Clears a portion of the canvas with a transparent color value. + * + * @param x X position of the upper left corner of the rectangle. + * @param y Y position of the upper left corner of the rectangle. + * @param width width of the rectangle. + * @param height height of the rectangle. + */ + public void rect(double x, double y, double width, double height) { graphicsEnvironmentImpl.rect(x, y, width, height); } - public void save(){ + /** + * Saves the following attributes onto a stack. + * + * This method does NOT alter the current state in any way. Also, not that + * the current path is not saved. + */ + public void save() { graphicsEnvironmentImpl.save(); } - public void restore(){ + /** + * Pops the state off of the stack, setting the following attributes to + * their value at the time when that state was pushed onto the stack. If the + * stack is empty then nothing is changed. + * + * + */ + public void restore() { graphicsEnvironmentImpl.restore(); } - public void rotate(double angle){ + /** + * Rotates the current transform in degrees. + * + * @param angle value in degrees to rotate the current transform. + */ + public void rotate(double angle) { graphicsEnvironmentImpl.rotate(angle); } - public void transform(double a, double b, double c, double d, double e, double f){ - graphicsEnvironmentImpl.transform(a, b, c, d, e, f); + /** + * Concatenates the input with the current transform. + * + * @param mxx - the X coordinate scaling element of the 3x4 matrix + * @param myx - the Y coordinate shearing element of the 3x4 matrix + * @param mxy - the X coordinate shearing element of the 3x4 matrix + * @param myy - the Y coordinate scaling element of the 3x4 matrix + * @param mxt - the X coordinate translation element of the 3x4 matrix + * @param myt - the Y coordinate translation element of the 3x4 matrix + */ + public void transform(double mxx, double myx, double mxy, double myy, double mxt, double myt) { + graphicsEnvironmentImpl.transform(mxx, myx, mxy, myy, mxt, myt); } - public void setTransform(double a, double b, double c, double d, double e, double f){ - graphicsEnvironmentImpl.setTransform(a, b, c, d, e, f); + /** + * Concatenates the input with the current transform. + * + * @param mxx - the X coordinate scaling element of the 3x4 matrix + * @param myx - the Y coordinate shearing element of the 3x4 matrix + * @param mxy - the X coordinate shearing element of the 3x4 matrix + * @param myy - the Y coordinate scaling element of the 3x4 matrix + * @param mxt - the X coordinate translation element of the 3x4 matrix + * @param myt - the Y coordinate translation element of the 3x4 matrix + */ + public void setTransform(double mxx, double myx, double mxy, double myy, double mxt, double myt) { + graphicsEnvironmentImpl.setTransform(mxx, myx, mxy, myy, mxt, myt); } - public void translate(double x, double y){ + /** + * Translates the current transform by x, y. + * + * @param x value to translate along the x axis. + * @param y value to translate along the y axis. + */ + public void translate(double x, double y) { graphicsEnvironmentImpl.translate(x, y); } - public void scale(double x, double y){ + /** + * Scales the current transform by x, y. + * + * @param x value to scale in the x axis. + * @param y value to scale in the y axis. + */ + public void scale(double x, double y) { graphicsEnvironmentImpl.scale(x, y); } - public void drawImage(Image image, double x, double y){ + /** + * Draws an image at the given x, y position using the width and height of + * the given image. + * + * @param img the image to be drawn. + * @param x the X coordinate on the destination for the upper left of the + * image. + * @param y the Y coordinate on the destination for the upper left of the + * image. + */ + public void drawImage(Image image, double x, double y) { Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, image.getCached()); image.cache(nativeImage); } - public void drawImage(Image image, double x, double y, double width, double height){ + /** + * Draws an image into the given destination rectangle of the canvas. The + * Image is scaled to fit into the destination rectagnle. + * + * @param img the image to be drawn. + * @param x the X coordinate on the destination for the upper left of the + * image. + * @param y the Y coordinate on the destination for the upper left of the + * image. + * @param width the width of the destination rectangle. + * @param height the height of the destination rectangle. + */ + public void drawImage(Image image, double x, double y, double width, double height) { Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, width, height, image.getCached()); image.cache(nativeImage); } - public void drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height){ - Object nativeImage = graphicsEnvironmentImpl.drawImage(image, sx, sy, sWidth, sHeight, x, y, width, height, image.getCached()); + /** + * Draws the current source rectangle of the given image to the given + * destination rectangle of the Canvas. + * + * @param img the image to be drawn. + * @param sx the source rectangle's X coordinate position. + * @param sy the source rectangle's Y coordinate position. + * @param sw the source rectangle's width. + * @param sh the source rectangle's height. + * @param dx the destination rectangle's X coordinate position. + * @param dy the destination rectangle's Y coordinate position. + * @param dw the destination rectangle's width. + * @param dh the destination rectangle's height. + */ + public void drawImage(Image image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh) { + Object nativeImage = graphicsEnvironmentImpl.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh, image.getCached()); image.cache(nativeImage); } - - public Image merge(Image a, Image b){ - if(a.getCached()==null){ + + /** + * Merges two images drawing one on top of the other and returning the + * result. + * + * @param a the lower Image + * @param b the upper Image + * @return + */ + public Image merge(Image a, Image b) { + if (a.getCached() == null) { drawImage(a, 0, 0); } - if(b.getCached()==null){ + if (b.getCached() == null) { drawImage(b, 0, 0); } - Object nativeImage = graphicsEnvironmentImpl.mergeImages(a,b,a.getCached(),b.getCached()); + Object nativeImage = graphicsEnvironmentImpl.mergeImages(a, b, a.getCached(), b.getCached()); Image merged = Image.create("should add real path here"); merged.cache(nativeImage); return merged; } - public void setShadowColor(String color){ - graphicsEnvironmentImpl.setShadowColor(color); - } - - public void setShadowBlur(double blur){ - graphicsEnvironmentImpl.setShadowBlur(blur); - } - - public void setShadowOffsetX(double x){ - graphicsEnvironmentImpl.setShadowOffsetX(x); - } - - public void setShadowOffsetY(double y){ - graphicsEnvironmentImpl.setShadowOffsetY(y); - } - - public String getShadowColor(){ - return graphicsEnvironmentImpl.getShadowColor(); - } - - public double getShadowBlur(){ - return graphicsEnvironmentImpl.getShadowBlur(); - } - - public double getShadowOffsetX(){ - return graphicsEnvironmentImpl.getShadowOffsetX(); - } - - public double getShadowOffsetY(){ - return graphicsEnvironmentImpl.getShadowOffsetY(); - } - - public String getLineCap(){ +// public void setShadowColor(String color) { +// graphicsEnvironmentImpl.setShadowColor(color); +// } +// +// public void setShadowBlur(double blur) { +// graphicsEnvironmentImpl.setShadowBlur(blur); +// } +// +// public void setShadowOffsetX(double x) { +// graphicsEnvironmentImpl.setShadowOffsetX(x); +// } +// +// public void setShadowOffsetY(double y) { +// graphicsEnvironmentImpl.setShadowOffsetY(y); +// } +// +// public String getShadowColor() { +// return graphicsEnvironmentImpl.getShadowColor(); +// } +// +// public double getShadowBlur() { +// return graphicsEnvironmentImpl.getShadowBlur(); +// } +// +// public double getShadowOffsetX() { +// return graphicsEnvironmentImpl.getShadowOffsetX(); +// } +// +// public double getShadowOffsetY() { +// return graphicsEnvironmentImpl.getShadowOffsetY(); +// } + public String getLineCap() { return graphicsEnvironmentImpl.getLineCap(); } - public void setLineCap(String style){ + public void setLineCap(String style) { graphicsEnvironmentImpl.setLineCap(style); } - public String getLineJoin(){ + public String getLineJoin() { return graphicsEnvironmentImpl.getLineJoin(); } - public void setLineJoin(String style){ + public void setLineJoin(String style) { graphicsEnvironmentImpl.setLineJoin(style); } - public double getLineWidth(){ + public double getLineWidth() { return graphicsEnvironmentImpl.getLineWidth(); } - public void setLineWidth(double width){ + public void setLineWidth(double width) { graphicsEnvironmentImpl.setLineWidth(width); } - public double getMiterLimit(){ + public double getMiterLimit() { return graphicsEnvironmentImpl.getMiterLimit(); } - public void setMiterLimit(double limit){ + public void setMiterLimit(double limit) { graphicsEnvironmentImpl.setMiterLimit(limit); } - - public void setFillStyle(Style style){ + + public void setFillStyle(Style style) { Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached()); style.cache(nativeFillStyle); } - public String getFont(){ + public String getFont() { return graphicsEnvironmentImpl.getFont(); } - public void setFont(String font){ + public void setFont(String font) { graphicsEnvironmentImpl.setFont(font); } - - public void setStrokeStyle(Style style){ + + public void setStrokeStyle(Style style) { Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached()); style.cache(nativeStrokeStyle); } - public String getTextAlign(){ + public String getTextAlign() { return graphicsEnvironmentImpl.getTextAlign(); } - public void setTextAlign(String textAlign){ + public void setTextAlign(String textAlign) { graphicsEnvironmentImpl.setTextAlign(textAlign); } - public String getTextBaseline(){ + public String getTextBaseline() { return graphicsEnvironmentImpl.getTextBaseline(); } - public void setTextBaseline(String textbaseline){ + public void setTextBaseline(String textbaseline) { graphicsEnvironmentImpl.setTextBaseline(textbaseline); } - public void fillText(String text, double x, double y){ + public void fillText(String text, double x, double y) { graphicsEnvironmentImpl.fillText(text, x, y); } - public void fillText(String text, double x, double y, double maxWidth){ + public void fillText(String text, double x, double y, double maxWidth) { graphicsEnvironmentImpl.fillText(text, x, y, maxWidth); } - public Dimension measureText(String text){ + public Dimension measureText(String text) { return graphicsEnvironmentImpl.measureText(text); } - public void strokeText(String text, double x, double y){ + public void strokeText(String text, double x, double y) { graphicsEnvironmentImpl.strokeText(text, x, y); } - public void strokeText(String text, double x, double y, double maxWidth){ + public void strokeText(String text, double x, double y, double maxWidth) { graphicsEnvironmentImpl.strokeText(text, x, y, maxWidth); } - public ImageData createPixelMap(double x, double y){ - return graphicsEnvironmentImpl.createPixelMap(x, y); - } - - public ImageData createPixelMap(ImageData pixelMap){ - return graphicsEnvironmentImpl.createPixelMap(pixelMap); - } - - public ImageData getSnapshot(double x, double y, double width, double height){ - return graphicsEnvironmentImpl.getPixelMap(x, y, width, height); - } - - public void drawPixelMap(ImageData pixelMap, double x, double y){ - graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y); - } - - public void drawPixelMap(ImageData pixelMap, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight){ - graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y, dirtyx, dirtyy, dirtywidth, dirtyheight); - } - - public void setGlobalAlpha(double alpha){ +// public ImageData createPixelMap(double x, double y) { +// return graphicsEnvironmentImpl.createPixelMap(x, y); +// } +// +// public ImageData createPixelMap(ImageData pixelMap) { +// return graphicsEnvironmentImpl.createPixelMap(pixelMap); +// } +// +// public ImageData getSnapshot(double x, double y, double width, double height) { +// return graphicsEnvironmentImpl.getPixelMap(x, y, width, height); +// } +// +// public void drawPixelMap(ImageData pixelMap, double x, double y) { +// graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y); +// } +// +// public void drawPixelMap(ImageData pixelMap, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) { +// graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y, dirtyx, dirtyy, dirtywidth, dirtyheight); +// } + /** + * Sets the global alpha of the current state. + * + * @param alpha value in the range {@code 0.0-1.0}. The value is clamped if + * it is out of range. + */ + public void setGlobalAlpha(double alpha) { graphicsEnvironmentImpl.setGlobalAlpha(alpha); } - public double getGlobalAlpha(){ + /** + * Gets the current global alpha. + * + * @return the current global alpha. + */ + public double getGlobalAlpha() { return graphicsEnvironmentImpl.getGlobalAlpha(); } - public void setGlobalCompositeOperation(String operation){ + /** + * Sets the global blend mode. + * + * @param op the BlendMode that will be set. + */ + public void setGlobalCompositeOperation(String operation) { graphicsEnvironmentImpl.setGlobalCompositeOperation(operation); } - public String getGlobalCompositeOperation(){ + /** + * Gets the global blend mode. + * + * @return the global BlendMode of the current state. + */ + public String getGlobalCompositeOperation() { return graphicsEnvironmentImpl.getGlobalCompositeOperation(); } - public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1){ + public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) { return new Style.LinearGradient(x0, y0, x1, y1); } - public Pattern createPattern(Image image, String repeat){ + public Pattern createPattern(Image image, String repeat) { return new Pattern(image, repeat); } - public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1){ + public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) { return new RadialGradient(x0, y0, r0, x1, y1, r1); } - - public Color getWebColor(String webColor){ + + public Color getWebColor(String webColor) { return new Style.Color(webColor); } - public int getHeight(){ + /** + * Get the height of this GraphicsContext (which should be the same as the + * enclosing canvas height) + * + * @return the height of this GraphicsContext + */ + public int getHeight() { return graphicsEnvironmentImpl.getHeight(); } - public int getWidth(){ + /** + * Get the width of this GraphicsContext (which should be the same as the + * enclosing canvas height) + * + * @return the width of this GraphicsContext + */ + public int getWidth() { return graphicsEnvironmentImpl.getWidth(); } - public void setHeight(int height){ - graphicsEnvironmentImpl.setHeight(height); +// public void setHeight(int height) { +// graphicsEnvironmentImpl.setHeight(height); +// } +// +// public void setWidth(int width) { +// graphicsEnvironmentImpl.setWidth(width); +// } + /** + * Fill a circle with a center position of centerX, centerY and the + * specified radius. + * + * @param centerX + * @param centerY + * @param radius + */ + public void fillCircle(float centerX, float centerY, float radius) { + graphicsEnvironmentImpl.arc(centerX, centerY, radius, 0, Math.PI * 2, false); } - public void setWidth(int width){ - graphicsEnvironmentImpl.setWidth(width); - } - - public void fillCircle(float centerX, float centerY, float radius) { - graphicsEnvironmentImpl.arc(centerX, centerY, radius, 0, Math.PI*2, false); - } - + /** + * Fills a polygon with the given points using the currently set fill paint. + * + * @param x_coord array containing the x coordinates of the polygon's + * points. + * @param y_coord array containing the y coordinates of the polygon's + * points. + * @param vertexCount the number of points that make the polygon. + */ public void fillPolygon(double[] x_coord, double[] y_coord, int vertexCount) { - if (vertexCount >=1&&x_coord!=null && x_coord.length>=vertexCount && y_coord!=null && y_coord.length>=vertexCount) - graphicsEnvironmentImpl.beginPath(); + if (vertexCount >= 1 && x_coord != null && x_coord.length >= vertexCount && y_coord != null && y_coord.length >= vertexCount) { + graphicsEnvironmentImpl.beginPath(); + } graphicsEnvironmentImpl.moveTo(x_coord[0], y_coord[0]); for (int i = 1; i < vertexCount; i++) { graphicsEnvironmentImpl.lineTo(x_coord[i], y_coord[i]); - + } graphicsEnvironmentImpl.closePath(); graphicsEnvironmentImpl.fill(); graphicsEnvironmentImpl.stroke(); - - } }