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