javaquery/canvas/src/main/java/net/java/html/canvas/GraphicsContext.java
author Anton Epple <toni.epple@eppleton.de>
Wed, 12 Feb 2014 08:33:31 +0100
branchcanvas
changeset 1444 8cb57e6c7012
parent 1443 6e51eb226f44
child 1447 3e3fb431d2b7
permissions -rw-r--r--
More JavaDoc improvements.
toni@1111
     1
/**
toni@1443
     2
 * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
toni@1443
     3
 * <jaroslav.tulach@apidesign.org>
toni@1111
     4
 *
toni@1443
     5
 * This program is free software: you can redistribute it and/or modify it under
toni@1443
     6
 * the terms of the GNU General Public License as published by the Free Software
toni@1443
     7
 * Foundation, version 2 of the License.
toni@1111
     8
 *
toni@1443
     9
 * This program is distributed in the hope that it will be useful, but WITHOUT
toni@1443
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
toni@1443
    11
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
toni@1443
    12
 * details.
toni@1111
    13
 *
toni@1443
    14
 * You should have received a copy of the GNU General Public License along with
toni@1443
    15
 * this program. Look for COPYING file in the top folder. If not, see
toni@1443
    16
 * http://opensource.org/licenses/GPL-2.0.
toni@521
    17
 */
toni@1109
    18
package net.java.html.canvas;
toni@521
    19
toni@1150
    20
import net.java.html.canvas.Style.Color;
toni@1150
    21
import net.java.html.canvas.Style.LinearGradient;
toni@1150
    22
import net.java.html.canvas.Style.Pattern;
toni@1150
    23
import net.java.html.canvas.Style.RadialGradient;
toni@1136
    24
import net.java.html.canvas.spi.GraphicsEnvironment;
toni@1302
    25
import org.apidesign.html.canvas.impl.CnvsAccssr;
toni@521
    26
toni@521
    27
/**
toni@1302
    28
 * A 2D Graphics Context similar to HTML5 or JavaFX GraphicsContext. Use this to
toni@1443
    29
 * paint on your Canvas
toni@1302
    30
 *
toni@1109
    31
 * @author antonepple
toni@521
    32
 */
toni@1128
    33
public final class GraphicsContext {
toni@1128
    34
toni@1442
    35
    public static void getAccssr() {
toni@1444
    36
        // do nothing we need this in order to have the class loaded and static 
toni@1444
    37
        // block executed for CnvsAccssr.
toni@1442
    38
    }
toni@1442
    39
toni@1128
    40
    GraphicsEnvironment graphicsEnvironmentImpl;
toni@1128
    41
toni@1302
    42
    static {
toni@1302
    43
        CnvsAccssr cnvsAccssr = new CnvsAccssr() {
toni@1302
    44
            @Override
toni@1302
    45
            public GraphicsContext create(GraphicsEnvironment environment) {
toni@1302
    46
                return new GraphicsContext(environment);
toni@1302
    47
            }
toni@1302
    48
        };
toni@1302
    49
    }
toni@1302
    50
toni@1302
    51
    GraphicsContext(GraphicsEnvironment graphicsEnvironment) {
toni@1128
    52
        this.graphicsEnvironmentImpl = graphicsEnvironment;
toni@1128
    53
    }
toni@521
    54
toni@1302
    55
    /**
toni@1302
    56
     * Adds path elements to the current path to make an arc.
toni@1302
    57
     *
toni@1302
    58
     * @param centerX the center x position of the arc.
toni@1302
    59
     * @param centerY the center y position of the arc.
toni@1302
    60
     * @param radius the radius of the arc.
toni@1302
    61
     * @param endAngle teh endAngle of the arc
toni@1302
    62
     * @param ccw the direction of the arc (counterclockwise)
toni@1302
    63
     */
toni@1111
    64
    public void arc(double centerX,
toni@1111
    65
            double centerY,
toni@1111
    66
            double startAngle,
toni@1111
    67
            double radius,
toni@1111
    68
            double endAngle,
toni@1128
    69
            boolean ccw) {
toni@1128
    70
        graphicsEnvironmentImpl.arc(centerX, centerY, startAngle, radius, endAngle, ccw);
toni@1128
    71
    }
toni@521
    72
toni@1302
    73
    /**
toni@1302
    74
     * Adds segments to the current path to make an arc.
toni@1302
    75
     *
toni@1302
    76
     * @param x1 the X coordinate of the first point of the arc.
toni@1302
    77
     * @param y1 the Y coordinate of the first point of the arc.
toni@1302
    78
     * @param x2 the X coordinate of the second point of the arc.
toni@1302
    79
     * @param y2 the Y coordinate of the second point of the arc.
toni@1302
    80
     * @param radius the radius of the arc in the range {0.0-positive infinity}.
toni@1302
    81
     */
toni@1111
    82
    public void arcTo(double x1,
toni@1111
    83
            double y1,
toni@1111
    84
            double x2,
toni@1111
    85
            double y2,
toni@1302
    86
            double radius) {
toni@1302
    87
        graphicsEnvironmentImpl.arcTo(x1, y1, x2, y2, radius);
toni@1128
    88
    }
toni@521
    89
toni@1302
    90
    /**
toni@1302
    91
     * Returns true if the the given x,y point is inside the path.
toni@1302
    92
     *
toni@1302
    93
     * @param x the X coordinate to use for the check.
toni@1302
    94
     * @param y the Y coordinate to use for the check.
toni@1302
    95
     * @return true if the point given is inside the path, false otherwise.
toni@1302
    96
     */
toni@1128
    97
    public boolean isPointInPath(double x, double y) {
toni@1128
    98
        return graphicsEnvironmentImpl.isPointInPath(x, y);
toni@1128
    99
    }
toni@521
   100
toni@1302
   101
    /**
toni@1302
   102
     * Fills the path with the current fill paint.
toni@1302
   103
     */
toni@1128
   104
    public void fill() {
toni@1128
   105
        graphicsEnvironmentImpl.fill();
toni@1128
   106
    }
toni@521
   107
toni@1302
   108
    /**
toni@1302
   109
     * Strokes the path with the current stroke paint.
toni@1302
   110
     */
toni@1128
   111
    public void stroke() {
toni@1128
   112
        graphicsEnvironmentImpl.stroke();
toni@1128
   113
    }
toni@1109
   114
toni@1302
   115
    /**
toni@1302
   116
     * Starts a Path
toni@1302
   117
     */
toni@1128
   118
    public void beginPath() {
toni@1128
   119
        graphicsEnvironmentImpl.beginPath();
toni@1128
   120
    }
toni@1109
   121
toni@1302
   122
    /**
toni@1302
   123
     * Closes the path.
toni@1302
   124
     */
toni@1302
   125
    public void closePath() {
toni@1128
   126
        graphicsEnvironmentImpl.closePath();
toni@1128
   127
    }
toni@521
   128
toni@1302
   129
    /**
toni@1302
   130
     * Clips using the current path
toni@1302
   131
     */
toni@1302
   132
    public void clip() {
toni@1128
   133
        graphicsEnvironmentImpl.clip();
toni@1128
   134
    }
toni@1109
   135
toni@1302
   136
    /**
toni@1302
   137
     * Issues a move command for the current path to the given x,y coordinate.
toni@1302
   138
     *
toni@1302
   139
     * @param x0 the X position for the move to command.
toni@1302
   140
     * @param y0 the Y position for the move to command.
toni@1302
   141
     */
toni@1302
   142
    public void moveTo(double x, double y) {
toni@1128
   143
        graphicsEnvironmentImpl.moveTo(x, y);
toni@1128
   144
    }
toni@1109
   145
toni@1302
   146
    /**
toni@1302
   147
     * Adds segments to the current path to make a line at the given x,y
toni@1302
   148
     * coordinate.
toni@1302
   149
     *
toni@1302
   150
     * @param x1 the X coordinate of the ending point of the line.
toni@1302
   151
     * @param y1 the Y coordinate of the ending point of the line.
toni@1302
   152
     */
toni@1302
   153
    public void lineTo(double x, double y) {
toni@1128
   154
        graphicsEnvironmentImpl.lineTo(x, y);
toni@1128
   155
    }
toni@1109
   156
toni@1302
   157
    /**
toni@1302
   158
     * Adds segments to the current path to make a quadratic curve.
toni@1302
   159
     *
toni@1302
   160
     * @param cpx the X coordinate of the control point
toni@1302
   161
     * @param cpy the Y coordinate of the control point
toni@1302
   162
     * @param x the X coordinate of the end point
toni@1302
   163
     * @param y the Y coordinate of the end point
toni@1302
   164
     */
toni@1302
   165
    public void quadraticCurveTo(double cpx, double cpy, double x, double y) {
toni@1302
   166
        graphicsEnvironmentImpl.quadraticCurveTo(cpx, cpy, x, y);
toni@1128
   167
    }
toni@1109
   168
toni@1302
   169
    /**
toni@1302
   170
     * Adds segments to the current path to make a cubic bezier curve.
toni@1302
   171
     *
toni@1302
   172
     * @param cp1x the X coordinate of first bezier control point.
toni@1302
   173
     * @param cp1y the Y coordinate of the first bezier control point.
toni@1302
   174
     * @param cp2x the X coordinate of the second bezier control point.
toni@1302
   175
     * @param cp2y the Y coordinate of the second bezier control point.
toni@1302
   176
     * @param x the X coordinate of the end point.
toni@1302
   177
     * @param y the Y coordinate of the end point.
toni@1302
   178
     */
toni@1302
   179
    public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y) {
toni@1128
   180
        graphicsEnvironmentImpl.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
toni@1128
   181
    }
toni@1109
   182
toni@1302
   183
    /**
toni@1302
   184
     * Fills a rectangle using the current fill paint.
toni@1302
   185
     *
toni@1302
   186
     * @param x the X position of the upper left corner of the rectangle.
toni@1302
   187
     * @param y the Y position of the upper left corner of the rectangle.
toni@1302
   188
     * @param w the width of the rectangle.
toni@1302
   189
     * @param h the height of the rectangle.
toni@1302
   190
     */
toni@1302
   191
    public void fillRect(double x, double y, double width, double height) {
toni@1128
   192
        graphicsEnvironmentImpl.fillRect(x, y, width, height);
toni@1128
   193
    }
toni@1109
   194
toni@1302
   195
    /**
toni@1302
   196
     * Strokes a rectangle using the current stroke paint.
toni@1302
   197
     *
toni@1302
   198
     * @param x the X position of the upper left corner of the rectangle.
toni@1302
   199
     * @param y the Y position of the upper left corner of the rectangle.
toni@1302
   200
     * @param width the width of the rectangle.
toni@1302
   201
     * @param height the height of the rectangle.
toni@1302
   202
     */
toni@1302
   203
    public void strokeRect(double x, double y, double width, double height) {
toni@1302
   204
        graphicsEnvironmentImpl.strokeRect(x, y, width, height);
toni@1128
   205
    }
toni@1109
   206
toni@1302
   207
    /**
toni@1302
   208
     * Clears a portion of the canvas with a transparent color value.
toni@1302
   209
     *
toni@1302
   210
     * @param x X position of the upper left corner of the rectangle.
toni@1302
   211
     * @param y Y position of the upper left corner of the rectangle.
toni@1302
   212
     * @param width width of the rectangle.
toni@1302
   213
     * @param height height of the rectangle.
toni@1302
   214
     */
toni@1302
   215
    public void clearRect(double x, double y, double width, double height) {
toni@1128
   216
        graphicsEnvironmentImpl.clearRect(x, y, width, height);
toni@1128
   217
    }
toni@1109
   218
toni@1302
   219
    /**
toni@1302
   220
     * Clears a portion of the canvas with a transparent color value.
toni@1302
   221
     *
toni@1302
   222
     * @param x X position of the upper left corner of the rectangle.
toni@1302
   223
     * @param y Y position of the upper left corner of the rectangle.
toni@1302
   224
     * @param width width of the rectangle.
toni@1302
   225
     * @param height height of the rectangle.
toni@1302
   226
     */
toni@1302
   227
    public void rect(double x, double y, double width, double height) {
toni@1128
   228
        graphicsEnvironmentImpl.rect(x, y, width, height);
toni@1128
   229
    }
toni@1109
   230
toni@1302
   231
    /**
toni@1302
   232
     * Saves the following attributes onto a stack.
toni@1302
   233
     * <ul>
toni@1302
   234
     * <li>Global Alpha</li>
toni@1302
   235
     * <li>Global Blend Operation</li>
toni@1302
   236
     * <li>Transform</li>
toni@1302
   237
     * <li>Fill Paint</li>
toni@1302
   238
     * <li>Stroke Paint</li>
toni@1302
   239
     * <li>Line Width</li>
toni@1302
   240
     * <li>Line Cap</li>
toni@1302
   241
     * <li>Line Join</li>
toni@1302
   242
     * <li>Miter Limit</li>
toni@1302
   243
     * <li>Number of Clip Paths</li>
toni@1302
   244
     * <li>Font</li>
toni@1302
   245
     * <li>Text Align</li>
toni@1302
   246
     * <li>Text Baseline</li>
toni@1302
   247
     * <li>Effect</li>
toni@1302
   248
     * <li>Fill Rule</li>
toni@1302
   249
     * </ul>
toni@1302
   250
     * This method does NOT alter the current state in any way. Also, not that
toni@1302
   251
     * the current path is not saved.
toni@1302
   252
     */
toni@1302
   253
    public void save() {
toni@1128
   254
        graphicsEnvironmentImpl.save();
toni@1128
   255
    }
toni@1109
   256
toni@1302
   257
    /**
toni@1302
   258
     * Pops the state off of the stack, setting the following attributes to
toni@1302
   259
     * their value at the time when that state was pushed onto the stack. If the
toni@1302
   260
     * stack is empty then nothing is changed.
toni@1302
   261
     *
toni@1302
   262
     * <ul>
toni@1302
   263
     * <li>Global Alpha</li>
toni@1302
   264
     * <li>Global Blend Operation</li>
toni@1302
   265
     * <li>Transform</li>
toni@1302
   266
     * <li>Fill Paint</li>
toni@1302
   267
     * <li>Stroke Paint</li>
toni@1302
   268
     * <li>Line Width</li>
toni@1302
   269
     * <li>Line Cap</li>
toni@1302
   270
     * <li>Line Join</li>
toni@1302
   271
     * <li>Miter Limit</li>
toni@1302
   272
     * <li>Number of Clip Paths</li>
toni@1302
   273
     * <li>Font</li>
toni@1302
   274
     * <li>Text Align</li>
toni@1302
   275
     * <li>Text Baseline</li>
toni@1302
   276
     * <li>Effect</li>
toni@1302
   277
     * <li>Fill Rule</li>
toni@1302
   278
     * </ul>
toni@1302
   279
     */
toni@1302
   280
    public void restore() {
toni@1128
   281
        graphicsEnvironmentImpl.restore();
toni@1128
   282
    }
toni@1109
   283
toni@1302
   284
    /**
toni@1302
   285
     * Rotates the current transform in degrees.
toni@1302
   286
     *
toni@1302
   287
     * @param angle value in degrees to rotate the current transform.
toni@1302
   288
     */
toni@1302
   289
    public void rotate(double angle) {
toni@1128
   290
        graphicsEnvironmentImpl.rotate(angle);
toni@1128
   291
    }
toni@1109
   292
toni@1302
   293
    /**
toni@1302
   294
     * Concatenates the input with the current transform.
toni@1302
   295
     *
toni@1302
   296
     * @param mxx - the X coordinate scaling element of the 3x4 matrix
toni@1302
   297
     * @param myx - the Y coordinate shearing element of the 3x4 matrix
toni@1302
   298
     * @param mxy - the X coordinate shearing element of the 3x4 matrix
toni@1302
   299
     * @param myy - the Y coordinate scaling element of the 3x4 matrix
toni@1302
   300
     * @param mxt - the X coordinate translation element of the 3x4 matrix
toni@1302
   301
     * @param myt - the Y coordinate translation element of the 3x4 matrix
toni@1302
   302
     */
toni@1302
   303
    public void transform(double mxx, double myx, double mxy, double myy, double mxt, double myt) {
toni@1302
   304
        graphicsEnvironmentImpl.transform(mxx, myx, mxy, myy, mxt, myt);
toni@1128
   305
    }
toni@1109
   306
toni@1302
   307
    /**
toni@1302
   308
     * Concatenates the input with the current transform.
toni@1302
   309
     *
toni@1302
   310
     * @param mxx - the X coordinate scaling element of the 3x4 matrix
toni@1302
   311
     * @param myx - the Y coordinate shearing element of the 3x4 matrix
toni@1302
   312
     * @param mxy - the X coordinate shearing element of the 3x4 matrix
toni@1302
   313
     * @param myy - the Y coordinate scaling element of the 3x4 matrix
toni@1302
   314
     * @param mxt - the X coordinate translation element of the 3x4 matrix
toni@1302
   315
     * @param myt - the Y coordinate translation element of the 3x4 matrix
toni@1302
   316
     */
toni@1302
   317
    public void setTransform(double mxx, double myx, double mxy, double myy, double mxt, double myt) {
toni@1302
   318
        graphicsEnvironmentImpl.setTransform(mxx, myx, mxy, myy, mxt, myt);
toni@1128
   319
    }
toni@1109
   320
toni@1302
   321
    /**
toni@1302
   322
     * Translates the current transform by x, y.
toni@1302
   323
     *
toni@1302
   324
     * @param x value to translate along the x axis.
toni@1302
   325
     * @param y value to translate along the y axis.
toni@1302
   326
     */
toni@1302
   327
    public void translate(double x, double y) {
toni@1128
   328
        graphicsEnvironmentImpl.translate(x, y);
toni@1128
   329
    }
toni@1109
   330
toni@1302
   331
    /**
toni@1302
   332
     * Scales the current transform by x, y.
toni@1302
   333
     *
toni@1302
   334
     * @param x value to scale in the x axis.
toni@1302
   335
     * @param y value to scale in the y axis.
toni@1302
   336
     */
toni@1302
   337
    public void scale(double x, double y) {
toni@1128
   338
        graphicsEnvironmentImpl.scale(x, y);
toni@1128
   339
    }
toni@1109
   340
toni@1302
   341
    /**
toni@1302
   342
     * Draws an image at the given x, y position using the width and height of
toni@1302
   343
     * the given image.
toni@1302
   344
     *
toni@1302
   345
     * @param img the image to be drawn.
toni@1302
   346
     * @param x the X coordinate on the destination for the upper left of the
toni@1302
   347
     * image.
toni@1302
   348
     * @param y the Y coordinate on the destination for the upper left of the
toni@1302
   349
     * image.
toni@1302
   350
     */
toni@1302
   351
    public void drawImage(Image image, double x, double y) {
toni@1144
   352
        Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, image.getCached());
toni@1144
   353
        image.cache(nativeImage);
toni@1144
   354
    }
toni@1144
   355
toni@1302
   356
    /**
toni@1302
   357
     * Draws an image into the given destination rectangle of the canvas. The
toni@1302
   358
     * Image is scaled to fit into the destination rectagnle.
toni@1302
   359
     *
toni@1302
   360
     * @param img the image to be drawn.
toni@1302
   361
     * @param x the X coordinate on the destination for the upper left of the
toni@1302
   362
     * image.
toni@1302
   363
     * @param y the Y coordinate on the destination for the upper left of the
toni@1302
   364
     * image.
toni@1302
   365
     * @param width the width of the destination rectangle.
toni@1302
   366
     * @param height the height of the destination rectangle.
toni@1302
   367
     */
toni@1302
   368
    public void drawImage(Image image, double x, double y, double width, double height) {
toni@1144
   369
        Object nativeImage = graphicsEnvironmentImpl.drawImage(image, x, y, width, height, image.getCached());
toni@1144
   370
        image.cache(nativeImage);
toni@1144
   371
    }
toni@1144
   372
toni@1302
   373
    /**
toni@1302
   374
     * Draws the current source rectangle of the given image to the given
toni@1302
   375
     * destination rectangle of the Canvas.
toni@1302
   376
     *
toni@1302
   377
     * @param img the image to be drawn.
toni@1302
   378
     * @param sx the source rectangle's X coordinate position.
toni@1302
   379
     * @param sy the source rectangle's Y coordinate position.
toni@1302
   380
     * @param sw the source rectangle's width.
toni@1302
   381
     * @param sh the source rectangle's height.
toni@1302
   382
     * @param dx the destination rectangle's X coordinate position.
toni@1302
   383
     * @param dy the destination rectangle's Y coordinate position.
toni@1302
   384
     * @param dw the destination rectangle's width.
toni@1302
   385
     * @param dh the destination rectangle's height.
toni@1302
   386
     */
toni@1302
   387
    public void drawImage(Image image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh) {
toni@1302
   388
        Object nativeImage = graphicsEnvironmentImpl.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh, image.getCached());
toni@1144
   389
        image.cache(nativeImage);
toni@1144
   390
    }
toni@1302
   391
toni@1302
   392
    /**
toni@1302
   393
     * Merges two images drawing one on top of the other and returning the
toni@1302
   394
     * result.
toni@1302
   395
     *
toni@1302
   396
     * @param a the lower Image
toni@1302
   397
     * @param b the upper Image
toni@1302
   398
     * @return
toni@1302
   399
     */
toni@1302
   400
    public Image merge(Image a, Image b) {
toni@1302
   401
        if (a.getCached() == null) {
toni@1263
   402
            drawImage(a, 0, 0);
toni@1263
   403
        }
toni@1302
   404
        if (b.getCached() == null) {
toni@1263
   405
            drawImage(b, 0, 0);
toni@1263
   406
        }
toni@1302
   407
        Object nativeImage = graphicsEnvironmentImpl.mergeImages(a, b, a.getCached(), b.getCached());
toni@1263
   408
        Image merged = Image.create("should add real path here");
toni@1263
   409
        merged.cache(nativeImage);
toni@1263
   410
        return merged;
toni@1263
   411
    }
toni@1109
   412
toni@1302
   413
//    public void setShadowColor(String color) {
toni@1302
   414
//        graphicsEnvironmentImpl.setShadowColor(color);
toni@1302
   415
//    }
toni@1302
   416
//
toni@1302
   417
//    public void setShadowBlur(double blur) {
toni@1302
   418
//        graphicsEnvironmentImpl.setShadowBlur(blur);
toni@1302
   419
//    }
toni@1302
   420
//
toni@1302
   421
//    public void setShadowOffsetX(double x) {
toni@1302
   422
//        graphicsEnvironmentImpl.setShadowOffsetX(x);
toni@1302
   423
//    }
toni@1302
   424
//
toni@1302
   425
//    public void setShadowOffsetY(double y) {
toni@1302
   426
//        graphicsEnvironmentImpl.setShadowOffsetY(y);
toni@1302
   427
//    }
toni@1302
   428
//
toni@1302
   429
//    public String getShadowColor() {
toni@1302
   430
//        return graphicsEnvironmentImpl.getShadowColor();
toni@1302
   431
//    }
toni@1302
   432
//
toni@1302
   433
//    public double getShadowBlur() {
toni@1302
   434
//        return graphicsEnvironmentImpl.getShadowBlur();
toni@1302
   435
//    }
toni@1302
   436
//
toni@1302
   437
//    public double getShadowOffsetX() {
toni@1302
   438
//        return graphicsEnvironmentImpl.getShadowOffsetX();
toni@1302
   439
//    }
toni@1302
   440
//
toni@1302
   441
//    public double getShadowOffsetY() {
toni@1302
   442
//        return graphicsEnvironmentImpl.getShadowOffsetY();
toni@1302
   443
//    }
toni@1443
   444
    
toni@1444
   445
    /**
toni@1444
   446
     * Gets the current stroke line cap attribute. 
toni@1444
   447
     * 
toni@1444
   448
     * @return  a value of butt, round, or square.
toni@1444
   449
     */    
toni@1302
   450
    public String getLineCap() {
toni@1128
   451
        return graphicsEnvironmentImpl.getLineCap();
toni@1128
   452
    }
toni@1444
   453
    
toni@1444
   454
    /**
toni@1444
   455
     * Sets the current stroke line cap attribute.
toni@1444
   456
     * 
toni@1444
   457
     * @param style  a value of miter, bevel, or round.
toni@1444
   458
     */
toni@1302
   459
    public void setLineCap(String style) {
toni@1128
   460
        graphicsEnvironmentImpl.setLineCap(style);
toni@1128
   461
    }
toni@1109
   462
toni@1444
   463
    /**
toni@1444
   464
     * Gets the current stroke line join attribute.
toni@1444
   465
     *
toni@1444
   466
     * @return  a value of miter, bevel, or round.
toni@1444
   467
     */
toni@1302
   468
    public String getLineJoin() {
toni@1128
   469
        return graphicsEnvironmentImpl.getLineJoin();
toni@1128
   470
    }
toni@1109
   471
toni@1444
   472
    /**
toni@1444
   473
     * Sets the current stroke line join attribute.
toni@1444
   474
     *
toni@1444
   475
     * @param style  a value of miter, bevel, or
toni@1444
   476
     * round.
toni@1444
   477
     */
toni@1302
   478
    public void setLineJoin(String style) {
toni@1128
   479
        graphicsEnvironmentImpl.setLineJoin(style);
toni@1128
   480
    }
toni@1109
   481
toni@1444
   482
    /**
toni@1444
   483
     * Gets the current line width attribute.
toni@1444
   484
     *
toni@1444
   485
     * @return value between 0 and infinity, with any other value being ignored
toni@1444
   486
     * and leaving the value unchanged.
toni@1444
   487
     *
toni@1444
   488
     */
toni@1302
   489
    public double getLineWidth() {
toni@1128
   490
        return graphicsEnvironmentImpl.getLineWidth();
toni@1128
   491
    }
toni@1109
   492
toni@1444
   493
    /**
toni@1444
   494
     * Sets the current line width attribute.
toni@1444
   495
     *
toni@1444
   496
     * @param lw value between 0 and infinity, with any other value being
toni@1444
   497
     * ignored and leaving the value unchanged.
toni@1444
   498
     *
toni@1444
   499
     */
toni@1302
   500
    public void setLineWidth(double width) {
toni@1128
   501
        graphicsEnvironmentImpl.setLineWidth(width);
toni@1128
   502
    }
toni@1109
   503
toni@1444
   504
    /**
toni@1444
   505
     * Gets the current miter limit attribute.
toni@1444
   506
     *
toni@1444
   507
     * @return limit value between 0 and positive infinity with any other value
toni@1444
   508
     * being ignored and leaving the value unchanged.
toni@1444
   509
     */
toni@1302
   510
    public double getMiterLimit() {
toni@1128
   511
        return graphicsEnvironmentImpl.getMiterLimit();
toni@1128
   512
    }
toni@1109
   513
toni@1444
   514
    /**
toni@1444
   515
     * Sets the current miter limit attribute.
toni@1444
   516
     *
toni@1444
   517
     * @param ml miter limit value between 0 and positive infinity with any
toni@1444
   518
     * other value being ignored and leaving the value unchanged.
toni@1444
   519
     */
toni@1302
   520
    public void setMiterLimit(double limit) {
toni@1128
   521
        graphicsEnvironmentImpl.setMiterLimit(limit);
toni@1128
   522
    }
toni@1302
   523
toni@1443
   524
    /**
toni@1444
   525
     * Sets the fill style. Will be used when rendering something, e.g. calling
toni@1444
   526
     * one of the fillText Methods.
toni@1444
   527
     *
toni@1444
   528
     * @param style
toni@1443
   529
     */
toni@1302
   530
    public void setFillStyle(Style style) {
toni@1141
   531
        Object nativeFillStyle = graphicsEnvironmentImpl.setFillStyle(style, style.getCached());
toni@1141
   532
        style.cache(nativeFillStyle);
toni@1141
   533
    }
toni@1109
   534
toni@1443
   535
    /**
toni@1443
   536
     * get the current font
toni@1443
   537
     *
toni@1444
   538
     * @return current Font. of the fillText Methods.
toni@1443
   539
     */
toni@1302
   540
    public String getFont() {
toni@1128
   541
        return graphicsEnvironmentImpl.getFont();
toni@1128
   542
    }
toni@1109
   543
toni@1443
   544
    /**
toni@1444
   545
     * Set the Font. Will be used when rendering Text, e.g. by calling one of
toni@1444
   546
     * the fillText Methods.
toni@1443
   547
     *
toni@1443
   548
     * @param font
toni@1443
   549
     */
toni@1302
   550
    public void setFont(String font) {
toni@1128
   551
        graphicsEnvironmentImpl.setFont(font);
toni@1128
   552
    }
toni@1302
   553
toni@1443
   554
    /**
toni@1443
   555
     * sets the Style of the Stroke.
toni@1443
   556
     *
toni@1443
   557
     * @param style
toni@1443
   558
     */
toni@1302
   559
    public void setStrokeStyle(Style style) {
toni@1141
   560
        Object nativeStrokeStyle = graphicsEnvironmentImpl.setStrokeStyle(style, style.getCached());
toni@1141
   561
        style.cache(nativeStrokeStyle);
toni@1141
   562
    }
toni@1109
   563
toni@1443
   564
    /**
toni@1443
   565
     * Gets the current TextAlignment attribute
toni@1443
   566
     *
toni@1443
   567
     * @return TextAlignment with values of left, center, right, or justify.
toni@1443
   568
     */
toni@1302
   569
    public String getTextAlign() {
toni@1128
   570
        return graphicsEnvironmentImpl.getTextAlign();
toni@1128
   571
    }
toni@521
   572
toni@1443
   573
    /**
toni@1443
   574
     * Defines horizontal text alignment, relative to the text {@code x} origin.
toni@1443
   575
     * <p>
toni@1443
   576
     * Let horizontal bounds represent the logical width of a single line of
toni@1443
   577
     * text. Where each line of text has a separate horizontal bounds.
toni@1443
   578
     * <p>
toni@1443
   579
     * Then TextAlignment is specified as:
toni@1443
   580
     * <ul>
toni@1443
   581
     * <li>left: the left edge of the horizontal bounds will be at {@code x}.
toni@1443
   582
     * <li>center: the center, halfway between left and right edge, of the
toni@1443
   583
     * horizontal bounds will be at {@code x}.
toni@1443
   584
     * <li>right: the right edge of the horizontal bounds will be at {@code x}.
toni@1443
   585
     * </ul>
toni@1443
   586
     * <p>
toni@1443
   587
     *
toni@1443
   588
     * Note: Canvas does not support line wrapping, therefore the text alignment
toni@1443
   589
     * Justify is identical to left aligned text.
toni@1443
   590
     * <p>
toni@1443
   591
     *
toni@1443
   592
     * @param textAlign with values of left, center, right.
toni@1443
   593
     */
toni@1302
   594
    public void setTextAlign(String textAlign) {
toni@1128
   595
        graphicsEnvironmentImpl.setTextAlign(textAlign);
toni@1128
   596
    }
toni@521
   597
toni@1443
   598
    /**
toni@1443
   599
     * Gets the current Text Baseline attribute.
toni@1443
   600
     *
toni@1443
   601
     * @return baseline with values of top, center, baseline, or bottom
toni@1443
   602
     */
toni@1302
   603
    public String getTextBaseline() {
toni@1128
   604
        return graphicsEnvironmentImpl.getTextBaseline();
toni@1128
   605
    }
toni@521
   606
toni@1443
   607
    /**
toni@1443
   608
     * Sets the current Text Baseline attribute.
toni@1443
   609
     *
toni@1443
   610
     * @param baseline with values of top, center, baseline, or bottom
toni@1443
   611
     */
toni@1302
   612
    public void setTextBaseline(String textbaseline) {
toni@1128
   613
        graphicsEnvironmentImpl.setTextBaseline(textbaseline);
toni@1128
   614
    }
toni@521
   615
toni@1443
   616
    /**
toni@1443
   617
     * Renders the indicated String with current fill. default is black.
toni@1443
   618
     *
toni@1443
   619
     * @param text the text to stroke
toni@1443
   620
     * @param x x coordinate of start position
toni@1443
   621
     * @param y y coordinate of start position
toni@1443
   622
     */
toni@1302
   623
    public void fillText(String text, double x, double y) {
toni@1128
   624
        graphicsEnvironmentImpl.fillText(text, x, y);
toni@1128
   625
    }
toni@521
   626
toni@1443
   627
    /**
toni@1443
   628
     * Renders the indicated String with current fill. default is black.
toni@1443
   629
     *
toni@1443
   630
     * @param text the text to stroke
toni@1443
   631
     * @param x x coordinate of start position
toni@1443
   632
     * @param y y coordinate of start position
toni@1443
   633
     * @param maxWidth maximum width of text
toni@1443
   634
     */
toni@1302
   635
    public void fillText(String text, double x, double y, double maxWidth) {
toni@1128
   636
        graphicsEnvironmentImpl.fillText(text, x, y, maxWidth);
toni@1128
   637
    }
toni@521
   638
toni@1443
   639
    /**
toni@1443
   640
     * Check the length of a text before writing it to the Canvas. Takes into
toni@1443
   641
     * account the current Font.
toni@1443
   642
     *
toni@1443
   643
     * @param text the text to measure
toni@1443
   644
     * @return the length in pixels
toni@1443
   645
     */
toni@1302
   646
    public Dimension measureText(String text) {
toni@1128
   647
        return graphicsEnvironmentImpl.measureText(text);
toni@1128
   648
    }
toni@1111
   649
toni@1443
   650
    /**
toni@1443
   651
     * Renders the indicated String (with no fill)
toni@1443
   652
     *
toni@1443
   653
     * @param text the text to stroke
toni@1443
   654
     * @param x x coordinate of start position
toni@1443
   655
     * @param y y coordinate of start position
toni@1443
   656
     */
toni@1302
   657
    public void strokeText(String text, double x, double y) {
toni@1128
   658
        graphicsEnvironmentImpl.strokeText(text, x, y);
toni@1128
   659
    }
toni@1111
   660
toni@1443
   661
    /**
toni@1443
   662
     * Renders the indicated String (with no fill)
toni@1443
   663
     *
toni@1443
   664
     * @param text the text to stroke
toni@1443
   665
     * @param x x coordinate of start position
toni@1443
   666
     * @param y y coordinate of start position
toni@1443
   667
     * @param maxWidth maximum width of text
toni@1443
   668
     */
toni@1302
   669
    public void strokeText(String text, double x, double y, double maxWidth) {
toni@1128
   670
        graphicsEnvironmentImpl.strokeText(text, x, y, maxWidth);
toni@1128
   671
    }
toni@1111
   672
toni@1302
   673
//    public ImageData createPixelMap(double x, double y) {
toni@1302
   674
//        return graphicsEnvironmentImpl.createPixelMap(x, y);
toni@1302
   675
//    }
toni@1302
   676
//
toni@1302
   677
//    public ImageData createPixelMap(ImageData pixelMap) {
toni@1302
   678
//        return graphicsEnvironmentImpl.createPixelMap(pixelMap);
toni@1302
   679
//    }
toni@1302
   680
//
toni@1302
   681
//    public ImageData getSnapshot(double x, double y, double width, double height) {
toni@1302
   682
//        return graphicsEnvironmentImpl.getPixelMap(x, y, width, height);
toni@1302
   683
//    }
toni@1302
   684
//
toni@1302
   685
//    public void drawPixelMap(ImageData pixelMap, double x, double y) {
toni@1302
   686
//        graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y);
toni@1302
   687
//    }
toni@1302
   688
//
toni@1302
   689
//    public void drawPixelMap(ImageData pixelMap, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
toni@1302
   690
//        graphicsEnvironmentImpl.putPixelMap(pixelMap, x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
toni@1302
   691
//    }
toni@1302
   692
    /**
toni@1302
   693
     * Sets the global alpha of the current state.
toni@1302
   694
     *
toni@1302
   695
     * @param alpha value in the range {@code 0.0-1.0}. The value is clamped if
toni@1302
   696
     * it is out of range.
toni@1302
   697
     */
toni@1302
   698
    public void setGlobalAlpha(double alpha) {
toni@1128
   699
        graphicsEnvironmentImpl.setGlobalAlpha(alpha);
toni@1128
   700
    }
toni@1111
   701
toni@1302
   702
    /**
toni@1302
   703
     * Gets the current global alpha.
toni@1302
   704
     *
toni@1302
   705
     * @return the current global alpha.
toni@1302
   706
     */
toni@1302
   707
    public double getGlobalAlpha() {
toni@1128
   708
        return graphicsEnvironmentImpl.getGlobalAlpha();
toni@1128
   709
    }
toni@1111
   710
toni@1302
   711
    /**
toni@1302
   712
     * Sets the global blend mode.
toni@1302
   713
     *
toni@1302
   714
     * @param op the BlendMode that will be set.
toni@1302
   715
     */
toni@1302
   716
    public void setGlobalCompositeOperation(String operation) {
toni@1128
   717
        graphicsEnvironmentImpl.setGlobalCompositeOperation(operation);
toni@1128
   718
    }
toni@1111
   719
toni@1302
   720
    /**
toni@1302
   721
     * Gets the global blend mode.
toni@1302
   722
     *
toni@1302
   723
     * @return the global BlendMode of the current state.
toni@1302
   724
     */
toni@1302
   725
    public String getGlobalCompositeOperation() {
toni@1128
   726
        return graphicsEnvironmentImpl.getGlobalCompositeOperation();
toni@1128
   727
    }
toni@1111
   728
toni@1443
   729
    /**
toni@1443
   730
     * Create a LinearGradient to use in Canvas.
toni@1443
   731
     *
toni@1443
   732
     * @param x0 x coordinate of start point
toni@1443
   733
     * @param y0 y coordinate of start point
toni@1443
   734
     * @param x1 x coordinate of end point
toni@1443
   735
     * @param y1 y coordinate of end point
toni@1443
   736
     * @return the gradient
toni@1443
   737
     */
toni@1302
   738
    public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
toni@1150
   739
        return new Style.LinearGradient(x0, y0, x1, y1);
toni@1128
   740
    }
toni@1111
   741
toni@1443
   742
    /**
toni@1443
   743
     * Create an Image Pattern from a source Image and a repeat style. Possible
toni@1443
   744
     * Styles are repeat, repeat-x, repeat-y, or no-repeat. defaults to repeat
toni@1443
   745
     *
toni@1443
   746
     * @param image the Image
toni@1443
   747
     * @param repeat the repeat style
toni@1443
   748
     * @return the Pattern
toni@1443
   749
     */
toni@1302
   750
    public Pattern createPattern(Image image, String repeat) {
toni@1144
   751
        return new Pattern(image, repeat);
toni@1144
   752
    }
toni@1111
   753
toni@1443
   754
    /**
toni@1443
   755
     * Create a RadialGradient
toni@1443
   756
     *
toni@1443
   757
     * @param x0 x Coordinate of starting circle
toni@1443
   758
     * @param y0 y Coordinate of starting circle
toni@1443
   759
     * @param r0 radius of starting circle
toni@1443
   760
     * @param x1 x coordinate of ending circle
toni@1443
   761
     * @param y1 y coordinate of ending circle
toni@1443
   762
     * @param r1 radius of ending circle
toni@1443
   763
     * @return the Gradient
toni@1443
   764
     */
toni@1302
   765
    public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
toni@1131
   766
        return new RadialGradient(x0, y0, r0, x1, y1, r1);
toni@1128
   767
    }
toni@1302
   768
toni@1443
   769
    /**
toni@1443
   770
     * Convert this String Representation of a Color to a Color Object.
toni@1443
   771
     *
toni@1443
   772
     * @param webColor
toni@1443
   773
     * @return The Color represented by the input
toni@1443
   774
     */
toni@1302
   775
    public Color getWebColor(String webColor) {
toni@1150
   776
        return new Style.Color(webColor);
toni@1150
   777
    }
toni@1111
   778
toni@1302
   779
    /**
toni@1302
   780
     * Get the height of this GraphicsContext (which should be the same as the
toni@1302
   781
     * enclosing canvas height)
toni@1302
   782
     *
toni@1302
   783
     * @return the height of this GraphicsContext
toni@1302
   784
     */
toni@1302
   785
    public int getHeight() {
toni@1128
   786
        return graphicsEnvironmentImpl.getHeight();
toni@1128
   787
    }
toni@1111
   788
toni@1302
   789
    /**
toni@1302
   790
     * Get the width of this GraphicsContext (which should be the same as the
toni@1302
   791
     * enclosing canvas height)
toni@1302
   792
     *
toni@1302
   793
     * @return the width of this GraphicsContext
toni@1302
   794
     */
toni@1302
   795
    public int getWidth() {
toni@1128
   796
        return graphicsEnvironmentImpl.getWidth();
toni@1128
   797
    }
toni@1111
   798
toni@1302
   799
//    public void setHeight(int height) {
toni@1302
   800
//        graphicsEnvironmentImpl.setHeight(height);
toni@1302
   801
//    }
toni@1302
   802
//
toni@1302
   803
//    public void setWidth(int width) {
toni@1302
   804
//        graphicsEnvironmentImpl.setWidth(width);
toni@1302
   805
//    }
toni@1302
   806
    /**
toni@1302
   807
     * Fill a circle with a center position of centerX, centerY and the
toni@1302
   808
     * specified radius.
toni@1302
   809
     *
toni@1302
   810
     * @param centerX
toni@1302
   811
     * @param centerY
toni@1302
   812
     * @param radius
toni@1302
   813
     */
toni@1302
   814
    public void fillCircle(float centerX, float centerY, float radius) {
toni@1302
   815
        graphicsEnvironmentImpl.arc(centerX, centerY, radius, 0, Math.PI * 2, false);
toni@1128
   816
    }
toni@1128
   817
toni@1302
   818
    /**
toni@1302
   819
     * Fills a polygon with the given points using the currently set fill paint.
toni@1302
   820
     *
toni@1302
   821
     * @param x_coord array containing the x coordinates of the polygon's
toni@1302
   822
     * points.
toni@1302
   823
     * @param y_coord array containing the y coordinates of the polygon's
toni@1302
   824
     * points.
toni@1302
   825
     * @param vertexCount the number of points that make the polygon.
toni@1302
   826
     */
toni@1263
   827
    public void fillPolygon(double[] x_coord, double[] y_coord, int vertexCount) {
toni@1302
   828
        if (vertexCount >= 1 && x_coord != null && x_coord.length >= vertexCount && y_coord != null && y_coord.length >= vertexCount) {
toni@1302
   829
            graphicsEnvironmentImpl.beginPath();
toni@1302
   830
        }
toni@1263
   831
        graphicsEnvironmentImpl.moveTo(x_coord[0], y_coord[0]);
toni@1263
   832
        for (int i = 1; i < vertexCount; i++) {
toni@1263
   833
            graphicsEnvironmentImpl.lineTo(x_coord[i], y_coord[i]);
toni@1302
   834
toni@1263
   835
        }
toni@1263
   836
        graphicsEnvironmentImpl.closePath();
toni@1263
   837
        graphicsEnvironmentImpl.fill();
toni@1263
   838
        graphicsEnvironmentImpl.stroke();
toni@1263
   839
    }
toni@521
   840
}