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