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
 
K

Knute Johnson

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

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);
}
}
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top