toni@1144
|
1 |
/**
|
toni@1302
|
2 |
* Back 2 Browser Bytecode Translator
|
toni@1302
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
toni@1144
|
4 |
*
|
toni@1302
|
5 |
* This program is free software: you can redistribute it and/or modify
|
toni@1302
|
6 |
* it under the terms of the GNU General Public License as published by
|
toni@1302
|
7 |
* the Free Software Foundation, version 2 of the License.
|
toni@1144
|
8 |
*
|
toni@1302
|
9 |
* This program is distributed in the hope that it will be useful,
|
toni@1302
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
toni@1302
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
toni@1302
|
12 |
* GNU General Public License for more details.
|
toni@1144
|
13 |
*
|
toni@1302
|
14 |
* You should have received a copy of the GNU General Public License
|
toni@1302
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
toni@1302
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
toni@1144
|
17 |
*/
|
toni@1144
|
18 |
package net.java.html.canvas;
|
toni@1144
|
19 |
|
toni@1144
|
20 |
import java.util.Objects;
|
toni@1263
|
21 |
import java.util.ServiceLoader;
|
toni@1263
|
22 |
import net.java.html.canvas.spi.GraphicsEnvironment;
|
toni@1144
|
23 |
|
toni@1144
|
24 |
/**
|
toni@1154
|
25 |
* Image represents an Image Resource defined by a path.
|
toni@1144
|
26 |
*
|
toni@1144
|
27 |
* @author antonepple
|
toni@1144
|
28 |
*/
|
toni@1304
|
29 |
public final class Image {
|
toni@1144
|
30 |
|
toni@1144
|
31 |
private String src;
|
toni@1308
|
32 |
private int cacheHash;
|
toni@1144
|
33 |
private Object cached;
|
toni@1144
|
34 |
|
jaroslav@1438
|
35 |
/** Creates a new image referencing a specified URL.
|
jaroslav@1438
|
36 |
* @param src the (relative) URL to the image
|
jaroslav@1438
|
37 |
* @return an object representing the image
|
jaroslav@1438
|
38 |
*/
|
toni@1263
|
39 |
public static Image create(String src) {
|
toni@1263
|
40 |
return new Image(src);
|
toni@1263
|
41 |
}
|
toni@1263
|
42 |
|
toni@1263
|
43 |
private Image(String src) {
|
toni@1144
|
44 |
this.src = src;
|
toni@1144
|
45 |
}
|
toni@1144
|
46 |
|
toni@1144
|
47 |
void cache(Object toCache) {
|
toni@1263
|
48 |
this.cached = toCache;
|
toni@1144
|
49 |
cacheHash = hashCode();
|
toni@1144
|
50 |
}
|
toni@1144
|
51 |
|
toni@1144
|
52 |
private boolean isCached() {
|
toni@1144
|
53 |
return cacheHash == hashCode();
|
toni@1144
|
54 |
}
|
toni@1144
|
55 |
|
toni@1144
|
56 |
Object getCached() {
|
toni@1144
|
57 |
return isCached() ? cached : null;
|
toni@1144
|
58 |
}
|
toni@1144
|
59 |
|
toni@1144
|
60 |
public String getSrc() {
|
toni@1144
|
61 |
return src;
|
toni@1144
|
62 |
}
|
toni@1144
|
63 |
|
toni@1445
|
64 |
/**
|
toni@1445
|
65 |
* get the width of this Image. Might be an expensive operation, depending on the
|
toni@1445
|
66 |
* GraphicsEnvironment, it might need to be rendered.
|
toni@1445
|
67 |
* @return the width of this Image.
|
toni@1445
|
68 |
*/
|
toni@1263
|
69 |
public int getWidth() {
|
toni@1263
|
70 |
ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
|
toni@1263
|
71 |
GraphicsEnvironment ge = null;
|
toni@1263
|
72 |
for (GraphicsEnvironment graphicsEnvironment : loader) {
|
toni@1263
|
73 |
ge = graphicsEnvironment;
|
toni@1263
|
74 |
break;
|
toni@1263
|
75 |
}
|
toni@1263
|
76 |
return ge.getWidth(this, cached);
|
toni@1263
|
77 |
}
|
toni@1263
|
78 |
|
toni@1445
|
79 |
/**
|
toni@1445
|
80 |
* get the height of this Image. Might be an expensive operation, depending on the
|
toni@1445
|
81 |
* GraphicsEnvironment, it might need to be rendered.
|
toni@1445
|
82 |
* @return the height of this Image.
|
toni@1445
|
83 |
*/
|
toni@1263
|
84 |
public int getHeight() {
|
toni@1263
|
85 |
ServiceLoader<GraphicsEnvironment> loader = ServiceLoader.load(GraphicsEnvironment.class);
|
toni@1263
|
86 |
GraphicsEnvironment ge = null;
|
toni@1263
|
87 |
for (GraphicsEnvironment graphicsEnvironment : loader) {
|
toni@1263
|
88 |
ge = graphicsEnvironment;
|
toni@1263
|
89 |
break;
|
toni@1263
|
90 |
}
|
toni@1263
|
91 |
return ge.getHeight(this, cached);
|
toni@1263
|
92 |
}
|
toni@1263
|
93 |
|
toni@1144
|
94 |
@Override
|
toni@1144
|
95 |
public int hashCode() {
|
toni@1144
|
96 |
int hash = 7;
|
toni@1263
|
97 |
hash = 59 * hash + Objects.hashCode(this.src) ^ (cached == null ? 1231 : 1237);
|
toni@1144
|
98 |
return hash;
|
toni@1144
|
99 |
}
|
toni@1144
|
100 |
|
toni@1144
|
101 |
@Override
|
toni@1144
|
102 |
public boolean equals(Object obj) {
|
toni@1144
|
103 |
if (obj == null) {
|
toni@1144
|
104 |
return false;
|
toni@1144
|
105 |
}
|
toni@1144
|
106 |
if (getClass() != obj.getClass()) {
|
toni@1144
|
107 |
return false;
|
toni@1144
|
108 |
}
|
toni@1144
|
109 |
final Image other = (Image) obj;
|
toni@1144
|
110 |
if (!Objects.equals(this.src, other.src)) {
|
toni@1144
|
111 |
return false;
|
toni@1144
|
112 |
}
|
toni@1263
|
113 |
if ((cached == null) != (other.getCached() == null)) {
|
toni@1153
|
114 |
return false;
|
toni@1153
|
115 |
}
|
toni@1144
|
116 |
return true;
|
toni@1144
|
117 |
}
|
toni@1144
|
118 |
}
|