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