javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java
author Anton Epple <toni.epple@eppleton.de>
Tue, 28 May 2013 08:50:06 +0200
branchcanvas
changeset 1160 6447c2031f1b
parent 1158 633572e14095
child 1263 088331d4cb76
permissions -rw-r--r--
small refactoring (back to drawXXX instead of paintXXX)
     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 void setShadowColor(String color){
   156         graphicsEnvironmentImpl.setShadowColor(color);
   157     }
   158 
   159     public void setShadowBlur(double blur){
   160         graphicsEnvironmentImpl.setShadowBlur(blur);
   161     }
   162 
   163     public void setShadowOffsetX(double x){
   164         graphicsEnvironmentImpl.setShadowOffsetX(x);
   165     }
   166 
   167     public void setShadowOffsetY(double y){
   168         graphicsEnvironmentImpl.setShadowOffsetY(y);
   169     }
   170 
   171     public String getShadowColor(){
   172         return graphicsEnvironmentImpl.getShadowColor();
   173     }
   174 
   175     public double getShadowBlur(){
   176         return graphicsEnvironmentImpl.getShadowBlur();
   177         }
   178 
   179     public double getShadowOffsetX(){
   180         return graphicsEnvironmentImpl.getShadowOffsetX();
   181     }
   182 
   183     public double getShadowOffsetY(){
   184         return graphicsEnvironmentImpl.getShadowOffsetY();
   185     }
   186 
   187     public String getLineCap(){
   188         return graphicsEnvironmentImpl.getLineCap();
   189     }
   190 
   191     public void setLineCap(String style){
   192         graphicsEnvironmentImpl.setLineCap(style);
   193     }
   194 
   195     public String getLineJoin(){
   196         return graphicsEnvironmentImpl.getLineJoin();
   197     }
   198 
   199     public void setLineJoin(String style){
   200         graphicsEnvironmentImpl.setLineJoin(style);
   201     }
   202 
   203     public double getLineWidth(){
   204         return graphicsEnvironmentImpl.getLineWidth();
   205     }
   206 
   207     public void setLineWidth(double width){
   208         graphicsEnvironmentImpl.setLineWidth(width);
   209     }
   210 
   211     public double getMiterLimit(){
   212         return graphicsEnvironmentImpl.getMiterLimit();
   213     }
   214 
   215     public void setMiterLimit(double limit){
   216         graphicsEnvironmentImpl.setMiterLimit(limit);
   217     }
   218     
   219     public void setFillStyle(Style style){
   220         Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached());
   221         style.cache(nativeFillStyle);
   222     }
   223 
   224     public String getFont(){
   225         return graphicsEnvironmentImpl.getFont();
   226     }
   227 
   228     public void setFont(String font){
   229         graphicsEnvironmentImpl.setFont(font);
   230     }
   231     
   232     public void setStrokeStyle(Style style){
   233         Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached());
   234         style.cache(nativeStrokeStyle);
   235     }
   236 
   237     public String getTextAlign(){
   238         return graphicsEnvironmentImpl.getTextAlign();
   239     }
   240 
   241     public void setTextAlign(String textAlign){
   242         graphicsEnvironmentImpl.setTextAlign(textAlign);
   243     }
   244 
   245     public String getTextBaseline(){
   246         return graphicsEnvironmentImpl.getTextBaseline();
   247     }
   248 
   249     public void setTextBaseline(String textbaseline){
   250         graphicsEnvironmentImpl.setTextBaseline(textbaseline);
   251     }
   252 
   253     public void fillText(String text, double x, double y){
   254         graphicsEnvironmentImpl.fillText(text, x, y);
   255     }
   256 
   257     public void fillText(String text, double x, double y, double maxWidth){
   258         graphicsEnvironmentImpl.fillText(text, x, y, maxWidth);
   259     }
   260 
   261     public Dimension measureText(String text){
   262         return graphicsEnvironmentImpl.measureText(text);
   263     }
   264 
   265     public void strokeText(String text, double x, double y){
   266         graphicsEnvironmentImpl.strokeText(text, x, y);
   267     }
   268 
   269     public void strokeText(String text, double x, double y, double maxWidth){
   270         graphicsEnvironmentImpl.strokeText(text, x, y, maxWidth);
   271     }
   272 
   273     public ImageData createPixelMap(double x, double y){
   274         return graphicsEnvironmentImpl.createPixelMap(x, y);
   275     }
   276 
   277     public ImageData createPixelMap(ImageData pixelMap){
   278         return graphicsEnvironmentImpl.createPixelMap(pixelMap);
   279     }
   280 
   281     public ImageData getSnapshot(double x, double y, double width, double height){
   282         return graphicsEnvironmentImpl.getPixelMap(x, y, width, height);
   283     }
   284 
   285     public void drawPixelMap(ImageData pixelMap, double x, double y){
   286         graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y);
   287     }
   288 
   289     public void drawPixelMap(ImageData pixelMap, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight){
   290         graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
   291     }
   292 
   293     public void setGlobalAlpha(double alpha){
   294         graphicsEnvironmentImpl.setGlobalAlpha(alpha);
   295     }
   296 
   297     public double getGlobalAlpha(){
   298         return graphicsEnvironmentImpl.getGlobalAlpha();
   299     }
   300 
   301     public void setGlobalCompositeOperation(String operation){
   302         graphicsEnvironmentImpl.setGlobalCompositeOperation(operation);
   303     }
   304 
   305     public String getGlobalCompositeOperation(){
   306         return graphicsEnvironmentImpl.getGlobalCompositeOperation();
   307     }
   308 
   309     public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1){
   310         return new Style.LinearGradient(x0, y0, x1, y1);
   311     }
   312 
   313     public Pattern createPattern(Image image, String repeat){
   314         return new Pattern(image, repeat);
   315     }
   316 
   317     public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1){
   318         return new RadialGradient(x0, y0, r0, x1, y1, r1);
   319     }
   320     
   321     public Color getWebColor(String webColor){
   322         return new Style.Color(webColor);
   323     }
   324 
   325     public Image getImageForPath(String path){
   326         return new net.java.html.canvas.Image(path);
   327     }
   328 
   329     public int getHeight(){
   330         return graphicsEnvironmentImpl.getHeight();
   331     }
   332 
   333     public int getWidth(){
   334         return graphicsEnvironmentImpl.getWidth();
   335     }
   336 
   337     public void setHeight(int height){
   338         graphicsEnvironmentImpl.setHeight(height);
   339     }
   340 
   341     public void setWidth(int width){
   342         graphicsEnvironmentImpl.setWidth(width);
   343     }
   344 }