📄 mediatracker.java
字号:
}
if (numerrors == 0) {
return null;
}
Object errors[] = new Object[numerrors];
cur = head;
numerrors = 0;
while (cur != null) {
if ((cur.getStatus(false) & ERRORED) != 0) {
errors[numerrors++] = cur.getMedia();
}
cur = cur.next;
}
return errors;
}
/**
* Starts loading all images. Waits until they have finished loading,
* are aborted, or it receives an error.
* If there is an error while loading or scaling an image then that
* image is considered "complete."
* Use isErrorAny() or statusAll() to check for errors.
* @exception InterruptedException
* Another thread has interrupted this thread.
* @see #waitForID
* @see #waitForAll(long)
* @see #isErrorAny
* @see #isErrorID
*/
public void waitForAll() throws InterruptedException {
waitForAll(0);
}
/**
* Starts loading all images. Waits until they have finished loading,
* are aborted, it receives an error, or until the specified timeout has
* elapsed.
* If there is an error while loading or scaling an image then that
* image is considered "complete."
* Use isErrorAny() or statusAll() to check for errors.
* @param ms the length of time to wait for the loading to complete
* @return true if all images were successfully loaded
* @see #waitForID
* @see #waitForAll()
* @see #isErrorAny
* @see #isErrorID
* @exception InterruptedException
* Another thread has interrupted this thread.
*/
public synchronized boolean waitForAll(long ms)
throws InterruptedException
{
long end = System.currentTimeMillis() + ms;
boolean first = true;
while (true) {
int status = statusAll(first);
if ((status & LOADING) == 0) {
return (status == COMPLETE);
}
first = false;
long timeout;
if (ms == 0) {
timeout = 0;
} else {
timeout = end - System.currentTimeMillis();
if (timeout <= 0) {
return false;
}
}
wait(timeout);
}
}
/**
* Returns the boolean OR of the status of all of the media being
* tracked.
* @param load specifies whether to start the media loading
* @see #statusID
* @see #LOADING
* @see #ABORTED
* @see #ERRORED
* @see #COMPLETE
*/
public int statusAll(boolean load) {
MediaEntry cur = head;
int status = 0;
while (cur != null) {
status = status | cur.getStatus(load);
cur = cur.next;
}
return status;
}
/**
* Checks to see if all images tagged with the indicated ID have
* finished loading, but does not start loading the images if they
* are not already loading.
* If there is an error while loading or scaling an image then that
* image is considered "complete."
* Use isErrorAny() or isErrorID() to check for errors.
* @param id the identifier used to determine which images to check
* @return true if all tagged images have finished loading, were
* aborted, or an error occurred.
* @see #checkID(int, boolean)
* @see #checkAll
* @see #isErrorAny
* @see #isErrorID
*/
public boolean checkID(int id) {
return checkID(id, false);
}
/**
* Checks to see if all images tagged with the indicated ID have
* finished loading. If load is true, starts loading any images
* with that ID that are not yet being loaded. If there is an
* error while loading or scaling an image then that image is
* considered "complete." Use isErrorAny() or isErrorID() to
* check for errors.
* @param id the identifier used to determine which images to check
* @param load start loading the images if this parameter is true
* @return true if all tagged images have finished loading, were
* aborted, or an error occurred
* @see #checkID(int)
* @see #checkAll
* @see #isErrorAny
* @see #isErrorID
*/
public synchronized boolean checkID(int id, boolean load) {
MediaEntry cur = head;
boolean done = true;
while (cur != null) {
if (cur.getID() == id && (cur.getStatus(load) & DONE) == 0) {
done = false;
}
cur = cur.next;
}
return done;
}
/**
* Checks the error status of all of the images with the specified ID.
* @param id the identifier used to determine which images to check
* @return true if any of the tagged images had an error during loading
* @see #isErrorAny
* @see #getErrorsID
*/
public synchronized boolean isErrorID(int id) {
MediaEntry cur = head;
while (cur != null) {
if (cur.getID() == id && (cur.getStatus(false) & ERRORED) != 0) {
return true;
}
cur = cur.next;
}
return false;
}
/**
* Returns a list of media with the specified ID that have encountered
* an error.
* @param id the identifier used to determine which images to return
* @return an array of media objects or null if there are no errors
* @see #isErrorID
* @see #getErrorsAny
*/
public synchronized Object[] getErrorsID(int id) {
MediaEntry cur = head;
int numerrors = 0;
while (cur != null) {
if (cur.getID() == id && (cur.getStatus(false) & ERRORED) != 0) {
numerrors++;
}
cur = cur.next;
}
if (numerrors == 0) {
return null;
}
Object errors[] = new Object[numerrors];
cur = head;
numerrors = 0;
while (cur != null) {
if (cur.getID() == id && (cur.getStatus(false) & ERRORED) != 0) {
errors[numerrors++] = cur.getMedia();
}
cur = cur.next;
}
return errors;
}
/**
* Starts loading all images with the specified ID and waits until they
* have finished loading or receive an error.
* If there is an error while loading or scaling an image then that
* image is considered "complete."
* Use statusID() or isErrorID() to check for errors.
* @param id the identifier used to determine which images to wait for
* @see #waitForAll
* @see #waitForID(int, long)
* @see #isErrorAny
* @see #isErrorID
* @exception InterruptedException
* Another thread has interrupted this thread.
*/
public void waitForID(int id) throws InterruptedException {
waitForID(id, 0);
}
/**
* Starts loading all images with the specified ID. Waits until they
* have finished loading, an error occurs, or the specified
* timeout has elapsed.
* If there is an error while loading or scaling an image then that
* image is considered "complete."
* Use statusID or isErrorID to check for errors.
* @param id the identifier used to determine which images to wait for
* @param ms the length of time to wait for the loading to complete
* @see #waitForAll
* @see #waitForID(int)
* @see #isErrorAny
* @see #isErrorID
* @exception InterruptedException
* Another thread has interrupted this thread.
*/
public synchronized boolean waitForID(int id, long ms)
throws InterruptedException
{
long end = System.currentTimeMillis() + ms;
boolean first = true;
while (true) {
int status = statusID(id, first);
if ((status & LOADING) == 0) {
return (status == COMPLETE);
}
first = false;
long timeout;
if (ms == 0) {
timeout = 0;
} else {
timeout = end - System.currentTimeMillis();
if (timeout <= 0) {
return false;
}
}
wait(timeout);
}
}
/**
* Returns the boolean OR of the status of all of the media with
* a given ID.
* @param id the identifier used to determine which images to check
* @param load specifies whether to start the media loading
* @see #statusAll
* @see #LOADING
* @see #ABORTED
* @see #ERRORED
* @see #COMPLETE
*/
public int statusID(int id, boolean load) {
MediaEntry cur = head;
int status = 0;
while (cur != null) {
if (cur.getID() == id) {
status = status | cur.getStatus(load);
}
cur = cur.next;
}
return status;
}
synchronized void setDone() {
notifyAll();
}
}
abstract class MediaEntry {
MediaTracker tracker;
int ID;
MediaEntry next;
int status;
MediaEntry(MediaTracker mt, int id) {
tracker = mt;
ID = id;
}
abstract Object getMedia();
static MediaEntry insert(MediaEntry head, MediaEntry me) {
MediaEntry cur = head;
MediaEntry prev = null;
while (cur != null) {
if (cur.ID > me.ID) {
break;
}
prev = cur;
cur = cur.next;
}
me.next = cur;
if (prev == null) {
head = me;
} else {
prev.next = me;
}
return head;
}
int getID() {
return ID;
}
abstract void startLoad();
static final int LOADING = MediaTracker.LOADING;
static final int ABORTED = MediaTracker.ABORTED;
static final int ERRORED = MediaTracker.ERRORED;
static final int COMPLETE = MediaTracker.COMPLETE;
static final int LOADSTARTED = (LOADING | ERRORED | COMPLETE);
static final int DONE = (ABORTED | ERRORED | COMPLETE);
synchronized int getStatus(boolean doLoad) {
if (doLoad && ((status & LOADSTARTED) == 0)) {
status = (status & ~ABORTED) | LOADING;
startLoad();
}
return status;
}
void setStatus(int flag) {
synchronized (this) {
status = flag;
}
tracker.setDone();
}
}
class ImageMediaEntry extends MediaEntry implements ImageObserver {
Image image;
int width;
int height;
ImageMediaEntry(MediaTracker mt, Image img, int c, int w, int h) {
super(mt, c);
image = img;
width = w;
height = h;
}
Object getMedia() {
return image;
}
void startLoad() {
if ( tracker.compTarget != null )
if (tracker.compTarget.prepareImage(image, width, height, this)) {
setStatus(COMPLETE);
}
else
if (tracker.tkTarget.prepareImage(image, width, height, this)) {
setStatus(COMPLETE);
}
}
public boolean imageUpdate(Image img, int infoflags,
int x, int y, int w, int h) {
if ((infoflags & ERROR) != 0) {
setStatus(ERRORED);
} else if ((infoflags & ABORT) != 0) {
setStatus(ABORTED);
} else if ((infoflags & (ALLBITS | FRAMEBITS)) != 0) {
setStatus(COMPLETE);
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -