Name: pvC76716 Date: 05/14/99
If we leave register an observer with animated image ( for some purposes,
with prepareImage() for example ), and return false from it once, than
the next call to the image will lead for image to be refetched.
Since I understand, when any image observer is removed, ImageRepresentation
class disposes the image if ALLBITS state for image wasn't reached.
But we never get ALLBITS set in imageinfo in infinite animating image.
This is shown by the following piece of code. URL is any URL that is
animated image.
------- file PutImage.java -------
import java.net.URL;
import java.awt.Graphics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import sun.awt.image.URLImageSource;
import java.awt.image.ImageObserver;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
public class PutImage extends Frame {
private Image img;
PutImage(URL url) {
MyWA w = new MyWA();
addWindowListener(w);
setSize(new Dimension(200,200));
setLayout(null);
show();
img = Toolkit.getDefaultToolkit().
createImage(new URLImageSource(url));
// this will run fetcher for the first time.
System.out.println("[ prepare start ]");
Thread x = new Preparer(this, img);
x.start();
try {
x.join();
} catch (Exception e) {
e.printStackTrace();
}
// paint the image.
System.out.println("[ paint ]");
getGraphics().drawImage(img, 40, 40, 100, 100, this);
}
public boolean imageUpdate(Image img, int imgflags, int x, int y,
int w, int h) {
if ((imgflags & (ABORT | ERROR)) != 0) {
System.out.println("Image fetching failed!");
System.exit(0);
}
if ((imgflags & (ALLBITS | FRAMEBITS)) != 0) {
repaint();
return false;
}
return true;
}
public void paint(Graphics g) {
update(g);
}
public synchronized void update(Graphics g) {
g.drawImage(img, 40,40, 100,100, this);
}
public static void main(String args[]) {
if (args.length < 1) {
System.out.println("Usage: java PutImage url");
} else {
try {
URL url = new URL(args[0]);
new PutImage(url);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class Preparer extends Thread implements ImageObserver {
Component comp;
Image img;
Thread prep;
Preparer(Component comp, Image img) {
this.img = img;
this.comp = comp;
}
public void run() {
prep = Thread.currentThread();
comp.prepareImage(img, 100, 100, this);
suspend();
try {
sleep(100); // delay
} catch (Exception e) {
e.printStackTrace(); // ?
}
}
public boolean imageUpdate(Image img, int imgflags, int x, int y,
int w, int h) {
if ((imgflags & (ABORT | ERROR)) != 0) {
System.out.println("Image fetching failed!");
System.exit(0);
}
if ((imgflags & (ALLBITS|FRAMEBITS)) != 0) {
prep.resume();
System.out.println("[ prepare done ]");
return false;
}
return true;
}
}
class MyWA extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
------- file PutImage.java -------
======================================================================