javaquery/canvas/src/main/java/net/java/html/canvas/Style.java
author Anton Epple <toni.epple@eppleton.de>
Wed, 12 Feb 2014 13:12:44 +0100
branchcanvas
changeset 1448 ff7ac82effa3
parent 1446 619f507713a2
child 1449 6be5961e27ee
permissions -rw-r--r--
removed HashMap from method signatures (Y07)
toni@1129
     1
/**
toni@1446
     2
 * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
toni@1446
     3
 * <jaroslav.tulach@apidesign.org>
toni@1129
     4
 *
toni@1446
     5
 * This program is free software: you can redistribute it and/or modify it under
toni@1446
     6
 * the terms of the GNU General Public License as published by the Free Software
toni@1446
     7
 * Foundation, version 2 of the License.
toni@1129
     8
 *
toni@1446
     9
 * This program is distributed in the hope that it will be useful, but WITHOUT
toni@1446
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
toni@1446
    11
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
toni@1446
    12
 * details.
toni@1129
    13
 *
toni@1446
    14
 * You should have received a copy of the GNU General Public License along with
toni@1446
    15
 * this program. Look for COPYING file in the top folder. If not, see
toni@1446
    16
 * http://opensource.org/licenses/GPL-2.0.
toni@1129
    17
 */
toni@1129
    18
package net.java.html.canvas;
toni@1129
    19
toni@1150
    20
import java.util.HashMap;
toni@1448
    21
import java.util.Map;
toni@1150
    22
import java.util.Objects;
toni@1150
    23
toni@1129
    24
/**
toni@1154
    25
 * Style for Stroke and Fill of GraphicsContext. Styles are created using
toni@1154
    26
 * GraphicsContexts factory methods. If the Implementation supports it, native
toni@1154
    27
 * Styles will be cached for performance reasons. This happens the first time
toni@1154
    28
 * the Style is actually used.
toni@1129
    29
 *
toni@1129
    30
 * @author antonepple
toni@1129
    31
 */
toni@1132
    32
public class Style {
toni@1132
    33
toni@1154
    34
    Object cached;
toni@1141
    35
    private int cacheHash;
toni@1141
    36
toni@1132
    37
    Style() {
toni@1132
    38
    }
toni@1141
    39
toni@1141
    40
    void cache(Object toCache) {
toni@1141
    41
        cacheHash = hashCode();
toni@1141
    42
        this.cached = toCache;
toni@1141
    43
    }
toni@1141
    44
toni@1141
    45
    private boolean isCached() {
toni@1141
    46
        return cacheHash == hashCode();
toni@1141
    47
    }
toni@1141
    48
toni@1141
    49
    Object getCached() {
toni@1141
    50
        return isCached() ? cached : null;
toni@1141
    51
    }
toni@1150
    52
toni@1308
    53
    /**
toni@1446
    54
     * A Fill Pattern using an Image Resource to create a fill style supporting
toni@1446
    55
     * different repeat styles repeat, repeat-x, repeat-y, or no-repeat. Default
toni@1446
    56
     * is repeat.
toni@1308
    57
     */
toni@1150
    58
    public static final class Pattern extends Style {
toni@1150
    59
toni@1308
    60
        private final Image imageResource;
toni@1308
    61
        private final String repeat;
toni@1150
    62
toni@1308
    63
        /**
toni@1446
    64
         *
toni@1308
    65
         * @param imageResource the base image of thsi pattern
toni@1446
    66
         * @param repeat the repeat pattern, possible values are repeat,
toni@1446
    67
         * repeat-x, repeat-y, or no-repeat.
toni@1308
    68
         */
toni@1270
    69
        public Pattern(Image imageResource, String repeat) {
toni@1150
    70
            this.imageResource = imageResource;
toni@1150
    71
            this.repeat = repeat;
toni@1150
    72
        }
toni@1150
    73
toni@1308
    74
        /**
toni@1308
    75
         * Get the base image of this pattern
toni@1446
    76
         *
toni@1308
    77
         * @return the base image of this pattern
toni@1308
    78
         */
toni@1150
    79
        public Image getImageResource() {
toni@1150
    80
            return imageResource;
toni@1150
    81
        }
toni@1150
    82
toni@1308
    83
        /**
toni@1308
    84
         * Get the repeat style for this pattern
toni@1446
    85
         *
toni@1308
    86
         * @return return the repeat style
toni@1308
    87
         */
toni@1150
    88
        public String getRepeat() {
toni@1150
    89
            return repeat;
toni@1150
    90
        }
toni@1150
    91
toni@1150
    92
    }
toni@1150
    93
toni@1446
    94
    /**
toni@1446
    95
     * An RGB color
toni@1446
    96
     */
toni@1150
    97
    public static final class Color extends Style {
toni@1150
    98
toni@1150
    99
        private String web;
toni@1150
   100
toni@1150
   101
        /**
toni@1150
   102
         * Creates an RGB color specified with an HTML or CSS attribute string.
toni@1150
   103
         *
toni@1308
   104
         * @param webColor Color defined as web color (e.g. #ff0000)
toni@1150
   105
         */
toni@1265
   106
        public Color(String webColor) {
toni@1157
   107
            this.web = webColor;
toni@1150
   108
        }
toni@1150
   109
toni@1308
   110
        /**
toni@1446
   111
         *
toni@1446
   112
         *
toni@1308
   113
         * @return the Color value as a Web Color (e.g. #ff0000)
toni@1308
   114
         */
toni@1150
   115
        public String getAsString() {
toni@1150
   116
            return web;
toni@1150
   117
        }
toni@1150
   118
    }
toni@1150
   119
toni@1308
   120
    /**
toni@1446
   121
     * A Linear Gradient. The Gradient has a direction defined by two
toni@1446
   122
     * coordinates and stops defining the Color at a specific position.
toni@1308
   123
     */
toni@1150
   124
    public static class LinearGradient extends Style {
toni@1150
   125
toni@1448
   126
        private Map<Double, String> stops;
toni@1150
   127
        private double x0, y0, x1, y1;
toni@1150
   128
toni@1308
   129
        /**
toni@1446
   130
         *
toni@1308
   131
         * @param x0 the x coordinate of the start point for this gradient
toni@1308
   132
         * @param y0 the y coordinate of the start point for this gradient
toni@1308
   133
         * @param x1 the x coordinate of the end point for this gradient
toni@1308
   134
         * @param y1 the y coordinate of the end point for this gradient
toni@1308
   135
         */
toni@1150
   136
        LinearGradient(double x0, double y0, double x1, double y1) {
toni@1150
   137
            this.x0 = x0;
toni@1150
   138
            this.y0 = y0;
toni@1150
   139
            this.x1 = x1;
toni@1150
   140
            this.y1 = y1;
toni@1150
   141
        }
toni@1150
   142
toni@1308
   143
        /**
toni@1446
   144
         * Add a new Color stop. A color stop defines a fixed color at a
toni@1446
   145
         * position along the coordinates.
toni@1446
   146
         *
toni@1308
   147
         * @param position the position of this stop in percent [0.0-1.0]
toni@1446
   148
         * @param color A Color defined in web format (e.g. #ff0000)
toni@1308
   149
         */
toni@1150
   150
        void addColorStop(double position, String color) {
toni@1150
   151
            if (stops == null) {
toni@1150
   152
                stops = new HashMap<>();
toni@1150
   153
            }
toni@1150
   154
            stops.put(position, color);
toni@1150
   155
        }
toni@1150
   156
toni@1308
   157
        /**
toni@1446
   158
         * Get the stops of this gradient.
toni@1446
   159
         *
toni@1308
   160
         * @return the stops of this gradient
toni@1308
   161
         */
toni@1448
   162
        public Map<Double, String> getStops() {
toni@1308
   163
            return new HashMap<>(stops);
toni@1308
   164
        }
toni@1446
   165
toni@1308
   166
        /**
toni@1446
   167
         * Set the stops as Position/Color pairs
toni@1446
   168
         *
toni@1308
   169
         * @param stops the stops for thsi Gradient
toni@1308
   170
         */
toni@1448
   171
        public void setStops(Map<Double, String> stops) {
toni@1308
   172
            this.stops = new HashMap<>(stops);
toni@1150
   173
        }
toni@1150
   174
toni@1446
   175
        /**
toni@1446
   176
         * Get the X coordinate of the Gradients start point
toni@1446
   177
         *
toni@1446
   178
         * @return x coordinate
toni@1446
   179
         */
toni@1150
   180
        public double getX0() {
toni@1150
   181
            return x0;
toni@1150
   182
        }
toni@1150
   183
toni@1446
   184
        /**
toni@1446
   185
         * Set the X coordinate of the Gradients start point
toni@1446
   186
         *
toni@1446
   187
         * @param x0 x coordinate
toni@1446
   188
         */
toni@1150
   189
        public void setX0(double x0) {
toni@1150
   190
            this.x0 = x0;
toni@1150
   191
        }
toni@1150
   192
toni@1446
   193
        /**
toni@1446
   194
         * Get the Y coordinate of the Gradients start point
toni@1446
   195
         *
toni@1446
   196
         * @return y coordinate
toni@1446
   197
         */
toni@1150
   198
        public double getY0() {
toni@1150
   199
            return y0;
toni@1150
   200
        }
toni@1150
   201
toni@1446
   202
        /**
toni@1446
   203
         * Set the Y coordinate of the Gradients start point
toni@1446
   204
         *
toni@1446
   205
         * @param y0 y coordinate
toni@1446
   206
         */
toni@1150
   207
        public void setY0(double y0) {
toni@1150
   208
            this.y0 = y0;
toni@1150
   209
        }
toni@1150
   210
toni@1446
   211
        /**
toni@1446
   212
         * Set the X coordinate of the Gradients end point
toni@1446
   213
         *
toni@1446
   214
         * @return x coordinate
toni@1446
   215
         */
toni@1150
   216
        public double getX1() {
toni@1150
   217
            return x1;
toni@1150
   218
        }
toni@1150
   219
toni@1446
   220
        /**
toni@1446
   221
         * Set the X coordinate of the Gradients end point
toni@1446
   222
         *
toni@1446
   223
         * @param X coordinate
toni@1446
   224
         */
toni@1150
   225
        public void setX1(double x1) {
toni@1150
   226
            this.x1 = x1;
toni@1150
   227
        }
toni@1150
   228
toni@1446
   229
        /**
toni@1446
   230
         * Get the Y coordinate of the Gradients end point
toni@1446
   231
         *
toni@1446
   232
         * @return y coordinate
toni@1446
   233
         */
toni@1150
   234
        public double getY1() {
toni@1150
   235
            return y1;
toni@1150
   236
        }
toni@1150
   237
toni@1446
   238
        /**
toni@1446
   239
         * Set the Y coordinate of the Gradients end point
toni@1446
   240
         *
toni@1446
   241
         * @param y1 coordinate
toni@1446
   242
         */
toni@1150
   243
        public void setY1(double y1) {
toni@1150
   244
            this.y1 = y1;
toni@1150
   245
        }
toni@1150
   246
toni@1150
   247
        @Override
toni@1150
   248
        public int hashCode() {
toni@1150
   249
            int hash = 7;
toni@1150
   250
            hash = 29 * hash + Objects.hashCode(this.stops);
toni@1150
   251
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
toni@1150
   252
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
toni@1150
   253
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
toni@1150
   254
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
toni@1150
   255
            return hash;
toni@1150
   256
        }
toni@1150
   257
toni@1150
   258
        @Override
toni@1150
   259
        public boolean equals(Object obj) {
toni@1150
   260
            if (obj == null) {
toni@1150
   261
                return false;
toni@1150
   262
            }
toni@1150
   263
            if (getClass() != obj.getClass()) {
toni@1150
   264
                return false;
toni@1150
   265
            }
toni@1150
   266
            final LinearGradient other = (LinearGradient) obj;
toni@1150
   267
            if (!Objects.equals(this.stops, other.stops)) {
toni@1150
   268
                return false;
toni@1150
   269
            }
toni@1150
   270
            if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
toni@1150
   271
                return false;
toni@1150
   272
            }
toni@1150
   273
            if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
toni@1150
   274
                return false;
toni@1150
   275
            }
toni@1150
   276
            if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
toni@1150
   277
                return false;
toni@1150
   278
            }
toni@1150
   279
            if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
toni@1150
   280
                return false;
toni@1150
   281
            }
toni@1150
   282
            return true;
toni@1150
   283
        }
toni@1150
   284
    }
toni@1150
   285
toni@1446
   286
    /**
toni@1446
   287
     * A Radial Gradient. Radial gradients are defined with two imaginary
toni@1446
   288
     * circles, a starting circle and an ending circle. The gradient starts with
toni@1446
   289
     * the start circle and moves towards the end circle.
toni@1446
   290
     */
toni@1150
   291
    public static final class RadialGradient extends LinearGradient {
toni@1150
   292
toni@1150
   293
        private double r0, r1;
toni@1150
   294
toni@1446
   295
        /**
toni@1446
   296
         * Create a new RadialGradient
toni@1446
   297
         *
toni@1446
   298
         * @param x0 x Coordinate of starting circle
toni@1446
   299
         * @param y0 y Coordinate of starting circle
toni@1446
   300
         * @param r0 radius of starting circle
toni@1446
   301
         * @param x1 x coordinate of ending circle
toni@1446
   302
         * @param y1 y coordinate of ending circle
toni@1446
   303
         * @param r1 radius of ending circle
toni@1446
   304
         */
toni@1150
   305
        RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
toni@1150
   306
            super(x0, y0, x1, y1);
toni@1150
   307
            this.r0 = r0;
toni@1150
   308
            this.r1 = r1;
toni@1150
   309
        }
toni@1150
   310
toni@1446
   311
        /**
toni@1446
   312
         * get the radius of the start circle.
toni@1446
   313
         * @return the radius
toni@1446
   314
         */
toni@1150
   315
        public double getR0() {
toni@1150
   316
            return r0;
toni@1150
   317
        }
toni@1150
   318
toni@1446
   319
        /**
toni@1446
   320
         * set the radius of the start circle.
toni@1446
   321
         * @param r0 the radius
toni@1446
   322
         */
toni@1150
   323
        public void setR0(double r0) {
toni@1150
   324
            this.r0 = r0;
toni@1150
   325
        }
toni@1150
   326
toni@1446
   327
        /**
toni@1446
   328
         * get the radius of the end circle
toni@1446
   329
         * @return the radius
toni@1446
   330
         */
toni@1150
   331
        public double getR1() {
toni@1150
   332
            return r1;
toni@1150
   333
        }
toni@1150
   334
toni@1446
   335
        /**
toni@1446
   336
         * set the radius of the end circle.
toni@1446
   337
         * @param r1 the radius.
toni@1446
   338
         */
toni@1150
   339
        public void setR1(double r1) {
toni@1150
   340
            this.r1 = r1;
toni@1150
   341
        }
toni@1150
   342
toni@1150
   343
        @Override
toni@1150
   344
        public int hashCode() {
toni@1150
   345
            int hash = super.hashCode();
toni@1150
   346
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
toni@1150
   347
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
toni@1150
   348
toni@1150
   349
            return hash;
toni@1150
   350
        }
toni@1150
   351
toni@1150
   352
        @Override
toni@1150
   353
        public boolean equals(Object obj) {
toni@1150
   354
            if (obj == null) {
toni@1150
   355
                return false;
toni@1150
   356
            }
toni@1150
   357
            if (getClass() != obj.getClass()) {
toni@1150
   358
                return false;
toni@1150
   359
            }
toni@1150
   360
            if (!super.equals(obj)) {
toni@1150
   361
                return false;
toni@1150
   362
            }
toni@1150
   363
            final RadialGradient other = (RadialGradient) obj;
toni@1150
   364
            if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
toni@1150
   365
                return false;
toni@1150
   366
            }
toni@1150
   367
            if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
toni@1150
   368
                return false;
toni@1150
   369
            }
toni@1154
   370
            if ((this.getCached() == null) != (other.getCached() == null)) {
toni@1154
   371
                return false;
toni@1154
   372
            }
toni@1150
   373
            return true;
toni@1150
   374
        }
toni@1150
   375
    }
toni@1129
   376
}