javaquery/canvas/src/main/java/net/java/html/canvas/Image.java
author Anton Epple <toni.epple@eppleton.de>
Sat, 07 Sep 2013 18:25:09 +0200
branchcanvas
changeset 1263 088331d4cb76
parent 1158 633572e14095
child 1302 e67363288df1
permissions -rw-r--r--
changes to canvas and graphicsenv to support additional methods fillPolygon and mergeImages
     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.Objects;
    21 import java.util.ServiceLoader;
    22 import net.java.html.canvas.spi.GraphicsEnvironment;
    23 
    24 /**
    25  * Image represents an Image Resource defined by a path.
    26  *
    27  * @author antonepple
    28  */
    29 public class Image {
    30 
    31     private String src;
    32     private Object cached;
    33     private int cacheHash;
    34 
    35     public static Image create(String src) {
    36         return new Image(src);
    37     }
    38 
    39     private Image(String src) {
    40         this.src = src;
    41     }
    42 
    43     void cache(Object toCache) {
    44         this.cached = toCache;
    45         cacheHash = hashCode();
    46     }
    47 
    48     private boolean isCached() {
    49         return cacheHash == hashCode();
    50     }
    51 
    52     Object getCached() {
    53         return isCached() ? cached : null;
    54     }
    55 
    56     public String getSrc() {
    57         return src;
    58     }
    59 
    60     public int getWidth() {
    61         ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
    62         GraphicsEnvironment ge = null;
    63         for (GraphicsEnvironment graphicsEnvironment : loader) {
    64             ge = graphicsEnvironment;
    65             break;
    66         }
    67         return ge.getWidth(this, cached);
    68     }
    69 
    70     public int getHeight() {
    71         ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
    72         GraphicsEnvironment ge = null;
    73         for (GraphicsEnvironment graphicsEnvironment : loader) {
    74             ge = graphicsEnvironment;
    75             break;
    76         }
    77         return ge.getHeight(this, cached);
    78     }
    79 
    80     @Override
    81     public int hashCode() {
    82         int hash = 7;
    83         hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237);
    84         return hash;
    85     }
    86 
    87     @Override
    88     public boolean equals(Object obj) {
    89         if (obj == null) {
    90             return false;
    91         }
    92         if (getClass() != obj.getClass()) {
    93             return false;
    94         }
    95         final Image other = (Image) obj;
    96         if (!Objects.equals(this.src, other.src)) {
    97             return false;
    98         }
    99         if ((cached == null) != (other.getCached() == null)) {
   100             return false;
   101         }
   102         return true;
   103     }
   104 }