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