javaquery/canvas/src/main/java/net/java/html/canvas/Image.java
branchcanvas
changeset 1263 088331d4cb76
parent 1158 633572e14095
child 1302 e67363288df1
     1.1 --- a/javaquery/canvas/src/main/java/net/java/html/canvas/Image.java	Mon May 27 14:13:01 2013 +0200
     1.2 +++ b/javaquery/canvas/src/main/java/net/java/html/canvas/Image.java	Sat Sep 07 18:25:09 2013 +0200
     1.3 @@ -18,6 +18,8 @@
     1.4  package net.java.html.canvas;
     1.5  
     1.6  import java.util.Objects;
     1.7 +import java.util.ServiceLoader;
     1.8 +import net.java.html.canvas.spi.GraphicsEnvironment;
     1.9  
    1.10  /**
    1.11   * Image represents an Image Resource defined by a path.
    1.12 @@ -30,13 +32,17 @@
    1.13      private Object cached;
    1.14      private int cacheHash;
    1.15  
    1.16 -    Image(String src) {
    1.17 +    public static Image create(String src) {
    1.18 +        return new Image(src);
    1.19 +    }
    1.20 +
    1.21 +    private Image(String src) {
    1.22          this.src = src;
    1.23      }
    1.24  
    1.25      void cache(Object toCache) {
    1.26 +        this.cached = toCache;
    1.27          cacheHash = hashCode();
    1.28 -        this.cached = toCache;
    1.29      }
    1.30  
    1.31      private boolean isCached() {
    1.32 @@ -51,10 +57,30 @@
    1.33          return src;
    1.34      }
    1.35  
    1.36 +    public int getWidth() {
    1.37 +        ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
    1.38 +        GraphicsEnvironment ge = null;
    1.39 +        for (GraphicsEnvironment graphicsEnvironment : loader) {
    1.40 +            ge = graphicsEnvironment;
    1.41 +            break;
    1.42 +        }
    1.43 +        return ge.getWidth(this, cached);
    1.44 +    }
    1.45 +
    1.46 +    public int getHeight() {
    1.47 +        ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
    1.48 +        GraphicsEnvironment ge = null;
    1.49 +        for (GraphicsEnvironment graphicsEnvironment : loader) {
    1.50 +            ge = graphicsEnvironment;
    1.51 +            break;
    1.52 +        }
    1.53 +        return ge.getHeight(this, cached);
    1.54 +    }
    1.55 +
    1.56      @Override
    1.57      public int hashCode() {
    1.58          int hash = 7;
    1.59 -        hash = 59 * hash + Objects.hashCode(this.src) ^ (cached==null? 1231 : 1237);
    1.60 +        hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237);
    1.61          return hash;
    1.62      }
    1.63  
    1.64 @@ -70,10 +96,9 @@
    1.65          if (!Objects.equals(this.src, other.src)) {
    1.66              return false;
    1.67          }
    1.68 -        if ((cached==null) != (other.getCached()==null)){
    1.69 +        if ((cached == null) != (other.getCached() == null)) {
    1.70              return false;
    1.71          }
    1.72          return true;
    1.73      }
    1.74 - 
    1.75  }