2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package net.java.html.canvas;
20 import java.util.Objects;
21 import java.util.ServiceLoader;
22 import net.java.html.canvas.spi.GraphicsEnvironment;
25 * Image represents an Image Resource defined by a path.
29 public final class Image {
32 private int cacheHash;
33 private Object cached;
35 /** Creates a new image referencing a specified URL.
36 * @param src the (relative) URL to the image
37 * @return an object representing the image
39 public static Image create(String src) {
40 return new Image(src);
43 private Image(String src) {
47 void cache(Object toCache) {
48 this.cached = toCache;
49 cacheHash = hashCode();
52 private boolean isCached() {
53 return cacheHash == hashCode();
57 return isCached() ? cached : null;
60 public String getSrc() {
64 public int getWidth() {
65 ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
66 GraphicsEnvironment ge = null;
67 for (GraphicsEnvironment graphicsEnvironment : loader) {
68 ge = graphicsEnvironment;
71 return ge.getWidth(this, cached);
75 public int getHeight() {
76 ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
77 GraphicsEnvironment ge = null;
78 for (GraphicsEnvironment graphicsEnvironment : loader) {
79 ge = graphicsEnvironment;
82 return ge.getHeight(this, cached);
86 public int hashCode() {
88 hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237);
93 public boolean equals(Object obj) {
97 if (getClass() != obj.getClass()) {
100 final Image other = (Image) obj;
101 if (!Objects.equals(this.src, other.src)) {
104 if ((cached == null) != (other.getCached() == null)) {