I've read all I can about what imageObserver is and how to
implement/use it. After several hours of reading, I am still failing.
Here is a piece of my code - any help, pointer, or comments is
appreciated because as of right now, it's not fuctioning at all.
All that is supposed to happen is I want image map7bg.bmp to paint...
simple as that.
<code>
public class Engine extends Applet implements KeyListener,
ImageObserver
{
<--code omited-->
public void paint(Graphics g)
{
Image i = (Toolkit.getDefaultToolkit()).getImage
("C:/map7bg.bmp");
g.drawImage(i,1,1, this); //also tried null instead of null
}
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width,int height)
{
return false;
}
}
</code>
I added that imageUpdate method after reading about it online, seems I
need it for imageObserver to work.
Thanks in advance for any help,
Dima
Knute Johnson - 18 Apr 2006 19:45 GMT
> I've read all I can about what imageObserver is and how to
> implement/use it. After several hours of reading, I am still failing.
[quoted text clipped - 29 lines]
> Thanks in advance for any help,
> Dima
Dima:
Thomas gave you some really good information to help you understand your
problem. In addition here is a simple program that you can compile and
run to see how the ImageObserver works. Notice when you run it how many
times paint() is called. It can be 10s or 100s of times while the image
is being prepared. While I didn't include it in this example you can
use the checkImage() to get some information about the drawing status of
your image. In most cases however it isn't required.
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class ImageObserverExample extends Panel {
Image image;
String msg = "Loading Image";
int n;
public ImageObserverExample() {
try {
URL url = new URL(
"http://www.thealpacastore.com/alpacacam/latest640.jpg");
Toolkit toolkit = Toolkit.getDefaultToolkit();
// use createImage instead of getImage - see api docs for why
image = toolkit.createImage(url);
} catch (MalformedURLException murle) {
msg = "MalformedURLException";
}
setPreferredSize(new Dimension(400,300));
}
public void paint(Graphics g) {
// show the number of calls to paint()
System.out.println(++n);
// if there is an image and drawing has not finished
// note: if the image is null drawImage will return true
if (image == null ||
!g.drawImage(image,0,0,getWidth(),getHeight(),this)) {
// draw the message
g.drawString(msg,20,getHeight()/2);
System.out.println(msg);
}
}
public static void main(String[] args) {
Frame frame = new Frame("ImageObserverExample");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
ImageObserverExample examp = new ImageObserverExample();
frame.add(examp,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}

Signature
Knute Johnson
email s/nospam/knute/