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.
32 private Object cached;
33 private int cacheHash;
35 public static Image create(String src) {
36 return new Image(src);
39 private Image(String src) {
43 void cache(Object toCache) {
44 this.cached = toCache;
45 cacheHash = hashCode();
48 private boolean isCached() {
49 return cacheHash == hashCode();
53 return isCached() ? cached : null;
56 public String getSrc() {
60 public int getWidth() {
61 ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
62 GraphicsEnvironment ge = null;
63 for (GraphicsEnvironment graphicsEnvironment : loader) {
64 ge = graphicsEnvironment;
67 return ge.getWidth(this, cached);
70 public int getHeight() {
71 ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
72 GraphicsEnvironment ge = null;
73 for (GraphicsEnvironment graphicsEnvironment : loader) {
74 ge = graphicsEnvironment;
77 return ge.getHeight(this, cached);
81 public int hashCode() {
83 hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237);
88 public boolean equals(Object obj) {
92 if (getClass() != obj.getClass()) {
95 final Image other = (Image) obj;
96 if (!Objects.equals(this.src, other.src)) {
99 if ((cached == null) != (other.getCached() == null)) {