imageObserver -- still don't understand

D

Dmitriy.pindrik

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
 
T

Thomas Fritsch

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");
There are several problems with the line above:
(*) Toolkit.getImage doesn't support BMP files (only GIF, JPEG, XBM, PNG).
(*) "C:/map7bg.bmp" has the wrong slash. Use "C:\\map7bg.bmp" on Windows.
(*) Applet security normally forbids accessing local files.
(*) Your paint-method might be called several hundred times per second.
It is therefore a bad idea to call getImage(...) here. You should
make i a member variable and put the 'i = ...;' code somewhere else,
may be in your applet's init() method.
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;
}
}
Normally you should *not* implement an imageUpdate(...) method of your own.
java.applet.Applet has already an imageUpdate(...) method (inherited from
java.awt.Component) which does a fine job. Your implmenentation is
especially wrong, because your "return false;" means: 'I have received the
complete image now. Don't call imageUpdate(...) again!'. See also the API
doc Component#imageUpdate and may be the source code of
Component#imageUpdate, too.
 
A

alexandre_paterson

Thomas Fritsch wrote:
....
(*) "C:/map7bg.bmp" has the wrong slash. Use "C:\\map7bg.bmp" on Windows.

Actually the forward slash works just fine as a file separator under
Windows in this case.

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top