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