At 12:15 PM 3/17/00 -0500, Ben Ashpole wrote:
> Is there a way to watch the progress of a download, at least for the
> download of an Image? Something that works in the background and fires
> events (that I can listen to) with updates about how much (bytes or
> percent of completion) would be really nice. MediaTracker only lets you
> query when its done, as opposed to the above, right? I can't believe
> there's nothing already written for this.
The details are covered in the docs for the flags in ImageObserver. You
can also look at the source for MediaTracker to help you see what is
going on. There are also many dozens of books on programming with Java
that give tons of tutorial and examples in the use of AWT.
Here is some untested code that should help:
class PercentageObserver implements ImageObserver
{
int width = -1;
int height = -1;
int total = -1;
int cumulative = 0;
public synchronized boolean imageUpdate(Image img
, int infoflags
, int x, int y, int w, int h)
{
if ((infoflags & WIDTH) != 0) {
width = w ;
if (total < 0) {
total = width * height;
}
}
if ((infoflags & HEIGHT) != 0) {
height = h;
if (total < 0) {
total = width * height;
}
}
if ((infoflags & SOMEBITS) != 0) {
cumulative += w * h;
}
if ((infoflags & (ALLBITS | FRAMEBITS)) != 0) {
if (total < 0) {
total = 1;
}
cumulative = total;
return false;
}
return true;
}
// Get the status of the download.
// A value less than zero means the percentage is unknown.
// A non-negative value is the percent of completion.
public synchronized int getPercentageLoaded()
{
if (total <= 0)
return total;
return Math.min(100, (cumulative * 100) / total);
}
}
To add this ImageObserver in addition to your MediaTracker, use
Component.checkImage(Image, ImageObserver). The Component you would use
would be the same one used for the MediaTracker (usually the Applet).
jim
----------------------------------------------------------------
James P. White Netscape DevEdge Champion for IFC
IFC Exchange * Insanely great Java * http://www.ifcx.org
[EMAIL PROTECTED] Pagesmiths' home is http://www.pagesmiths.com
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".