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