147 * // loading the images. Otherwise always paint the
148 * // background so that it appears incrementally as it
149 * // is loading. Finally, only paint the current animation
150 * // frame if all of the frames (id == 1) are done loading,
151 * // so that we don't get partial animations.
152 * public void paint(Graphics g) {
153 * if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
154 * g.setColor(Color.red);
155 * g.fillRect(0, 0, size().width, size().height);
156 * return;
157 * }
158 * g.drawImage(bg, 0, 0, this);
159 * if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
160 * g.drawImage(anim[index], 10, 10, this);
161 * }
162 * }
163 * }
164 * } </pre></blockquote><hr>
165 *
166 * @author Jim Graham
167 * @since JDK1.0
168 */
169 public class MediaTracker implements java.io.Serializable {
170
171 /**
172 * A given <code>Component</code> that will be
173 * tracked by a media tracker where the image will
174 * eventually be drawn.
175 *
176 * @serial
177 * @see #MediaTracker(Component)
178 */
179 Component target;
180 /**
181 * The head of the list of <code>Images</code> that is being
182 * tracked by the <code>MediaTracker</code>.
183 *
184 * @serial
185 * @see #addImage(Image, int)
186 * @see #removeImage(Image)
187 */
707
708 private synchronized int statusID(int id, boolean load, boolean verify) {
709 MediaEntry cur = head;
710 int status = 0;
711 while (cur != null) {
712 if (cur.getID() == id) {
713 status = status | cur.getStatus(load, verify);
714 }
715 cur = cur.next;
716 }
717 return status;
718 }
719
720 /**
721 * Removes the specified image from this media tracker.
722 * All instances of the specified image are removed,
723 * regardless of scale or ID.
724 * @param image the image to be removed
725 * @see java.awt.MediaTracker#removeImage(java.awt.Image, int)
726 * @see java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
727 * @since JDK1.1
728 */
729 public synchronized void removeImage(Image image) {
730 removeImageImpl(image);
731 Image rvImage = getResolutionVariant(image);
732 if (rvImage != null) {
733 removeImageImpl(rvImage);
734 }
735 notifyAll(); // Notify in case remaining images are "done".
736 }
737
738 private void removeImageImpl(Image image) {
739 MediaEntry cur = head;
740 MediaEntry prev = null;
741 while (cur != null) {
742 MediaEntry next = cur.next;
743 if (cur.getMedia() == image) {
744 if (prev == null) {
745 head = next;
746 } else {
747 prev.next = next;
748 }
749 cur.cancel();
750 } else {
751 prev = cur;
752 }
753 cur = next;
754 }
755 }
756
757 /**
758 * Removes the specified image from the specified tracking
759 * ID of this media tracker.
760 * All instances of <code>Image</code> being tracked
761 * under the specified ID are removed regardless of scale.
762 * @param image the image to be removed
763 * @param id the tracking ID from which to remove the image
764 * @see java.awt.MediaTracker#removeImage(java.awt.Image)
765 * @see java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
766 * @since JDK1.1
767 */
768 public synchronized void removeImage(Image image, int id) {
769 removeImageImpl(image, id);
770 Image rvImage = getResolutionVariant(image);
771 if (rvImage != null) {
772 removeImageImpl(rvImage, id);
773 }
774 notifyAll(); // Notify in case remaining images are "done".
775 }
776
777 private void removeImageImpl(Image image, int id) {
778 MediaEntry cur = head;
779 MediaEntry prev = null;
780 while (cur != null) {
781 MediaEntry next = cur.next;
782 if (cur.getID() == id && cur.getMedia() == image) {
783 if (prev == null) {
784 head = next;
785 } else {
786 prev.next = next;
787 }
788 cur.cancel();
789 } else {
790 prev = cur;
791 }
792 cur = next;
793 }
794 }
795
796 /**
797 * Removes the specified image with the specified
798 * width, height, and ID from this media tracker.
799 * Only the specified instance (with any duplicates) is removed.
800 * @param image the image to be removed
801 * @param id the tracking ID from which to remove the image
802 * @param width the width to remove (-1 for unscaled)
803 * @param height the height to remove (-1 for unscaled)
804 * @see java.awt.MediaTracker#removeImage(java.awt.Image)
805 * @see java.awt.MediaTracker#removeImage(java.awt.Image, int)
806 * @since JDK1.1
807 */
808 public synchronized void removeImage(Image image, int id,
809 int width, int height) {
810 removeImageImpl(image, id, width, height);
811 Image rvImage = getResolutionVariant(image);
812 if (rvImage != null) {
813 removeImageImpl(rvImage, id, 2 * width, 2 * height);
814
815 }
816 notifyAll(); // Notify in case remaining images are "done".
817 }
818
819 private void removeImageImpl(Image image, int id, int width, int height) {
820 MediaEntry cur = head;
821 MediaEntry prev = null;
822 while (cur != null) {
823 MediaEntry next = cur.next;
824 if (cur.getID() == id && cur instanceof ImageMediaEntry
825 && ((ImageMediaEntry) cur).matches(image, width, height))
826 {
|
147 * // loading the images. Otherwise always paint the
148 * // background so that it appears incrementally as it
149 * // is loading. Finally, only paint the current animation
150 * // frame if all of the frames (id == 1) are done loading,
151 * // so that we don't get partial animations.
152 * public void paint(Graphics g) {
153 * if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
154 * g.setColor(Color.red);
155 * g.fillRect(0, 0, size().width, size().height);
156 * return;
157 * }
158 * g.drawImage(bg, 0, 0, this);
159 * if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
160 * g.drawImage(anim[index], 10, 10, this);
161 * }
162 * }
163 * }
164 * } </pre></blockquote><hr>
165 *
166 * @author Jim Graham
167 * @since 1.0
168 */
169 public class MediaTracker implements java.io.Serializable {
170
171 /**
172 * A given <code>Component</code> that will be
173 * tracked by a media tracker where the image will
174 * eventually be drawn.
175 *
176 * @serial
177 * @see #MediaTracker(Component)
178 */
179 Component target;
180 /**
181 * The head of the list of <code>Images</code> that is being
182 * tracked by the <code>MediaTracker</code>.
183 *
184 * @serial
185 * @see #addImage(Image, int)
186 * @see #removeImage(Image)
187 */
707
708 private synchronized int statusID(int id, boolean load, boolean verify) {
709 MediaEntry cur = head;
710 int status = 0;
711 while (cur != null) {
712 if (cur.getID() == id) {
713 status = status | cur.getStatus(load, verify);
714 }
715 cur = cur.next;
716 }
717 return status;
718 }
719
720 /**
721 * Removes the specified image from this media tracker.
722 * All instances of the specified image are removed,
723 * regardless of scale or ID.
724 * @param image the image to be removed
725 * @see java.awt.MediaTracker#removeImage(java.awt.Image, int)
726 * @see java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
727 * @since 1.1
728 */
729 public synchronized void removeImage(Image image) {
730 removeImageImpl(image);
731 Image rvImage = getResolutionVariant(image);
732 if (rvImage != null) {
733 removeImageImpl(rvImage);
734 }
735 notifyAll(); // Notify in case remaining images are "done".
736 }
737
738 private void removeImageImpl(Image image) {
739 MediaEntry cur = head;
740 MediaEntry prev = null;
741 while (cur != null) {
742 MediaEntry next = cur.next;
743 if (cur.getMedia() == image) {
744 if (prev == null) {
745 head = next;
746 } else {
747 prev.next = next;
748 }
749 cur.cancel();
750 } else {
751 prev = cur;
752 }
753 cur = next;
754 }
755 }
756
757 /**
758 * Removes the specified image from the specified tracking
759 * ID of this media tracker.
760 * All instances of <code>Image</code> being tracked
761 * under the specified ID are removed regardless of scale.
762 * @param image the image to be removed
763 * @param id the tracking ID from which to remove the image
764 * @see java.awt.MediaTracker#removeImage(java.awt.Image)
765 * @see java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
766 * @since 1.1
767 */
768 public synchronized void removeImage(Image image, int id) {
769 removeImageImpl(image, id);
770 Image rvImage = getResolutionVariant(image);
771 if (rvImage != null) {
772 removeImageImpl(rvImage, id);
773 }
774 notifyAll(); // Notify in case remaining images are "done".
775 }
776
777 private void removeImageImpl(Image image, int id) {
778 MediaEntry cur = head;
779 MediaEntry prev = null;
780 while (cur != null) {
781 MediaEntry next = cur.next;
782 if (cur.getID() == id && cur.getMedia() == image) {
783 if (prev == null) {
784 head = next;
785 } else {
786 prev.next = next;
787 }
788 cur.cancel();
789 } else {
790 prev = cur;
791 }
792 cur = next;
793 }
794 }
795
796 /**
797 * Removes the specified image with the specified
798 * width, height, and ID from this media tracker.
799 * Only the specified instance (with any duplicates) is removed.
800 * @param image the image to be removed
801 * @param id the tracking ID from which to remove the image
802 * @param width the width to remove (-1 for unscaled)
803 * @param height the height to remove (-1 for unscaled)
804 * @see java.awt.MediaTracker#removeImage(java.awt.Image)
805 * @see java.awt.MediaTracker#removeImage(java.awt.Image, int)
806 * @since 1.1
807 */
808 public synchronized void removeImage(Image image, int id,
809 int width, int height) {
810 removeImageImpl(image, id, width, height);
811 Image rvImage = getResolutionVariant(image);
812 if (rvImage != null) {
813 removeImageImpl(rvImage, id, 2 * width, 2 * height);
814
815 }
816 notifyAll(); // Notify in case remaining images are "done".
817 }
818
819 private void removeImageImpl(Image image, int id, int width, int height) {
820 MediaEntry cur = head;
821 MediaEntry prev = null;
822 while (cur != null) {
823 MediaEntry next = cur.next;
824 if (cur.getID() == id && cur instanceof ImageMediaEntry
825 && ((ImageMediaEntry) cur).matches(image, width, height))
826 {
|