javaquery/canvas/src/main/java/net/java/html/canvas/Style.java
author Anton Epple <toni.epple@eppleton.de>
Wed, 12 Feb 2014 14:19:10 +0100
branchcanvas
changeset 1449 6be5961e27ee
parent 1448 ff7ac82effa3
permissions -rw-r--r--
changed LinearGradient and RadialGradient inheritance (Y08)
removed setters from Style subclasses (Y06)
renamed method getAccssr to what it does: init (Y02)
created factory method for Gradients (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@1449
   120
    private static class Gradient extends Style {
toni@1150
   121
toni@1449
   122
        protected final Map<Double, String> stops;
toni@1449
   123
        protected final double x0, y0, x1, y1;
toni@1150
   124
toni@1308
   125
        /**
toni@1446
   126
         *
toni@1308
   127
         * @param x0 the x coordinate of the start point for this gradient
toni@1308
   128
         * @param y0 the y coordinate of the start point for this gradient
toni@1308
   129
         * @param x1 the x coordinate of the end point for this gradient
toni@1308
   130
         * @param y1 the y coordinate of the end point for this gradient
toni@1449
   131
         * @param stops  the stops of this gradient
toni@1308
   132
         */
toni@1449
   133
        private Gradient(double x0, double y0, double x1, double y1,Map<Double, String> stops) {
toni@1150
   134
            this.x0 = x0;
toni@1150
   135
            this.y0 = y0;
toni@1150
   136
            this.x1 = x1;
toni@1150
   137
            this.y1 = y1;
toni@1308
   138
            this.stops = new HashMap<>(stops);
toni@1150
   139
        }
toni@1150
   140
toni@1446
   141
        /**
toni@1446
   142
         * Get the X coordinate of the Gradients start point
toni@1446
   143
         *
toni@1446
   144
         * @return x coordinate
toni@1446
   145
         */
toni@1150
   146
        public double getX0() {
toni@1150
   147
            return x0;
toni@1150
   148
        }
toni@1150
   149
toni@1446
   150
        /**
toni@1446
   151
         * Set the X coordinate of the Gradients start point
toni@1446
   152
         *
toni@1449
   153
         * @param x0 x coordinate public void setX0(double x0) { this.x0 = x0; }
toni@1446
   154
         */
toni@1446
   155
        /**
toni@1446
   156
         * Get the Y coordinate of the Gradients start point
toni@1446
   157
         *
toni@1446
   158
         * @return y coordinate
toni@1446
   159
         */
toni@1150
   160
        public double getY0() {
toni@1150
   161
            return y0;
toni@1150
   162
        }
toni@1150
   163
toni@1446
   164
        /**
toni@1446
   165
         * Set the Y coordinate of the Gradients start point
toni@1446
   166
         *
toni@1449
   167
         * @param y0 y coordinate public void setY0(double y0) { this.y0 = y0; }
toni@1446
   168
         */
toni@1446
   169
        /**
toni@1446
   170
         * Set the X coordinate of the Gradients end point
toni@1446
   171
         *
toni@1446
   172
         * @return x coordinate
toni@1446
   173
         */
toni@1150
   174
        public double getX1() {
toni@1150
   175
            return x1;
toni@1150
   176
        }
toni@1150
   177
toni@1446
   178
        /**
toni@1446
   179
         * Set the X coordinate of the Gradients end point
toni@1446
   180
         *
toni@1449
   181
         * @param X coordinate public void setX1(double x1) { this.x1 = x1; }
toni@1446
   182
         */
toni@1446
   183
        /**
toni@1446
   184
         * Get the Y coordinate of the Gradients end point
toni@1446
   185
         *
toni@1446
   186
         * @return y coordinate
toni@1446
   187
         */
toni@1150
   188
        public double getY1() {
toni@1150
   189
            return y1;
toni@1150
   190
        }
toni@1150
   191
toni@1449
   192
toni@1446
   193
        /**
toni@1449
   194
         * Get the stops of this gradient.
toni@1446
   195
         *
toni@1449
   196
         * @return the stops of this gradient
toni@1446
   197
         */
toni@1449
   198
        public Map<Double, String> getStops() {
toni@1449
   199
            return new HashMap<>(stops);
toni@1150
   200
        }
toni@1150
   201
toni@1150
   202
        @Override
toni@1150
   203
        public int hashCode() {
toni@1150
   204
            int hash = 7;
toni@1150
   205
            hash = 29 * hash + Objects.hashCode(this.stops);
toni@1150
   206
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
toni@1150
   207
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
toni@1150
   208
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
toni@1150
   209
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
toni@1150
   210
            return hash;
toni@1150
   211
        }
toni@1150
   212
toni@1150
   213
        @Override
toni@1150
   214
        public boolean equals(Object obj) {
toni@1150
   215
            if (obj == null) {
toni@1150
   216
                return false;
toni@1150
   217
            }
toni@1150
   218
            if (getClass() != obj.getClass()) {
toni@1150
   219
                return false;
toni@1150
   220
            }
toni@1150
   221
            final LinearGradient other = (LinearGradient) obj;
toni@1150
   222
            if (!Objects.equals(this.stops, other.stops)) {
toni@1150
   223
                return false;
toni@1150
   224
            }
toni@1150
   225
            if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
toni@1150
   226
                return false;
toni@1150
   227
            }
toni@1150
   228
            if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
toni@1150
   229
                return false;
toni@1150
   230
            }
toni@1150
   231
            if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
toni@1150
   232
                return false;
toni@1150
   233
            }
toni@1150
   234
            if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
toni@1150
   235
                return false;
toni@1150
   236
            }
toni@1150
   237
            return true;
toni@1150
   238
        }
toni@1150
   239
    }
toni@1150
   240
toni@1446
   241
    /**
toni@1449
   242
     * A Linear Gradient. The Gradient has a direction defined by two
toni@1449
   243
     * coordinates and stops defining the Color at a specific position.
toni@1449
   244
     */
toni@1449
   245
    public static class LinearGradient extends Gradient {
toni@1449
   246
toni@1449
   247
       private LinearGradient(double x0, double y0, double x1, double y1,Map<Double, String> stops) {
toni@1449
   248
            super(x0, y0, x1, y1, stops);
toni@1449
   249
        }
toni@1449
   250
toni@1449
   251
        /**
toni@1449
   252
         *
toni@1449
   253
         * @param x0 the x coordinate of the start point for this gradient
toni@1449
   254
         * @param y0 the y coordinate of the start point for this gradient
toni@1449
   255
         * @param x1 the x coordinate of the end point for this gradient
toni@1449
   256
         * @param y1 the y coordinate of the end point for this gradient
toni@1449
   257
         * @param stops  the stops of this gradient
toni@1449
   258
         * @return linearGradient the gradient
toni@1449
   259
         */
toni@1449
   260
        public static LinearGradient create(double x0, double y0, double x1, double y1,Map<Double, String> stops) {
toni@1449
   261
            return new LinearGradient(x0, y0, x1, y1, stops);
toni@1449
   262
        }
toni@1449
   263
toni@1449
   264
    }
toni@1449
   265
toni@1449
   266
    /**
toni@1446
   267
     * A Radial Gradient. Radial gradients are defined with two imaginary
toni@1446
   268
     * circles, a starting circle and an ending circle. The gradient starts with
toni@1446
   269
     * the start circle and moves towards the end circle.
toni@1446
   270
     */
toni@1449
   271
    public static final class RadialGradient extends Gradient {
toni@1150
   272
toni@1449
   273
        final private double r0, r1;
toni@1150
   274
toni@1449
   275
toni@1449
   276
        private RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1, Map<Double, String> stops) {
toni@1449
   277
            super(x0, y0, x1, y1,stops);
toni@1449
   278
            this.r0 = r0;
toni@1449
   279
            this.r1 = r1;
toni@1449
   280
        }
toni@1449
   281
toni@1449
   282
         /**
toni@1446
   283
         * Create a new RadialGradient
toni@1446
   284
         *
toni@1446
   285
         * @param x0 x Coordinate of starting circle
toni@1446
   286
         * @param y0 y Coordinate of starting circle
toni@1446
   287
         * @param r0 radius of starting circle
toni@1446
   288
         * @param x1 x coordinate of ending circle
toni@1446
   289
         * @param y1 y coordinate of ending circle
toni@1446
   290
         * @param r1 radius of ending circle
toni@1449
   291
         * @param stops  the stops of this gradient
toni@1449
   292
         * @return radialGradient the gradient
toni@1446
   293
         */
toni@1449
   294
        public static RadialGradient create(double x0, double y0, double r0, double x1, double y1, double r1, Map<Double, String> stops){
toni@1449
   295
            return new RadialGradient(x0, y0, r0, x1, y1, r1, stops);
toni@1150
   296
        }
toni@1449
   297
        
toni@1446
   298
        /**
toni@1446
   299
         * get the radius of the start circle.
toni@1449
   300
         *
toni@1446
   301
         * @return the radius
toni@1446
   302
         */
toni@1150
   303
        public double getR0() {
toni@1150
   304
            return r0;
toni@1150
   305
        }
toni@1150
   306
toni@1446
   307
        /**
toni@1446
   308
         * set the radius of the start circle.
toni@1449
   309
         *
toni@1446
   310
         * @param r0 the radius
toni@1449
   311
         *
toni@1449
   312
         * public void setR0(double r0) { this.r0 = r0; }
toni@1446
   313
         */
toni@1446
   314
        /**
toni@1446
   315
         * get the radius of the end circle
toni@1449
   316
         *
toni@1446
   317
         * @return the radius
toni@1446
   318
         */
toni@1150
   319
        public double getR1() {
toni@1150
   320
            return r1;
toni@1150
   321
        }
toni@1150
   322
toni@1446
   323
        /**
toni@1446
   324
         * set the radius of the end circle.
toni@1449
   325
         *
toni@1446
   326
         * @param r1 the radius.
toni@1449
   327
         *
toni@1449
   328
         * public void setR1(double r1) { this.r1 = r1; }
toni@1446
   329
         */
toni@1150
   330
        @Override
toni@1150
   331
        public int hashCode() {
toni@1150
   332
            int hash = super.hashCode();
toni@1150
   333
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
toni@1150
   334
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
toni@1150
   335
toni@1150
   336
            return hash;
toni@1150
   337
        }
toni@1150
   338
toni@1150
   339
        @Override
toni@1150
   340
        public boolean equals(Object obj) {
toni@1150
   341
            if (obj == null) {
toni@1150
   342
                return false;
toni@1150
   343
            }
toni@1150
   344
            if (getClass() != obj.getClass()) {
toni@1150
   345
                return false;
toni@1150
   346
            }
toni@1150
   347
            if (!super.equals(obj)) {
toni@1150
   348
                return false;
toni@1150
   349
            }
toni@1150
   350
            final RadialGradient other = (RadialGradient) obj;
toni@1150
   351
            if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
toni@1150
   352
                return false;
toni@1150
   353
            }
toni@1150
   354
            if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
toni@1150
   355
                return false;
toni@1150
   356
            }
toni@1154
   357
            if ((this.getCached() == null) != (other.getCached() == null)) {
toni@1154
   358
                return false;
toni@1154
   359
            }
toni@1150
   360
            return true;
toni@1150
   361
        }
toni@1150
   362
    }
toni@1449
   363
toni@1129
   364
}