javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java
author Anton Epple <toni.epple@eppleton.de>
Thu, 26 Sep 2013 14:20:18 -0700
branchcanvas
changeset 1302 e67363288df1
parent 1263 088331d4cb76
child 1303 3d62ad46d744
permissions -rw-r--r--
refactoring for API review
     1 /**
     2  * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
     3  * <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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
    12  * details.
    13  *
    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.
    17  */
    18 package net.java.html.canvas;
    19 
    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;
    26 
    27 /**
    28  * A 2D Graphics Context similar to HTML5 or JavaFX GraphicsContext. Use this to
    29  * paint on your Canvas.s
    30  *
    31  * @author antonepple
    32  */
    33 public final class GraphicsContext {
    34 
    35     GraphicsEnvironment graphicsEnvironmentImpl;
    36 
    37     static {
    38         CnvsAccssr cnvsAccssr = new CnvsAccssr() {
    39             @Override
    40             public GraphicsContext create(GraphicsEnvironment environment) {
    41                 return new GraphicsContext(environment);
    42             }
    43         };
    44     }
    45 
    46     GraphicsContext(GraphicsEnvironment graphicsEnvironment) {
    47         this.graphicsEnvironmentImpl = graphicsEnvironment;
    48     }
    49 
    50     /**
    51      * Adds path elements to the current path to make an arc.
    52      *
    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)
    58      */
    59     public void arc(double centerX,
    60             double centerY,
    61             double startAngle,
    62             double radius,
    63             double endAngle,
    64             boolean ccw) {
    65         graphicsEnvironmentImpl.arc(centerX, centerY, startAngle, radius, endAngle, ccw);
    66     }
    67 
    68     /**
    69      * Adds segments to the current path to make an arc.
    70      *
    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}.
    76      */
    77     public void arcTo(double x1,
    78             double y1,
    79             double x2,
    80             double y2,
    81             double radius) {
    82         graphicsEnvironmentImpl.arcTo(x1, y1, x2, y2, radius);
    83     }
    84 
    85     /**
    86      * Returns true if the the given x,y point is inside the path.
    87      *
    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.
    91      */
    92     public boolean isPointInPath(double x, double y) {
    93         return graphicsEnvironmentImpl.isPointInPath(x, y);
    94     }
    95 
    96     /**
    97      * Fills the path with the current fill paint.
    98      */
    99     public void fill() {
   100         graphicsEnvironmentImpl.fill();
   101     }
   102 
   103     /**
   104      * Strokes the path with the current stroke paint.
   105      */
   106     public void stroke() {
   107         graphicsEnvironmentImpl.stroke();
   108     }
   109 
   110     /**
   111      * Starts a Path
   112      */
   113     public void beginPath() {
   114         graphicsEnvironmentImpl.beginPath();
   115     }
   116 
   117     /**
   118      * Closes the path.
   119      */
   120     public void closePath() {
   121         graphicsEnvironmentImpl.closePath();
   122     }
   123 
   124     /**
   125      * Clips using the current path
   126      */
   127     public void clip() {
   128         graphicsEnvironmentImpl.clip();
   129     }
   130 
   131     /**
   132      * Issues a move command for the current path to the given x,y coordinate.
   133      *
   134      * @param x0 the X position for the move to command.
   135      * @param y0 the Y position for the move to command.
   136      */
   137     public void moveTo(double x, double y) {
   138         graphicsEnvironmentImpl.moveTo(x, y);
   139     }
   140 
   141     /**
   142      * Adds segments to the current path to make a line at the given x,y
   143      * coordinate.
   144      *
   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.
   147      */
   148     public void lineTo(double x, double y) {
   149         graphicsEnvironmentImpl.lineTo(x, y);
   150     }
   151 
   152     /**
   153      * Adds segments to the current path to make a quadratic curve.
   154      *
   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
   159      */
   160     public void quadraticCurveTo(double cpx, double cpy, double x, double y) {
   161         graphicsEnvironmentImpl.quadraticCurveTo(cpx, cpy, x, y);
   162     }
   163 
   164     /**
   165      * Adds segments to the current path to make a cubic bezier curve.
   166      *
   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.
   173      */
   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);
   176     }
   177 
   178     /**
   179      * Fills a rectangle using the current fill paint.
   180      *
   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.
   185      */
   186     public void fillRect(double x, double y, double width, double height) {
   187         graphicsEnvironmentImpl.fillRect(x, y, width, height);
   188     }
   189 
   190     /**
   191      * Strokes a rectangle using the current stroke paint.
   192      *
   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.
   197      */
   198     public void strokeRect(double x, double y, double width, double height) {
   199         graphicsEnvironmentImpl.strokeRect(x, y, width, height);
   200     }
   201 
   202     /**
   203      * Clears a portion of the canvas with a transparent color value.
   204      *
   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.
   209      */
   210     public void clearRect(double x, double y, double width, double height) {
   211         graphicsEnvironmentImpl.clearRect(x, y, width, height);
   212     }
   213 
   214     /**
   215      * Clears a portion of the canvas with a transparent color value.
   216      *
   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.
   221      */
   222     public void rect(double x, double y, double width, double height) {
   223         graphicsEnvironmentImpl.rect(x, y, width, height);
   224     }
   225 
   226     /**
   227      * Saves the following attributes onto a stack.
   228      * <ul>
   229      * <li>Global Alpha</li>
   230      * <li>Global Blend Operation</li>
   231      * <li>Transform</li>
   232      * <li>Fill Paint</li>
   233      * <li>Stroke Paint</li>
   234      * <li>Line Width</li>
   235      * <li>Line Cap</li>
   236      * <li>Line Join</li>
   237      * <li>Miter Limit</li>
   238      * <li>Number of Clip Paths</li>
   239      * <li>Font</li>
   240      * <li>Text Align</li>
   241      * <li>Text Baseline</li>
   242      * <li>Effect</li>
   243      * <li>Fill Rule</li>
   244      * </ul>
   245      * This method does NOT alter the current state in any way. Also, not that
   246      * the current path is not saved.
   247      */
   248     public void save() {
   249         graphicsEnvironmentImpl.save();
   250     }
   251 
   252     /**
   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.
   256      *
   257      * <ul>
   258      * <li>Global Alpha</li>
   259      * <li>Global Blend Operation</li>
   260      * <li>Transform</li>
   261      * <li>Fill Paint</li>
   262      * <li>Stroke Paint</li>
   263      * <li>Line Width</li>
   264      * <li>Line Cap</li>
   265      * <li>Line Join</li>
   266      * <li>Miter Limit</li>
   267      * <li>Number of Clip Paths</li>
   268      * <li>Font</li>
   269      * <li>Text Align</li>
   270      * <li>Text Baseline</li>
   271      * <li>Effect</li>
   272      * <li>Fill Rule</li>
   273      * </ul>
   274      */
   275     public void restore() {
   276         graphicsEnvironmentImpl.restore();
   277     }
   278 
   279     /**
   280      * Rotates the current transform in degrees.
   281      *
   282      * @param angle value in degrees to rotate the current transform.
   283      */
   284     public void rotate(double angle) {
   285         graphicsEnvironmentImpl.rotate(angle);
   286     }
   287 
   288     /**
   289      * Concatenates the input with the current transform.
   290      *
   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
   297      */
   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);
   300     }
   301 
   302     /**
   303      * Concatenates the input with the current transform.
   304      *
   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
   311      */
   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);
   314     }
   315 
   316     /**
   317      * Translates the current transform by x, y.
   318      *
   319      * @param x value to translate along the x axis.
   320      * @param y value to translate along the y axis.
   321      */
   322     public void translate(double x, double y) {
   323         graphicsEnvironmentImpl.translate(x, y);
   324     }
   325 
   326     /**
   327      * Scales the current transform by x, y.
   328      *
   329      * @param x value to scale in the x axis.
   330      * @param y value to scale in the y axis.
   331      */
   332     public void scale(double x, double y) {
   333         graphicsEnvironmentImpl.scale(x, y);
   334     }
   335 
   336     /**
   337      * Draws an image at the given x, y position using the width and height of
   338      * the given image.
   339      *
   340      * @param img the image to be drawn.
   341      * @param x the X coordinate on the destination for the upper left of the
   342      * image.
   343      * @param y the Y coordinate on the destination for the upper left of the
   344      * image.
   345      */
   346     public void drawImage(Image image, double x, double y) {
   347         Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, image.getCached());
   348         image.cache(nativeImage);
   349     }
   350 
   351     /**
   352      * Draws an image into the given destination rectangle of the canvas. The
   353      * Image is scaled to fit into the destination rectagnle.
   354      *
   355      * @param img the image to be drawn.
   356      * @param x the X coordinate on the destination for the upper left of the
   357      * image.
   358      * @param y the Y coordinate on the destination for the upper left of the
   359      * image.
   360      * @param width the width of the destination rectangle.
   361      * @param height the height of the destination rectangle.
   362      */
   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);
   366     }
   367 
   368     /**
   369      * Draws the current source rectangle of the given image to the given
   370      * destination rectangle of the Canvas.
   371      *
   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.
   381      */
   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);
   385     }
   386 
   387     /**
   388      * Merges two images drawing one on top of the other and returning the
   389      * result.
   390      *
   391      * @param a the lower Image
   392      * @param b the upper Image
   393      * @return
   394      */
   395     public Image merge(Image a, Image b) {
   396         if (a.getCached() == null) {
   397             drawImage(a, 0, 0);
   398         }
   399         if (b.getCached() == null) {
   400             drawImage(b, 0, 0);
   401         }
   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);
   405         return merged;
   406     }
   407 
   408 //    public void setShadowColor(String color) {
   409 //        graphicsEnvironmentImpl.setShadowColor(color);
   410 //    }
   411 //
   412 //    public void setShadowBlur(double blur) {
   413 //        graphicsEnvironmentImpl.setShadowBlur(blur);
   414 //    }
   415 //
   416 //    public void setShadowOffsetX(double x) {
   417 //        graphicsEnvironmentImpl.setShadowOffsetX(x);
   418 //    }
   419 //
   420 //    public void setShadowOffsetY(double y) {
   421 //        graphicsEnvironmentImpl.setShadowOffsetY(y);
   422 //    }
   423 //
   424 //    public String getShadowColor() {
   425 //        return graphicsEnvironmentImpl.getShadowColor();
   426 //    }
   427 //
   428 //    public double getShadowBlur() {
   429 //        return graphicsEnvironmentImpl.getShadowBlur();
   430 //    }
   431 //
   432 //    public double getShadowOffsetX() {
   433 //        return graphicsEnvironmentImpl.getShadowOffsetX();
   434 //    }
   435 //
   436 //    public double getShadowOffsetY() {
   437 //        return graphicsEnvironmentImpl.getShadowOffsetY();
   438 //    }
   439     public String getLineCap() {
   440         return graphicsEnvironmentImpl.getLineCap();
   441     }
   442 
   443     public void setLineCap(String style) {
   444         graphicsEnvironmentImpl.setLineCap(style);
   445     }
   446 
   447     public String getLineJoin() {
   448         return graphicsEnvironmentImpl.getLineJoin();
   449     }
   450 
   451     public void setLineJoin(String style) {
   452         graphicsEnvironmentImpl.setLineJoin(style);
   453     }
   454 
   455     public double getLineWidth() {
   456         return graphicsEnvironmentImpl.getLineWidth();
   457     }
   458 
   459     public void setLineWidth(double width) {
   460         graphicsEnvironmentImpl.setLineWidth(width);
   461     }
   462 
   463     public double getMiterLimit() {
   464         return graphicsEnvironmentImpl.getMiterLimit();
   465     }
   466 
   467     public void setMiterLimit(double limit) {
   468         graphicsEnvironmentImpl.setMiterLimit(limit);
   469     }
   470 
   471     public void setFillStyle(Style style) {
   472         Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached());
   473         style.cache(nativeFillStyle);
   474     }
   475 
   476     public String getFont() {
   477         return graphicsEnvironmentImpl.getFont();
   478     }
   479 
   480     public void setFont(String font) {
   481         graphicsEnvironmentImpl.setFont(font);
   482     }
   483 
   484     public void setStrokeStyle(Style style) {
   485         Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached());
   486         style.cache(nativeStrokeStyle);
   487     }
   488 
   489     public String getTextAlign() {
   490         return graphicsEnvironmentImpl.getTextAlign();
   491     }
   492 
   493     public void setTextAlign(String textAlign) {
   494         graphicsEnvironmentImpl.setTextAlign(textAlign);
   495     }
   496 
   497     public String getTextBaseline() {
   498         return graphicsEnvironmentImpl.getTextBaseline();
   499     }
   500 
   501     public void setTextBaseline(String textbaseline) {
   502         graphicsEnvironmentImpl.setTextBaseline(textbaseline);
   503     }
   504 
   505     public void fillText(String text, double x, double y) {
   506         graphicsEnvironmentImpl.fillText(text, x, y);
   507     }
   508 
   509     public void fillText(String text, double x, double y, double maxWidth) {
   510         graphicsEnvironmentImpl.fillText(text, x, y, maxWidth);
   511     }
   512 
   513     public Dimension measureText(String text) {
   514         return graphicsEnvironmentImpl.measureText(text);
   515     }
   516 
   517     public void strokeText(String text, double x, double y) {
   518         graphicsEnvironmentImpl.strokeText(text, x, y);
   519     }
   520 
   521     public void strokeText(String text, double x, double y, double maxWidth) {
   522         graphicsEnvironmentImpl.strokeText(text, x, y, maxWidth);
   523     }
   524 
   525 //    public ImageData createPixelMap(double x, double y) {
   526 //        return graphicsEnvironmentImpl.createPixelMap(x, y);
   527 //    }
   528 //
   529 //    public ImageData createPixelMap(ImageData pixelMap) {
   530 //        return graphicsEnvironmentImpl.createPixelMap(pixelMap);
   531 //    }
   532 //
   533 //    public ImageData getSnapshot(double x, double y, double width, double height) {
   534 //        return graphicsEnvironmentImpl.getPixelMap(x, y, width, height);
   535 //    }
   536 //
   537 //    public void drawPixelMap(ImageData pixelMap, double x, double y) {
   538 //        graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y);
   539 //    }
   540 //
   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);
   543 //    }
   544     /**
   545      * Sets the global alpha of the current state.
   546      *
   547      * @param alpha value in the range {@code 0.0-1.0}. The value is clamped if
   548      * it is out of range.
   549      */
   550     public void setGlobalAlpha(double alpha) {
   551         graphicsEnvironmentImpl.setGlobalAlpha(alpha);
   552     }
   553 
   554     /**
   555      * Gets the current global alpha.
   556      *
   557      * @return the current global alpha.
   558      */
   559     public double getGlobalAlpha() {
   560         return graphicsEnvironmentImpl.getGlobalAlpha();
   561     }
   562 
   563     /**
   564      * Sets the global blend mode.
   565      *
   566      * @param op the BlendMode that will be set.
   567      */
   568     public void setGlobalCompositeOperation(String operation) {
   569         graphicsEnvironmentImpl.setGlobalCompositeOperation(operation);
   570     }
   571 
   572     /**
   573      * Gets the global blend mode.
   574      *
   575      * @return the global BlendMode of the current state.
   576      */
   577     public String getGlobalCompositeOperation() {
   578         return graphicsEnvironmentImpl.getGlobalCompositeOperation();
   579     }
   580 
   581     public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
   582         return new Style.LinearGradient(x0, y0, x1, y1);
   583     }
   584 
   585     public Pattern createPattern(Image image, String repeat) {
   586         return new Pattern(image, repeat);
   587     }
   588 
   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);
   591     }
   592 
   593     public Color getWebColor(String webColor) {
   594         return new Style.Color(webColor);
   595     }
   596 
   597     /**
   598      * Get the height of this GraphicsContext (which should be the same as the
   599      * enclosing canvas height)
   600      *
   601      * @return the height of this GraphicsContext
   602      */
   603     public int getHeight() {
   604         return graphicsEnvironmentImpl.getHeight();
   605     }
   606 
   607     /**
   608      * Get the width of this GraphicsContext (which should be the same as the
   609      * enclosing canvas height)
   610      *
   611      * @return the width of this GraphicsContext
   612      */
   613     public int getWidth() {
   614         return graphicsEnvironmentImpl.getWidth();
   615     }
   616 
   617 //    public void setHeight(int height) {
   618 //        graphicsEnvironmentImpl.setHeight(height);
   619 //    }
   620 //
   621 //    public void setWidth(int width) {
   622 //        graphicsEnvironmentImpl.setWidth(width);
   623 //    }
   624     /**
   625      * Fill a circle with a center position of centerX, centerY and the
   626      * specified radius.
   627      *
   628      * @param centerX
   629      * @param centerY
   630      * @param radius
   631      */
   632     public void fillCircle(float centerX, float centerY, float radius) {
   633         graphicsEnvironmentImpl.arc(centerX, centerY, radius, 0, Math.PI * 2, false);
   634     }
   635 
   636     /**
   637      * Fills a polygon with the given points using the currently set fill paint.
   638      *
   639      * @param x_coord array containing the x coordinates of the polygon's
   640      * points.
   641      * @param y_coord array containing the y coordinates of the polygon's
   642      * points.
   643      * @param vertexCount the number of points that make the polygon.
   644      */
   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();
   648         }
   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]);
   652 
   653         }
   654         graphicsEnvironmentImpl.closePath();
   655         graphicsEnvironmentImpl.fill();
   656         graphicsEnvironmentImpl.stroke();
   657     }
   658 }