javaquery/canvas/src/main/java/net/java/html/canvas/Style.java
author Anton Epple <toni.epple@eppleton.de>
Thu, 26 Sep 2013 16:33:04 -0700
branchcanvas
changeset 1308 e8429fba8cce
parent 1302 e67363288df1
child 1446 619f507713a2
permissions -rw-r--r--
documentation
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package net.java.html.canvas;
    19 
    20 import java.util.Collection;
    21 import java.util.HashMap;
    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. 
    56      * Default 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, repeat-x, repeat-y, or no-repeat.
    67          */
    68         public Pattern(Image imageResource, String repeat) {
    69             this.imageResource = imageResource;
    70             this.repeat = repeat;
    71         }
    72 
    73         /**
    74          * Get the base image of this pattern
    75          * @return the base image of this pattern
    76          */
    77         public Image getImageResource() {
    78             return imageResource;
    79         }
    80 
    81         /**
    82          * Get the repeat style for this pattern
    83          * @return return the repeat style
    84          */
    85         public String getRepeat() {
    86             return repeat;
    87         }
    88 
    89     }
    90 
    91     public static final class Color extends Style {
    92 
    93         private String web;
    94 
    95         /**
    96          * Creates an RGB color specified with an HTML or CSS attribute string.
    97          *
    98          * @param webColor Color defined as web color (e.g. #ff0000)
    99          */
   100         public Color(String webColor) {
   101             this.web = webColor;
   102         }
   103 
   104         /**
   105          * 
   106          * 
   107          * @return the Color value as a Web Color (e.g. #ff0000)
   108          */
   109         public String getAsString() {
   110             return web;
   111         }
   112     }
   113 
   114     /**
   115      * A Linear Gradient. The GRadient has a direction defined by two coordinates
   116      * and stops defining the Color at a specific position.
   117      */
   118     public static class LinearGradient extends Style {
   119 
   120         private HashMap<Double, String> stops;
   121         private double x0, y0, x1, y1;
   122 
   123         /**
   124          * 
   125          * @param x0 the x coordinate of the start point for this gradient
   126          * @param y0 the y coordinate of the start point for this gradient
   127          * @param x1 the x coordinate of the end point for this gradient
   128          * @param y1 the y coordinate of the end point for this gradient
   129          */
   130         LinearGradient(double x0, double y0, double x1, double y1) {
   131             this.x0 = x0;
   132             this.y0 = y0;
   133             this.x1 = x1;
   134             this.y1 = y1;
   135         }
   136 
   137         /**
   138          * Add a new Color stop. A color stop defines a fixed color at a position
   139          * along the coordinates.
   140          * @param position the position of this stop in percent [0.0-1.0]
   141          * @param color A Color defined in web format (e.g. #ff0000) 
   142          */
   143         void addColorStop(double position, String color) {
   144             if (stops == null) {
   145                 stops = new HashMap<>();
   146             }
   147             stops.put(position, color);
   148         }
   149 
   150         /**
   151          * Get the stops of this gradient. 
   152          * @return the stops of this gradient
   153          */
   154         public HashMap<Double, String> getStops(){
   155             return new HashMap<>(stops);
   156         }
   157         
   158         /**
   159          * Set the stops as Position/Color pairs 
   160          * @param stops the stops for thsi Gradient
   161          */
   162         public void setStops(HashMap<Double, String> stops) {     
   163             this.stops = new HashMap<>(stops);
   164         }
   165 
   166         
   167         public double getX0() {
   168             return x0;
   169         }
   170 
   171         public void setX0(double x0) {
   172             this.x0 = x0;
   173         }
   174 
   175         public double getY0() {
   176             return y0;
   177         }
   178 
   179         public void setY0(double y0) {
   180             this.y0 = y0;
   181         }
   182 
   183         public double getX1() {
   184             return x1;
   185         }
   186 
   187         public void setX1(double x1) {
   188             this.x1 = x1;
   189         }
   190 
   191         public double getY1() {
   192             return y1;
   193         }
   194 
   195         public void setY1(double y1) {
   196             this.y1 = y1;
   197         }
   198 
   199         @Override
   200         public int hashCode() {
   201             int hash = 7;
   202             hash = 29 * hash + Objects.hashCode(this.stops);
   203             hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
   204             hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
   205             hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
   206             hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
   207             return hash;
   208         }
   209 
   210         @Override
   211         public boolean equals(Object obj) {
   212             if (obj == null) {
   213                 return false;
   214             }
   215             if (getClass() != obj.getClass()) {
   216                 return false;
   217             }
   218             final LinearGradient other = (LinearGradient) obj;
   219             if (!Objects.equals(this.stops, other.stops)) {
   220                 return false;
   221             }
   222             if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
   223                 return false;
   224             }
   225             if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
   226                 return false;
   227             }
   228             if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
   229                 return false;
   230             }
   231             if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
   232                 return false;
   233             }
   234             return true;
   235         }
   236     }
   237 
   238     public static final class RadialGradient extends LinearGradient {
   239 
   240         private double r0, r1;
   241 
   242         RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
   243             super(x0, y0, x1, y1);
   244             this.r0 = r0;
   245             this.r1 = r1;
   246         }
   247 
   248         public double getR0() {
   249             return r0;
   250         }
   251 
   252         public void setR0(double r0) {
   253             this.r0 = r0;
   254         }
   255 
   256         public double getR1() {
   257             return r1;
   258         }
   259 
   260         public void setR1(double r1) {
   261             this.r1 = r1;
   262         }
   263 
   264         @Override
   265         public int hashCode() {
   266             int hash = super.hashCode();
   267             hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
   268             hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
   269 
   270             return hash;
   271         }
   272 
   273         @Override
   274         public boolean equals(Object obj) {
   275             if (obj == null) {
   276                 return false;
   277             }
   278             if (getClass() != obj.getClass()) {
   279                 return false;
   280             }
   281             if (!super.equals(obj)) {
   282                 return false;
   283             }
   284             final RadialGradient other = (RadialGradient) obj;
   285             if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
   286                 return false;
   287             }
   288             if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
   289                 return false;
   290             }
   291             if ((this.getCached() == null) != (other.getCached() == null)) {
   292                 return false;
   293             }
   294             return true;
   295         }
   296     }
   297 }