API Overview API Index Package Overview Direct link to this page
JDK 1.6
  java.awt. MediaTracker View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933

/*
 * @(#)MediaTracker.java	1.44 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.awt;

import java.awt.Component;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.image.ImageObserver;

/**
 * The <code>MediaTracker</code> class is a utility class to track 
 * the status of a number of media objects. Media objects could 
 * include audio clips as well as images, though currently only 
 * images are supported. 
 * <p>
 * To use a media tracker, create an instance of  
 * <code>MediaTracker</code> and call its <code>addImage</code>  
 * method for each image to be tracked. In addition, each image can 
 * be assigned a unique identifier. This identifier controls the 
 * priority order in which the images are fetched. It can also be used 
 * to identify unique subsets of the images that can be waited on 
 * independently. Images with a lower ID are loaded in preference to 
 * those with a higher ID number. 
 *
 * <p>
 *
 * Tracking an animated image 
 * might not always be useful
 * due to the multi-part nature of animated image
 * loading and painting,
 * but it is supported.
 * <code>MediaTracker</code> treats an animated image
 * as completely loaded
 * when the first frame is completely loaded.
 * At that point, the <code>MediaTracker</code> 
 * signals any waiters 
 * that the image is completely loaded.
 * If no <code>ImageObserver</code>s are observing the image
 * when the first frame has finished loading,
 * the image might flush itself
 * to conserve resources
 * (see {@link Image#flush()}).
 *
 * <p>
 * Here is an example of using <code>MediaTracker</code>: 
 * <p>
 * <hr><blockquote><pre>
 * import java.applet.Applet;
 * import java.awt.Color;
 * import java.awt.Image;
 * import java.awt.Graphics;
 * import java.awt.MediaTracker;
 *
 * public class ImageBlaster extends Applet implements Runnable {
 *	MediaTracker tracker;
 *	Image bg;
 *	Image anim[] = new Image[5];
 *	int index;
 *	Thread animator;
 *
 *	// Get the images for the background (id == 0) 
 *	// and the animation frames (id == 1) 
 *	// and add them to the MediaTracker
 *	public void init() {
 *	    tracker = new MediaTracker(this);
 *	    bg = getImage(getDocumentBase(), 
 *                  "images/background.gif");
 *	    tracker.addImage(bg, 0);
 *	    for (int i = 0; i < 5; i++) {
 *		anim[i] = getImage(getDocumentBase(), 
 *                      "images/anim"+i+".gif");
 *		tracker.addImage(anim[i], 1);
 *	    }
 *	}
 *
 *	// Start the animation thread.
 *	public void start() {
 *	    animator = new Thread(this);
 *	    animator.start();
 *	}
 *
 *	// Stop the animation thread.
 *	public void stop() {
 *	    animator = null;
 *	}
 *
 *	// Run the animation thread.
 *	// First wait for the background image to fully load 
 *	// and paint.  Then wait for all of the animation 
 *	// frames to finish loading. Finally, loop and 
 *	// increment the animation frame index.
 *	public void run() {
 *	    try {
 *		tracker.waitForID(0);
 *		tracker.waitForID(1);
 *	    } catch (InterruptedException e) {
 *		return;
 *	    }
 *	    Thread me = Thread.currentThread();
 *	    while (animator == me) {
 *		try {
 *		    Thread.sleep(100);
 *		} catch (InterruptedException e) {
 *		    break;
 *		}
 *		synchronized (this) {
 *		    index++;
 *		    if (index >= anim.length) {
 *			index = 0;
 *		    }
 *		}
 *		repaint();
 *	    }
 *	}
 *
 *	// The background image fills the frame so we 
 *	// don't need to clear the applet on repaints. 
 *	// Just call the paint method.
 *	public void update(Graphics g) {
 *	    paint(g);
 *	}
 *
 *	// Paint a large red rectangle if there are any errors 
 *	// loading the images.  Otherwise always paint the 
 *	// background so that it appears incrementally as it 
 *	// is loading.  Finally, only paint the current animation 
 *	// frame if all of the frames (id == 1) are done loading,
 *	// so that we don't get partial animations.
 *	public void paint(Graphics g) {
 *	    if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
 *		g.setColor(Color.red);
 *		g.fillRect(0, 0, size().width, size().height);
 *		return;
 *	    }
 *	    g.drawImage(bg, 0, 0, this);
 *	    if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
 *		g.drawImage(anim[index], 10, 10, this);
 *	    }
 *	}
 * }
 * </pre></blockquote><hr>
 *
 * @version 	1.44, 11/17/05
 * @author 	Jim Graham
 * @since       JDK1.0
 */
public class MediaTracker implements java.io.Serializable {
    
    /**
     * A given <code>Component</code> that will be
     * tracked by a media tracker where the image will
     * eventually be drawn.
     *
     * @serial
     * @see #MediaTracker(Component)
     */
    Component target;
    /**
     * The head of the list of <code>Images</code> that is being
     * tracked by the <code>MediaTracker</code>.
     *
     * @serial
     * @see #addImage(Image, int)
     * @see #removeImage(Image)
     */
    MediaEntry head;

    /*
     * JDK 1.1 serialVersionUID 
     */
    private static final long serialVersionUID = -483174189758638095L;

    /**
     * Creates a media tracker to track images for a given component.
     * @param     comp the component on which the images 
     *                     will eventually be drawn
     */
    public MediaTracker(Component comp) {
	target = comp;
    }

    /**
     * Adds an image to the list of images being tracked by this media 
     * tracker. The image will eventually be rendered at its default 
     * (unscaled) size. 
     * @param     image   the image to be tracked
     * @param     id      an identifier used to track this image
     */
    public void addImage(Image image, int id) {
	addImage(image, id, -1, -1);
    }

    /**
     * Adds a scaled image to the list of images being tracked  
     * by this media tracker. The image will eventually be 
     * rendered at the indicated width and height.
     *
     * @param     image   the image to be tracked
     * @param     id   an identifier that can be used to track this image
     * @param     w    the width at which the image is rendered
     * @param     h    the height at which the image is rendered
     */
    public synchronized void addImage(Image image, int id, int w, int h) {
	head = MediaEntry.insert(head,
				 new ImageMediaEntry(this, image, id, w, h));
    }

    /**
     * Flag indicating that media is currently being loaded.
     * @see         java.awt.MediaTracker#statusAll
     * @see         java.awt.MediaTracker#statusID
     */
    public static final int LOADING = 1;

    /**
     * Flag indicating that the downloading of media was aborted.
     * @see         java.awt.MediaTracker#statusAll
     * @see         java.awt.MediaTracker#statusID
     */
    public static final int ABORTED = 2;

    /**
     * Flag indicating that the downloading of media encountered 
     * an error.
     * @see         java.awt.MediaTracker#statusAll
     * @see         java.awt.MediaTracker#statusID
     */
    public static final int ERRORED = 4;

    /**
     * Flag indicating that the downloading of media was completed 
     * successfully.
     * @see         java.awt.MediaTracker#statusAll
     * @see         java.awt.MediaTracker#statusID
     */
    public static final int COMPLETE = 8;

    static final int DONE = (ABORTED | ERRORED | COMPLETE);

    /**
     * Checks to see if all images being tracked by this media tracker 
     * have finished loading. 
     * <p>
     * This method does not start loading the images if they are not 
     * already loading. 
     * <p>
     * If there is an error while loading or scaling an image, then that 
     * image is considered to have finished loading. Use the 
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
     * check for errors. 
     * @return      <code>true</code> if all images have finished loading, 
     *                       have been aborted, or have encountered 
     *                       an error; <code>false</code> otherwise
     * @see         java.awt.MediaTracker#checkAll(boolean)
     * @see         java.awt.MediaTracker#checkID
     * @see         java.awt.MediaTracker#isErrorAny
     * @see         java.awt.MediaTracker#isErrorID
     */
    public boolean checkAll() {
	return checkAll(false, true);
    }

    /**
     * Checks to see if all images being tracked by this media tracker 
     * have finished loading. 
     * <p>
     * If the value of the <code>load</code> flag is <code>true</code>, 
     * then this method starts loading any images that are not yet 
     * being loaded. 
     * <p>
     * If there is an error while loading or scaling an image, that 
     * image is considered to have finished loading. Use the 
     * <code>isErrorAny</code> and <code>isErrorID</code> methods to 
     * check for errors. 
     * @param       load   if <code>true</code>, start loading any 
     *                       images that are not yet being loaded
     * @return      <code>true</code> if all images have finished loading, 
     *                       have been aborted, or have encountered 
     *                       an error; <code>false</code> otherwise
     * @see         java.awt.MediaTracker#checkID
     * @see         java.awt.MediaTracker#checkAll()
     * @see         java.awt.MediaTracker#isErrorAny()
     * @see         java.awt.MediaTracker#isErrorID(int)
     */
    public boolean checkAll(boolean load) {
	return checkAll(load, true);
    }

    private synchronized boolean checkAll(boolean load, boolean verify) {
	MediaEntry cur = head;
	boolean done = true;
	while (cur != null) {
	    if ((cur.getStatus(load, verify) & DONE) == 0) {
		done = false;
	    }
	    cur = cur.next;
	}
	return done;
    }

    /**
     * Checks the error status of all of the images.
     * @return   <code>true</code> if any of the images tracked
     *                  by this media tracker had an error during 
     *                  loading; <code>false</code> otherwise
     * @see      java.awt.MediaTracker#isErrorID
     * @see      java.awt.MediaTracker#getErrorsAny
     */
    public synchronized boolean isErrorAny() {
	MediaEntry cur = head;
	while (cur != null) {
	    if ((cur.getStatus(false, true) & ERRORED) != 0) {
		return true;
	    }
	    cur = cur.next;
	}
	return false;
    }

    /**
     * Returns a list of all media that have encountered an error.
     * @return       an array of media objects tracked by this 
     *                        media tracker that have encountered 
     *                        an error, or <code>null</code> if 
     *                        there are none with errors
     * @see          java.awt.MediaTracker#isErrorAny
     * @see          java.awt.MediaTracker#getErrorsID
     */
    public synchronized Object[] getErrorsAny() {
	MediaEntry cur = head;
	int numerrors = 0;
	while (cur != null) {
	    if ((cur.getStatus(false, true) & 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.getStatus(false, false) & ERRORED) != 0) {
		errors[numerrors++] = cur.getMedia();
	    }
	    cur = cur.next;
	}
	return errors;
    }

    /**
     * Starts loading all images tracked by this media tracker. This 
     * method waits until all the images being tracked have finished 
     * loading. 
     * <p>
     * If there is an error while loading or scaling an image, then that 
     * image is considered to have finished loading. Use the 
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
     * check for errors. 
     * @see         java.awt.MediaTracker#waitForID(int)
     * @see         java.awt.MediaTracker#waitForAll(long)
     * @see         java.awt.MediaTracker#isErrorAny
     * @see         java.awt.MediaTracker#isErrorID
     * @exception   InterruptedException  if any thread has 
     *                                     interrupted this thread
     */
    public void waitForAll() throws InterruptedException {
	waitForAll(0);
    }

    /**
     * Starts loading all images tracked by this media tracker. This 
     * method waits until all the images being tracked have finished 
     * loading, or until the length of time specified in milliseconds  
     * by the <code>ms</code> argument has passed. 
     * <p>
     * If there is an error while loading or scaling an image, then  
     * that image is considered to have finished loading. Use the 
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
     * check for errors. 
     * @param       ms       the number of milliseconds to wait 
     *                       for the loading to complete
     * @return      <code>true</code> if all images were successfully 
     *                       loaded; <code>false</code> otherwise
     * @see         java.awt.MediaTracker#waitForID(int)
     * @see         java.awt.MediaTracker#waitForAll(long)
     * @see         java.awt.MediaTracker#isErrorAny
     * @see         java.awt.MediaTracker#isErrorID
     * @exception   InterruptedException  if any 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, 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);
	}
    }

    /**
     * Calculates and returns the bitwise inclusive <b>OR</b> of the 
     * status of all media that are tracked by this media tracker. 
     * <p>
     * Possible flags defined by the 
     * <code>MediaTracker</code> class are <code>LOADING</code>, 
     * <code>ABORTED</code>, <code>ERRORED</code>, and 
     * <code>COMPLETE</code>. An image that hasn't started 
     * loading has zero as its status. 
     * <p>
     * If the value of <code>load</code> is <code>true</code>, then
     * this method starts loading any images that are not yet being loaded. 
     *
     * @param        load   if <code>true</code>, start loading 
     *                            any images that are not yet being loaded
     * @return       the bitwise inclusive <b>OR</b> of the status of 
     *                            all of the media being tracked
     * @see          java.awt.MediaTracker#statusID(int, boolean)
     * @see          java.awt.MediaTracker#LOADING
     * @see          java.awt.MediaTracker#ABORTED
     * @see          java.awt.MediaTracker#ERRORED
     * @see          java.awt.MediaTracker#COMPLETE
     */
    public int statusAll(boolean load) {
	return statusAll(load, true);
    }

    private synchronized int statusAll(boolean load, boolean verify) {
	MediaEntry cur = head;
	int status = 0;
	while (cur != null) {
	    status = status | cur.getStatus(load, verify);
	    cur = cur.next;
	}
	return status;
    }

    /**
     * Checks to see if all images tracked by this media tracker that 
     * are tagged with the specified identifier have finished loading. 
     * <p>
     * This method does not start loading the images if they are not 
     * already loading. 
     * <p>
     * If there is an error while loading or scaling an image, then that 
     * image is considered to have finished loading. Use the 
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
     * check for errors. 
     * @param       id   the identifier of the images to check
     * @return      <code>true</code> if all images have finished loading, 
     *                       have been aborted, or have encountered 
     *                       an error; <code>false</code> otherwise
     * @see         java.awt.MediaTracker#checkID(int, boolean)
     * @see         java.awt.MediaTracker#checkAll()
     * @see         java.awt.MediaTracker#isErrorAny()
     * @see         java.awt.MediaTracker#isErrorID(int)
     */
    public boolean checkID(int id) {
	return checkID(id, false, true);
    }

    /**
     * Checks to see if all images tracked by this media tracker that 
     * are tagged with the specified identifier have finished loading. 
     * <p>
     * If the value of the <code>load</code> flag is <code>true</code>, 
     * then this method starts loading any images that are not yet 
     * being loaded. 
     * <p>
     * If there is an error while loading or scaling an image, then that 
     * image is considered to have finished loading. Use the 
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
     * check for errors. 
     * @param       id       the identifier of the images to check
     * @param       load     if <code>true</code>, start loading any 
     *                       images that are not yet being loaded
     * @return      <code>true</code> if all images have finished loading, 
     *                       have been aborted, or have encountered 
     *                       an error; <code>false</code> otherwise
     * @see         java.awt.MediaTracker#checkID(int, boolean)
     * @see         java.awt.MediaTracker#checkAll()
     * @see         java.awt.MediaTracker#isErrorAny()
     * @see         java.awt.MediaTracker#isErrorID(int)
     */
    public boolean checkID(int id, boolean load) {
	return checkID(id, load, true);
    }

    private synchronized boolean checkID(int id, boolean load, boolean verify)
    {
	MediaEntry cur = head;
	boolean done = true;
	while (cur != null) {
	    if (cur.getID() == id
		&& (cur.getStatus(load, verify) & DONE) == 0)
	    {
		done = false;
	    }
	    cur = cur.next;
	}
	return done;
    }

    /**
     * Checks the error status of all of the images tracked by this 
     * media tracker with the specified identifier. 
     * @param        id   the identifier of the images to check
     * @return       <code>true</code> if any of the images with the 
     *                          specified identifier had an error during 
     *                          loading; <code>false</code> otherwise
     * @see          java.awt.MediaTracker#isErrorAny
     * @see          java.awt.MediaTracker#getErrorsID
     */
    public synchronized boolean isErrorID(int id) {
	MediaEntry cur = head;
	while (cur != null) {
	    if (cur.getID() == id
		&& (cur.getStatus(false, true) & 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 of the images to check
     * @return      an array of media objects tracked by this media 
     *                       tracker with the specified identifier 
     *                       that have encountered an error, or 
     *                       <code>null</code> if there are none with errors
     * @see         java.awt.MediaTracker#isErrorID
     * @see         java.awt.MediaTracker#isErrorAny
     * @see         java.awt.MediaTracker#getErrorsAny
     */
    public synchronized Object[] getErrorsID(int id) {
	MediaEntry cur = head;
	int numerrors = 0;
	while (cur != null) {
	    if (cur.getID() == id
		&& (cur.getStatus(false, true) & 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, false) & ERRORED) != 0)
	    {
		errors[numerrors++] = cur.getMedia();
	    }
	    cur = cur.next;
	}
	return errors;
    }

    /**
     * Starts loading all images tracked by this media tracker with the 
     * specified identifier. This method waits until all the images with 
     * the specified identifier have finished loading. 
     * <p>
     * If there is an error while loading or scaling an image, then that 
     * image is considered to have finished loading. Use the 
     * <code>isErrorAny</code> and <code>isErrorID</code> methods to 
     * check for errors. 
     * @param         id   the identifier of the images to check
     * @see           java.awt.MediaTracker#waitForAll
     * @see           java.awt.MediaTracker#isErrorAny()
     * @see           java.awt.MediaTracker#isErrorID(int)
     * @exception     InterruptedException  if any thread has 
     *                          interrupted this thread.
     */
    public void waitForID(int id) throws InterruptedException {
	waitForID(id, 0);
    }

    /**
     * Starts loading all images tracked by this media tracker with the 
     * specified identifier. This method waits until all the images with 
     * the specified identifier have finished loading, or until the 
     * length of time specified in milliseconds by the <code>ms</code> 
     * argument has passed. 
     * <p>
     * If there is an error while loading or scaling an image, then that 
     * image is considered to have finished loading. Use the 
     * <code>statusID</code>, <code>isErrorID</code>, and
     * <code>isErrorAny</code> methods to check for errors. 
     * @param         id   the identifier of the images to check
     * @param         ms   the length of time, in milliseconds, to wait 
     *                           for the loading to complete
     * @see           java.awt.MediaTracker#waitForAll
     * @see           java.awt.MediaTracker#waitForID(int)
     * @see           java.awt.MediaTracker#statusID
     * @see           java.awt.MediaTracker#isErrorAny()
     * @see           java.awt.MediaTracker#isErrorID(int)
     * @exception     InterruptedException  if any 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, 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);
	}
    }

    /**
     * Calculates and returns the bitwise inclusive <b>OR</b> of the 
     * status of all media with the specified identifier that are 
     * tracked by this media tracker. 
     * <p>
     * Possible flags defined by the 
     * <code>MediaTracker</code> class are <code>LOADING</code>, 
     * <code>ABORTED</code>, <code>ERRORED</code>, and 
     * <code>COMPLETE</code>. An image that hasn't started 
     * loading has zero as its status. 
     * <p>
     * If the value of <code>load</code> is <code>true</code>, then
     * this method starts loading any images that are not yet being loaded. 
     * @param        id   the identifier of the images to check
     * @param        load   if <code>true</code>, start loading 
     *                            any images that are not yet being loaded
     * @return       the bitwise inclusive <b>OR</b> of the status of 
     *                            all of the media with the specified
     *                            identifier that are being tracked
     * @see          java.awt.MediaTracker#statusAll(boolean)
     * @see          java.awt.MediaTracker#LOADING
     * @see          java.awt.MediaTracker#ABORTED
     * @see          java.awt.MediaTracker#ERRORED
     * @see          java.awt.MediaTracker#COMPLETE
     */
    public int statusID(int id, boolean load) {
	return statusID(id, load, true);
    }

    private synchronized int statusID(int id, boolean load, boolean verify) {
	MediaEntry cur = head;
	int status = 0;
	while (cur != null) {
	    if (cur.getID() == id) {
		status = status | cur.getStatus(load, verify);
	    }
	    cur = cur.next;
	}
	return status;
    }

    /**
     * Removes the specified image from this media tracker.
     * All instances of the specified image are removed, 
     * regardless of scale or ID.
     * @param   image     the image to be removed
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int)
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
     * @since   JDK1.1
     */
    public synchronized void removeImage(Image image) {
	MediaEntry cur = head;
	MediaEntry prev = null;
	while (cur != null) {
	    MediaEntry next = cur.next;
	    if (cur.getMedia() == image) {
		if (prev == null) {
		    head = next;
		} else {
		    prev.next = next;
		}
		cur.cancel();
	    } else {
		prev = cur;
	    }
	    cur = next;
	}
	notifyAll();	// Notify in case remaining images are "done".
    }

    /**
     * Removes the specified image from the specified tracking 
     * ID of this media tracker.
     * All instances of <code>Image</code> being tracked 
     * under the specified ID are removed regardless of scale.
     * @param      image the image to be removed
     * @param      id the tracking ID frrom which to remove the image
     * @see        java.awt.MediaTracker#removeImage(java.awt.Image)
     * @see        java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
     * @since      JDK1.1
     */
    public synchronized void removeImage(Image image, int id) {
	MediaEntry cur = head;
	MediaEntry prev = null;
	while (cur != null) {
	    MediaEntry next = cur.next;
	    if (cur.getID() == id && cur.getMedia() == image) {
		if (prev == null) {
		    head = next;
		} else {
		    prev.next = next;
		}
		cur.cancel();
	    } else {
		prev = cur;
	    }
	    cur = next;
	}
	notifyAll();	// Notify in case remaining images are "done".
    }

    /**
     * Removes the specified image with the specified 
     * width, height, and ID from this media tracker.
     * Only the specified instance (with any duplicates) is removed.
     * @param   image the image to be removed
     * @param   id the tracking ID from which to remove the image
     * @param   width the width to remove (-1 for unscaled)
     * @param   height the height to remove (-1 for unscaled)
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image)
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int)
     * @since   JDK1.1
     */
    public synchronized void removeImage(Image image, int id,
					 int width, int height) {
	MediaEntry cur = head;
	MediaEntry prev = null;
	while (cur != null) {
	    MediaEntry next = cur.next;
	    if (cur.getID() == id && cur instanceof ImageMediaEntry
		&& ((ImageMediaEntry) cur).matches(image, width, height))
	    {
		if (prev == null) {
		    head = next;
		} else {
		    prev.next = next;
		}
		cur.cancel();
	    } else {
		prev = cur;
	    }
	    cur = next;
	}
	notifyAll();	// Notify in case remaining images are "done".
    }

    synchronized void setDone() {
	notifyAll();
    }
}

abstract class MediaEntry {
    MediaTracker tracker;
    int ID;
    MediaEntry next;

    int status;
    boolean cancelled;

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

    void cancel() {
	cancelled = true;
    }

    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, boolean doVerify) {
	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, 
java.io.Serializable {
    Image image;
    int width;
    int height;

    /*
     * JDK 1.1 serialVersionUID 
     */
    private static final long serialVersionUID = 4739377000350280650L;
    
    ImageMediaEntry(MediaTracker mt, Image img, int c, int w, int h) {
	super(mt, c);
	image = img;
	width = w;
	height = h;
    }

    boolean matches(Image img, int w, int h) {
	return (image == img && width == w && height == h);
    }

    Object getMedia() {
	return image;
    }

    int getStatus(boolean doLoad, boolean doVerify) {
	if (doVerify) {
	    int flags = tracker.target.checkImage(image, width, height, null);
	    int s = parseflags(flags);
	    if (s == 0) {
		if ((status & (ERRORED | COMPLETE)) != 0) {
		    setStatus(ABORTED);
		}
	    } else if (s != status) {
		setStatus(s);
	    }
	}
	return super.getStatus(doLoad, doVerify);
    }

    void startLoad() {
	if (tracker.target.prepareImage(image, width, height, this)) {
	    setStatus(COMPLETE);
	}
    }

    int parseflags(int infoflags) {
	if ((infoflags & ERROR) != 0) {
	    return ERRORED;
	} else if ((infoflags & ABORT) != 0) {
	    return ABORTED;
	} else if ((infoflags & (ALLBITS | FRAMEBITS)) != 0) {
	    return COMPLETE;
	}
	return 0;
    }

    public boolean imageUpdate(Image img, int infoflags,
			       int x, int y, int w, int h) {
	if (cancelled) {
	    return false;
	}
	int s = parseflags(infoflags);
	if (s != 0 && s != status) {
	    setStatus(s);
	}
	return ((status & LOADING) != 0);
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar