51 |
import java.io.IOException; |
import java.io.IOException; |
52 |
import java.util.Hashtable; |
import java.util.Hashtable; |
53 |
import java.util.Vector; |
import java.util.Vector; |
54 |
|
import java.io.ByteArrayOutputStream; |
55 |
|
import java.io.BufferedInputStream; |
56 |
|
import java.net.URL; |
57 |
import gnu.classpath.Pointer; |
import gnu.classpath.Pointer; |
58 |
|
|
59 |
/** |
/** |
132 |
private native void setPixels(int[] pixels); |
private native void setPixels(int[] pixels); |
133 |
|
|
134 |
/** |
/** |
135 |
* Loads an image using gdk-pixbuf. |
* Loads an image using gdk-pixbuf from a file. |
136 |
*/ |
*/ |
137 |
private native boolean loadPixbuf(String name); |
private native boolean loadPixbuf(String name); |
138 |
|
|
139 |
/** |
/** |
140 |
|
* Loads an image using gdk-pixbuf from data. |
141 |
|
*/ |
142 |
|
private native boolean loadImageFromData(byte[] data); |
143 |
|
|
144 |
|
/** |
145 |
* Allocates a Gtk Pixbuf or pixmap |
* Allocates a Gtk Pixbuf or pixmap |
146 |
*/ |
*/ |
147 |
private native void createPixmap(); |
private native void createPixmap(); |
195 |
} |
} |
196 |
|
|
197 |
/** |
/** |
198 |
|
* Constructs a blank GtkImage. This is called when |
199 |
|
* GtkToolkit.createImage (String) is called with an empty string |
200 |
|
* argument (""). A blank image is loaded immediately upon |
201 |
|
* construction and has width -1 and height -1. |
202 |
|
*/ |
203 |
|
public GtkImage () |
204 |
|
{ |
205 |
|
isLoaded = true; |
206 |
|
observers = null; |
207 |
|
offScreen = false; |
208 |
|
props = new Hashtable(); |
209 |
|
errorLoading = false; |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
* Constructs a GtkImage by loading a given file. |
* Constructs a GtkImage by loading a given file. |
214 |
* |
* |
215 |
* @throws IllegalArgumentException if the image could not be loaded. |
* @throws IllegalArgumentException if the image could not be loaded. |
234 |
} |
} |
235 |
|
|
236 |
/** |
/** |
237 |
|
* Constructs a GtkImage from a byte array of an image file. |
238 |
|
* |
239 |
|
* @throws IllegalArgumentException if the image could not be |
240 |
|
* loaded. |
241 |
|
*/ |
242 |
|
public GtkImage (byte[] data) |
243 |
|
{ |
244 |
|
if (loadImageFromData (data) != true) |
245 |
|
throw new IllegalArgumentException ("Couldn't load image."); |
246 |
|
|
247 |
|
isLoaded = true; |
248 |
|
observers = null; |
249 |
|
offScreen = false; |
250 |
|
props = new Hashtable(); |
251 |
|
errorLoading = false; |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
|
* Constructs a GtkImage from a URL. May result in an error image. |
256 |
|
*/ |
257 |
|
public GtkImage (URL url) |
258 |
|
{ |
259 |
|
isLoaded = false; |
260 |
|
observers = new Vector(); |
261 |
|
errorLoading = false; |
262 |
|
if( url == null) |
263 |
|
return; |
264 |
|
ByteArrayOutputStream baos = new ByteArrayOutputStream (5000); |
265 |
|
try |
266 |
|
{ |
267 |
|
BufferedInputStream bis = new BufferedInputStream (url.openStream()); |
268 |
|
|
269 |
|
byte[] buf = new byte[5000]; |
270 |
|
int n = 0; |
271 |
|
|
272 |
|
while ((n = bis.read(buf)) != -1) |
273 |
|
baos.write(buf, 0, n); |
274 |
|
bis.close(); |
275 |
|
} |
276 |
|
catch(IOException e) |
277 |
|
{ |
278 |
|
throw new IllegalArgumentException ("Couldn't load image."); |
279 |
|
} |
280 |
|
if (loadImageFromData (baos.toByteArray()) != true) |
281 |
|
throw new IllegalArgumentException ("Couldn't load image."); |
282 |
|
|
283 |
|
isLoaded = true; |
284 |
|
observers = null; |
285 |
|
props = new Hashtable(); |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
* Constructs an empty GtkImage. |
* Constructs an empty GtkImage. |
290 |
*/ |
*/ |
291 |
public GtkImage (int width, int height) |
public GtkImage (int width, int height) |
316 |
} |
} |
317 |
|
|
318 |
/** |
/** |
319 |
|
* Package private constructor to create a GtkImage from a given |
320 |
|
* PixBuf pointer. |
321 |
|
*/ |
322 |
|
GtkImage (Pointer pixbuf) |
323 |
|
{ |
324 |
|
pixmap = pixbuf; |
325 |
|
createFromPixbuf(); |
326 |
|
isLoaded = true; |
327 |
|
observers = null; |
328 |
|
offScreen = false; |
329 |
|
props = new Hashtable(); |
330 |
|
} |
331 |
|
|
332 |
|
/** |
333 |
|
* Native helper function for constructor that takes a pixbuf Pointer. |
334 |
|
*/ |
335 |
|
private native void createFromPixbuf(); |
336 |
|
|
337 |
|
/** |
338 |
* Callback from the image consumer. |
* Callback from the image consumer. |
339 |
*/ |
*/ |
340 |
public void setImage(int width, int height, |
public void setImage(int width, int height, |