MediaTracker
A MediaTracker is an object that will check the status of an
arbitrary number of images in parallel. To use MediaTracker, you create a new instance and use its addImage( ) method to track the loading
status of an image. addImage( ) has
the following general forms:
void addImage(Image imgObj, int imgID)
void addImage(Image imgObj, int imgID, int width, int height)
Here, imgObj is the image being tracked. Its identification number is
passed in imgID. ID numbers do not
need to be unique. You can use the same number with several images as a means
of identifying them as part of a group. Furthermore, images with lower IDs are
given priority over those with higher IDs when loading. In the second form, width and height specify the dimensions of the object when it is displayed.
Once you’ve registered an
image, you can check whether it’s loaded, or you can wait for it to completely
load. To check the status of an image, call checkID( ). The version used in this chapter is shown here:
boolean checkID(int imgID)
Here, imgID specifies the ID of the image you want to check. The method
returns true if all images that have
the specified ID have been loaded (or if an error or user-abort has terminated
loading). Otherwise, it returns false.
You can use the checkAll( ) method
to see if all images being tracked have been loaded.
You should use MediaTracker when loading a group of
images. If all of the images that you’re interested in aren’t downloaded, you
can display something else to entertain the user until they all arrive.
Here’s an example that loads
a three-image slide show and displays a nice bar chart of the loading progress:
/*
<applet code="TrackedImageLoad"
width=400 height=345>
<param name="img"
value="Lilies+SunFlower+ConeFlowers">
</applet>
*/
import java.util.*; import java.applet.*;
import java.awt.*;
public class TrackedImageLoad extends Applet
implements Runnable { MediaTracker tracker;
int tracked;
int frame_rate = 5; int current_img = 0; Thread
motor;
static final int MAXIMAGES = 10; Image img[] =
new Image[MAXIMAGES]; String name[] = new String[MAXIMAGES]; volatile boolean
stopFlag;
public void init() {
tracker = new MediaTracker(this);
StringTokenizer st = new
StringTokenizer(getParameter("img"), "+");
while(st.hasMoreTokens() && tracked
<= MAXIMAGES) { name[tracked] = st.nextToken();
img[tracked] = getImage(getDocumentBase(),
name[tracked] + ".jpg");
tracker.addImage(img[tracked], tracked);
tracked++;
}
}
public void paint(Graphics g) { String loaded =
"";
int donecount = 0;
for(int i=0; i<tracked; i++) {
if (tracker.checkID(i, true)) {
donecount++;
loaded += name[i] + " ";
}
}
Dimension d = getSize(); int w = d.width;
int h = d.height;
if (donecount == tracked) { frame_rate = 1;
Image i = img[current_img++]; int iw =
i.getWidth(null); int ih = i.getHeight(null);
g.drawImage(i, (w - iw)/2, (h - ih)/2, null);
if (current_img >= tracked)
current_img = 0;
} else {
int x = w * donecount / tracked;
g.setColor(Color.black); g.fillRect(0, h/3, x, 16); g.setColor(Color.white);
g.fillRect(x, h/3, w-x, 16); g.setColor(Color.black); g.drawString(loaded, 10,
h/2);
}
}
public void start() { motor = new Thread(this);
stopFlag = false; motor.start();
}
public void stop() { stopFlag = true;
}
public void run() {
motor.setPriority(Thread.MIN_PRIORITY); while (true) {
repaint(); try {
Thread.sleep(1000/frame_rate);
} catch (InterruptedException e) {
System.out.println("Interrupted"); return;
}
if(stopFlag)
return;
}
}
}
This example creates a new MediaTracker in the init( ) method and then adds each of
the named images as a tracked image with addImage(
). In the paint( ) method, it
calls checkID( ) on each of the
images that we’re tracking. If all of the images are loaded, they are displayed. If not, a simple bar
chart of the number of images loaded is shown, with the names of the fully
loaded images displayed underneath the bar.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.