javaquery/canvas/src/main/java/net/java/html/canvas/Style.java
author Anton Epple <toni.epple@eppleton.de>
Sat, 07 Sep 2013 18:41:21 +0200
branchcanvas
changeset 1265 75a0866c27c7
parent 1157 c312f00cbace
child 1270 6b553ee385df
permissions -rw-r--r--
public constructor for color
toni@1129
     1
/**
toni@1129
     2
 * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
toni@1129
     3
 * <jaroslav.tulach@apidesign.org>
toni@1129
     4
 *
toni@1129
     5
 * This program is free software: you can redistribute it and/or modify it under
toni@1129
     6
 * the terms of the GNU General Public License as published by the Free Software
toni@1129
     7
 * Foundation, version 2 of the License.
toni@1129
     8
 *
toni@1129
     9
 * This program is distributed in the hope that it will be useful, but WITHOUT
toni@1129
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
toni@1129
    11
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
toni@1129
    12
 * details.
toni@1129
    13
 *
toni@1129
    14
 * You should have received a copy of the GNU General Public License along with
toni@1129
    15
 * this program. Look for COPYING file in the top folder. If not, see
toni@1129
    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@1150
    21
import java.util.Objects;
toni@1150
    22
toni@1129
    23
/**
toni@1154
    24
 * Style for Stroke and Fill of GraphicsContext. Styles are created using
toni@1154
    25
 * GraphicsContexts factory methods. If the Implementation supports it, native
toni@1154
    26
 * Styles will be cached for performance reasons. This happens the first time
toni@1154
    27
 * the Style is actually used.
toni@1129
    28
 *
toni@1129
    29
 * @author antonepple
toni@1129
    30
 */
toni@1132
    31
public class Style {
toni@1132
    32
toni@1154
    33
    Object cached;
toni@1141
    34
    private int cacheHash;
toni@1141
    35
toni@1132
    36
    Style() {
toni@1132
    37
    }
toni@1141
    38
toni@1141
    39
    void cache(Object toCache) {
toni@1141
    40
        cacheHash = hashCode();
toni@1141
    41
        this.cached = toCache;
toni@1141
    42
    }
toni@1141
    43
toni@1141
    44
    private boolean isCached() {
toni@1141
    45
        return cacheHash == hashCode();
toni@1141
    46
    }
toni@1141
    47
toni@1141
    48
    Object getCached() {
toni@1141
    49
        return isCached() ? cached : null;
toni@1141
    50
    }
toni@1150
    51
toni@1150
    52
    public static final class Pattern extends Style {
toni@1150
    53
toni@1150
    54
        private Image imageResource;
toni@1150
    55
        private String repeat;
toni@1150
    56
toni@1150
    57
        Pattern(Image imageResource, String repeat) {
toni@1150
    58
            this.imageResource = imageResource;
toni@1150
    59
            this.repeat = repeat;
toni@1150
    60
        }
toni@1150
    61
toni@1150
    62
        public Image getImageResource() {
toni@1150
    63
            return imageResource;
toni@1150
    64
        }
toni@1150
    65
toni@1150
    66
        public void setImageResource(Image imageResource) {
toni@1150
    67
            this.imageResource = imageResource;
toni@1150
    68
        }
toni@1150
    69
toni@1150
    70
        public String getRepeat() {
toni@1150
    71
            return repeat;
toni@1150
    72
        }
toni@1150
    73
toni@1150
    74
        public void setRepeat(String repeat) {
toni@1150
    75
            this.repeat = repeat;
toni@1150
    76
        }
toni@1150
    77
    }
toni@1150
    78
toni@1150
    79
    public static final class Color extends Style {
toni@1150
    80
toni@1150
    81
        private String web;
toni@1150
    82
toni@1150
    83
        /**
toni@1150
    84
         * Creates an RGB color specified with an HTML or CSS attribute string.
toni@1150
    85
         *
toni@1150
    86
         * @param webColor Colordefined as
toni@1150
    87
         */
toni@1265
    88
        public Color(String webColor) {
toni@1157
    89
            this.web = webColor;
toni@1150
    90
        }
toni@1150
    91
toni@1150
    92
        public String getAsString() {
toni@1150
    93
            return web;
toni@1150
    94
        }
toni@1150
    95
    }
toni@1150
    96
toni@1150
    97
    public static class LinearGradient extends Style {
toni@1150
    98
toni@1150
    99
        private HashMap<Double, String> stops;
toni@1150
   100
        private double x0, y0, x1, y1;
toni@1150
   101
toni@1150
   102
        LinearGradient(double x0, double y0, double x1, double y1) {
toni@1150
   103
            this.x0 = x0;
toni@1150
   104
            this.y0 = y0;
toni@1150
   105
            this.x1 = x1;
toni@1150
   106
            this.y1 = y1;
toni@1150
   107
        }
toni@1150
   108
toni@1150
   109
        void addColorStop(double position, String color) {
toni@1150
   110
            if (stops == null) {
toni@1150
   111
                stops = new HashMap<>();
toni@1150
   112
            }
toni@1150
   113
            stops.put(position, color);
toni@1150
   114
        }
toni@1150
   115
toni@1150
   116
        public HashMap<Double, String> getStops() {
toni@1150
   117
            return stops;
toni@1150
   118
        }
toni@1150
   119
toni@1150
   120
        public void setStops(HashMap<Double, String> stops) {
toni@1150
   121
            this.stops = stops;
toni@1150
   122
        }
toni@1150
   123
toni@1150
   124
        public double getX0() {
toni@1150
   125
            return x0;
toni@1150
   126
        }
toni@1150
   127
toni@1150
   128
        public void setX0(double x0) {
toni@1150
   129
            this.x0 = x0;
toni@1150
   130
        }
toni@1150
   131
toni@1150
   132
        public double getY0() {
toni@1150
   133
            return y0;
toni@1150
   134
        }
toni@1150
   135
toni@1150
   136
        public void setY0(double y0) {
toni@1150
   137
            this.y0 = y0;
toni@1150
   138
        }
toni@1150
   139
toni@1150
   140
        public double getX1() {
toni@1150
   141
            return x1;
toni@1150
   142
        }
toni@1150
   143
toni@1150
   144
        public void setX1(double x1) {
toni@1150
   145
            this.x1 = x1;
toni@1150
   146
        }
toni@1150
   147
toni@1150
   148
        public double getY1() {
toni@1150
   149
            return y1;
toni@1150
   150
        }
toni@1150
   151
toni@1150
   152
        public void setY1(double y1) {
toni@1150
   153
            this.y1 = y1;
toni@1150
   154
        }
toni@1150
   155
toni@1150
   156
        @Override
toni@1150
   157
        public int hashCode() {
toni@1150
   158
            int hash = 7;
toni@1150
   159
            hash = 29 * hash + Objects.hashCode(this.stops);
toni@1150
   160
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
toni@1150
   161
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
toni@1150
   162
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
toni@1150
   163
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
toni@1150
   164
            return hash;
toni@1150
   165
        }
toni@1150
   166
toni@1150
   167
        @Override
toni@1150
   168
        public boolean equals(Object obj) {
toni@1150
   169
            if (obj == null) {
toni@1150
   170
                return false;
toni@1150
   171
            }
toni@1150
   172
            if (getClass() != obj.getClass()) {
toni@1150
   173
                return false;
toni@1150
   174
            }
toni@1150
   175
            final LinearGradient other = (LinearGradient) obj;
toni@1150
   176
            if (!Objects.equals(this.stops, other.stops)) {
toni@1150
   177
                return false;
toni@1150
   178
            }
toni@1150
   179
            if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
toni@1150
   180
                return false;
toni@1150
   181
            }
toni@1150
   182
            if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
toni@1150
   183
                return false;
toni@1150
   184
            }
toni@1150
   185
            if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
toni@1150
   186
                return false;
toni@1150
   187
            }
toni@1150
   188
            if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
toni@1150
   189
                return false;
toni@1150
   190
            }
toni@1150
   191
            return true;
toni@1150
   192
        }
toni@1150
   193
    }
toni@1150
   194
toni@1150
   195
    public static final class RadialGradient extends LinearGradient {
toni@1150
   196
toni@1150
   197
        private double r0, r1;
toni@1150
   198
toni@1150
   199
        RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
toni@1150
   200
            super(x0, y0, x1, y1);
toni@1150
   201
            this.r0 = r0;
toni@1150
   202
            this.r1 = r1;
toni@1150
   203
        }
toni@1150
   204
toni@1150
   205
        public double getR0() {
toni@1150
   206
            return r0;
toni@1150
   207
        }
toni@1150
   208
toni@1150
   209
        public void setR0(double r0) {
toni@1150
   210
            this.r0 = r0;
toni@1150
   211
        }
toni@1150
   212
toni@1150
   213
        public double getR1() {
toni@1150
   214
            return r1;
toni@1150
   215
        }
toni@1150
   216
toni@1150
   217
        public void setR1(double r1) {
toni@1150
   218
            this.r1 = r1;
toni@1150
   219
        }
toni@1150
   220
toni@1150
   221
        @Override
toni@1150
   222
        public int hashCode() {
toni@1150
   223
            int hash = super.hashCode();
toni@1150
   224
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
toni@1150
   225
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
toni@1150
   226
toni@1150
   227
            return hash;
toni@1150
   228
        }
toni@1150
   229
toni@1150
   230
        @Override
toni@1150
   231
        public boolean equals(Object obj) {
toni@1150
   232
            if (obj == null) {
toni@1150
   233
                return false;
toni@1150
   234
            }
toni@1150
   235
            if (getClass() != obj.getClass()) {
toni@1150
   236
                return false;
toni@1150
   237
            }
toni@1150
   238
            if (!super.equals(obj)) {
toni@1150
   239
                return false;
toni@1150
   240
            }
toni@1150
   241
            final RadialGradient other = (RadialGradient) obj;
toni@1150
   242
            if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
toni@1150
   243
                return false;
toni@1150
   244
            }
toni@1150
   245
            if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
toni@1150
   246
                return false;
toni@1150
   247
            }
toni@1154
   248
            if ((this.getCached() == null) != (other.getCached() == null)) {
toni@1154
   249
                return false;
toni@1154
   250
            }
toni@1150
   251
            return true;
toni@1150
   252
        }
toni@1150
   253
    }
toni@1129
   254
}