javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java
author Anton Epple <toni.epple@eppleton.de>
Sat, 07 Sep 2013 18:25:09 +0200
branchcanvas
changeset 1263 088331d4cb76
parent 1160 6447c2031f1b
child 1302 e67363288df1
permissions -rw-r--r--
changes to canvas and graphicsenv to support additional methods fillPolygon and mergeImages
     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 
    26 /**
    27  * A 2D Graphics Context similar to HTML5 or JavaFX GraphicsContext. 
    28  * Use this to paint on your Canvas.s
    29  * @author antonepple
    30  */
    31 public final class GraphicsContext {
    32 
    33     GraphicsEnvironment graphicsEnvironmentImpl;
    34 
    35     public GraphicsContext(GraphicsEnvironment graphicsEnvironment) {
    36         this.graphicsEnvironmentImpl = graphicsEnvironment;
    37     }
    38 
    39     public void arc(double centerX,
    40             double centerY,
    41             double startAngle,
    42             double radius,
    43             double endAngle,
    44             boolean ccw) {
    45         graphicsEnvironmentImpl.arc(centerX, centerY, startAngle, radius, endAngle, ccw);
    46     }
    47 
    48     public void arcTo(double x1,
    49             double y1,
    50             double x2,
    51             double y2,
    52             double r) {
    53         graphicsEnvironmentImpl.arcTo(x1, y1, x2, y2, r);
    54     }
    55 
    56     public boolean isPointInPath(double x, double y) {
    57         return graphicsEnvironmentImpl.isPointInPath(x, y);
    58     }
    59 
    60     public void fill() {
    61         graphicsEnvironmentImpl.fill();
    62     }
    63 
    64     public void stroke() {
    65         graphicsEnvironmentImpl.stroke();
    66     }
    67 
    68     public void beginPath() {
    69         graphicsEnvironmentImpl.beginPath();
    70     }
    71 
    72     public void closePath(){
    73         graphicsEnvironmentImpl.closePath();
    74     }
    75 
    76     public void clip(){
    77         graphicsEnvironmentImpl.clip();
    78     }
    79 
    80     public void moveTo(double x, double y){
    81         graphicsEnvironmentImpl.moveTo(x, y);
    82     }
    83 
    84     public void lineTo(double x, double y){
    85         graphicsEnvironmentImpl.lineTo(x, y);
    86     }
    87 
    88     public void quadraticCurveTo(double cpx, double cpy, double x, double y){
    89     graphicsEnvironmentImpl.quadraticCurveTo(cpx, cpy, x, y);
    90     }
    91 
    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);
    94     }
    95 
    96     public void fillRect(double x, double y, double width, double height){
    97         graphicsEnvironmentImpl.fillRect(x, y, width, height);
    98     }
    99 
   100     public void strokeRect(double x, double y, double width, double height){
   101     graphicsEnvironmentImpl.strokeRect(x, y, width, height);
   102     }
   103 
   104     public void clearRect(double x, double y, double width, double height){
   105         graphicsEnvironmentImpl.clearRect(x, y, width, height);
   106     }
   107 
   108     public void rect(double x, double y, double width, double height){
   109         graphicsEnvironmentImpl.rect(x, y, width, height);
   110     }
   111 
   112     public void save(){
   113         graphicsEnvironmentImpl.save();
   114     }
   115 
   116     public void restore(){
   117         graphicsEnvironmentImpl.restore();
   118     }
   119 
   120     public void rotate(double angle){
   121         graphicsEnvironmentImpl.rotate(angle);
   122     }
   123 
   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);
   126     }
   127 
   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);
   130     }
   131 
   132     public void translate(double x, double y){
   133         graphicsEnvironmentImpl.translate(x, y);
   134     }
   135 
   136     public void scale(double x, double y){
   137         graphicsEnvironmentImpl.scale(x, y);
   138     }
   139 
   140     public void drawImage(Image image, double x, double y){
   141         Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, image.getCached());
   142         image.cache(nativeImage);
   143     }
   144 
   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);
   148     }
   149 
   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);
   153     }
   154     
   155     public Image merge(Image a, Image b){
   156         if(a.getCached()==null){
   157             drawImage(a, 0, 0);
   158         }
   159         if(b.getCached()==null){
   160             drawImage(b, 0, 0);
   161         }
   162         Object nativeImage = graphicsEnvironmentImpl.mergeImages(a,b,a.getCached(),b.getCached());
   163         Image merged = Image.create("should add real path here");
   164         merged.cache(nativeImage);
   165         return merged;
   166     }
   167 
   168     public void setShadowColor(String color){
   169         graphicsEnvironmentImpl.setShadowColor(color);
   170     }
   171 
   172     public void setShadowBlur(double blur){
   173         graphicsEnvironmentImpl.setShadowBlur(blur);
   174     }
   175 
   176     public void setShadowOffsetX(double x){
   177         graphicsEnvironmentImpl.setShadowOffsetX(x);
   178     }
   179 
   180     public void setShadowOffsetY(double y){
   181         graphicsEnvironmentImpl.setShadowOffsetY(y);
   182     }
   183 
   184     public String getShadowColor(){
   185         return graphicsEnvironmentImpl.getShadowColor();
   186     }
   187 
   188     public double getShadowBlur(){
   189         return graphicsEnvironmentImpl.getShadowBlur();
   190         }
   191 
   192     public double getShadowOffsetX(){
   193         return graphicsEnvironmentImpl.getShadowOffsetX();
   194     }
   195 
   196     public double getShadowOffsetY(){
   197         return graphicsEnvironmentImpl.getShadowOffsetY();
   198     }
   199 
   200     public String getLineCap(){
   201         return graphicsEnvironmentImpl.getLineCap();
   202     }
   203 
   204     public void setLineCap(String style){
   205         graphicsEnvironmentImpl.setLineCap(style);
   206     }
   207 
   208     public String getLineJoin(){
   209         return graphicsEnvironmentImpl.getLineJoin();
   210     }
   211 
   212     public void setLineJoin(String style){
   213         graphicsEnvironmentImpl.setLineJoin(style);
   214     }
   215 
   216     public double getLineWidth(){
   217         return graphicsEnvironmentImpl.getLineWidth();
   218     }
   219 
   220     public void setLineWidth(double width){
   221         graphicsEnvironmentImpl.setLineWidth(width);
   222     }
   223 
   224     public double getMiterLimit(){
   225         return graphicsEnvironmentImpl.getMiterLimit();
   226     }
   227 
   228     public void setMiterLimit(double limit){
   229         graphicsEnvironmentImpl.setMiterLimit(limit);
   230     }
   231     
   232     public void setFillStyle(Style style){
   233         Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached());
   234         style.cache(nativeFillStyle);
   235     }
   236 
   237     public String getFont(){
   238         return graphicsEnvironmentImpl.getFont();
   239     }
   240 
   241     public void setFont(String font){
   242         graphicsEnvironmentImpl.setFont(font);
   243     }
   244     
   245     public void setStrokeStyle(Style style){
   246         Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached());
   247         style.cache(nativeStrokeStyle);
   248     }
   249 
   250     public String getTextAlign(){
   251         return graphicsEnvironmentImpl.getTextAlign();
   252     }
   253 
   254     public void setTextAlign(String textAlign){
   255         graphicsEnvironmentImpl.setTextAlign(textAlign);
   256     }
   257 
   258     public String getTextBaseline(){
   259         return graphicsEnvironmentImpl.getTextBaseline();
   260     }
   261 
   262     public void setTextBaseline(String textbaseline){
   263         graphicsEnvironmentImpl.setTextBaseline(textbaseline);
   264     }
   265 
   266     public void fillText(String text, double x, double y){
   267         graphicsEnvironmentImpl.fillText(text, x, y);
   268     }
   269 
   270     public void fillText(String text, double x, double y, double maxWidth){
   271         graphicsEnvironmentImpl.fillText(text, x, y, maxWidth);
   272     }
   273 
   274     public Dimension measureText(String text){
   275         return graphicsEnvironmentImpl.measureText(text);
   276     }
   277 
   278     public void strokeText(String text, double x, double y){
   279         graphicsEnvironmentImpl.strokeText(text, x, y);
   280     }
   281 
   282     public void strokeText(String text, double x, double y, double maxWidth){
   283         graphicsEnvironmentImpl.strokeText(text, x, y, maxWidth);
   284     }
   285 
   286     public ImageData createPixelMap(double x, double y){
   287         return graphicsEnvironmentImpl.createPixelMap(x, y);
   288     }
   289 
   290     public ImageData createPixelMap(ImageData pixelMap){
   291         return graphicsEnvironmentImpl.createPixelMap(pixelMap);
   292     }
   293 
   294     public ImageData getSnapshot(double x, double y, double width, double height){
   295         return graphicsEnvironmentImpl.getPixelMap(x, y, width, height);
   296     }
   297 
   298     public void drawPixelMap(ImageData pixelMap, double x, double y){
   299         graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y);
   300     }
   301 
   302     public void drawPixelMap(ImageData pixelMap, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight){
   303         graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
   304     }
   305 
   306     public void setGlobalAlpha(double alpha){
   307         graphicsEnvironmentImpl.setGlobalAlpha(alpha);
   308     }
   309 
   310     public double getGlobalAlpha(){
   311         return graphicsEnvironmentImpl.getGlobalAlpha();
   312     }
   313 
   314     public void setGlobalCompositeOperation(String operation){
   315         graphicsEnvironmentImpl.setGlobalCompositeOperation(operation);
   316     }
   317 
   318     public String getGlobalCompositeOperation(){
   319         return graphicsEnvironmentImpl.getGlobalCompositeOperation();
   320     }
   321 
   322     public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1){
   323         return new Style.LinearGradient(x0, y0, x1, y1);
   324     }
   325 
   326     public Pattern createPattern(Image image, String repeat){
   327         return new Pattern(image, repeat);
   328     }
   329 
   330     public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1){
   331         return new RadialGradient(x0, y0, r0, x1, y1, r1);
   332     }
   333     
   334     public Color getWebColor(String webColor){
   335         return new Style.Color(webColor);
   336     }
   337 
   338     public int getHeight(){
   339         return graphicsEnvironmentImpl.getHeight();
   340     }
   341 
   342     public int getWidth(){
   343         return graphicsEnvironmentImpl.getWidth();
   344     }
   345 
   346     public void setHeight(int height){
   347         graphicsEnvironmentImpl.setHeight(height);
   348     }
   349 
   350     public void setWidth(int width){
   351         graphicsEnvironmentImpl.setWidth(width);
   352     }
   353 
   354     public void fillCircle(float centerX, float centerY, float radius) {
   355         graphicsEnvironmentImpl.arc(centerX, centerY, radius, 0, Math.PI*2, false);
   356     }
   357 
   358     public void fillPolygon(double[] x_coord, double[] y_coord, int vertexCount) {
   359         if (vertexCount >=1&&x_coord!=null && x_coord.length>=vertexCount && y_coord!=null && y_coord.length>=vertexCount)
   360         graphicsEnvironmentImpl.beginPath();
   361         graphicsEnvironmentImpl.moveTo(x_coord[0], y_coord[0]);
   362         for (int i = 1; i < vertexCount; i++) {
   363             graphicsEnvironmentImpl.lineTo(x_coord[i], y_coord[i]);
   364             
   365         }
   366         graphicsEnvironmentImpl.closePath();
   367         graphicsEnvironmentImpl.fill();
   368         graphicsEnvironmentImpl.stroke();
   369         
   370         
   371     }
   372 }