jdk/src/share/classes/java/awt/MediaTracker.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.ImageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The <code>MediaTracker</code> class is a utility class to track
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * the status of a number of media objects. Media objects could
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * include audio clips as well as images, though currently only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * images are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * To use a media tracker, create an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>MediaTracker</code> and call its <code>addImage</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * method for each image to be tracked. In addition, each image can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * be assigned a unique identifier. This identifier controls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * priority order in which the images are fetched. It can also be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * to identify unique subsets of the images that can be waited on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * independently. Images with a lower ID are loaded in preference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * those with a higher ID number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Tracking an animated image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * might not always be useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * due to the multi-part nature of animated image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * loading and painting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * but it is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <code>MediaTracker</code> treats an animated image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * as completely loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * when the first frame is completely loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * At that point, the <code>MediaTracker</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * signals any waiters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * that the image is completely loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * If no <code>ImageObserver</code>s are observing the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * when the first frame has finished loading,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the image might flush itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * to conserve resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * (see {@link Image#flush()}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Here is an example of using <code>MediaTracker</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <hr><blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * import java.applet.Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * import java.awt.MediaTracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * public class ImageBlaster extends Applet implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      MediaTracker tracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      Image bg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      Image anim[] = new Image[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      Thread animator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      // Get the images for the background (id == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      // and the animation frames (id == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *      // and add them to the MediaTracker
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *      public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *          tracker = new MediaTracker(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *          bg = getImage(getDocumentBase(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *                  "images/background.gif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *          tracker.addImage(bg, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *          for (int i = 0; i < 5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *              anim[i] = getImage(getDocumentBase(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *                      "images/anim"+i+".gif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *              tracker.addImage(anim[i], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *      // Start the animation thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *      public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *          animator = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *          animator.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *      // Stop the animation thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *      public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *          animator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *      // Run the animation thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *      // First wait for the background image to fully load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *      // and paint.  Then wait for all of the animation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *      // frames to finish loading. Finally, loop and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *      // increment the animation frame index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *      public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *          try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *              tracker.waitForID(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *              tracker.waitForID(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *          } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *          Thread me = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *          while (animator == me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *              try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *                  Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *              } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *              synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *                  index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *                  if (index >= anim.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *                      index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *              repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *      // The background image fills the frame so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *      // don't need to clear the applet on repaints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *      // Just call the paint method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *      public void update(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *          paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *      // Paint a large red rectangle if there are any errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *      // loading the images.  Otherwise always paint the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *      // background so that it appears incrementally as it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *      // is loading.  Finally, only paint the current animation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *      // frame if all of the frames (id == 1) are done loading,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *      // so that we don't get partial animations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *      public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *          if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *              g.setColor(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *              g.fillRect(0, 0, size().width, size().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *          g.drawImage(bg, 0, 0, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *          if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *              g.drawImage(anim[index], 10, 10, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * </pre></blockquote><hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * @since       JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
public class MediaTracker implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * A given <code>Component</code> that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * tracked by a media tracker where the image will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * eventually be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see #MediaTracker(Component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    Component target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * The head of the list of <code>Images</code> that is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * tracked by the <code>MediaTracker</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see #addImage(Image, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see #removeImage(Image)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    MediaEntry head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private static final long serialVersionUID = -483174189758638095L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Creates a media tracker to track images for a given component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param     comp the component on which the images
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *                     will eventually be drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public MediaTracker(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        target = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Adds an image to the list of images being tracked by this media
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * tracker. The image will eventually be rendered at its default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * (unscaled) size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param     image   the image to be tracked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param     id      an identifier used to track this image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public void addImage(Image image, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        addImage(image, id, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Adds a scaled image to the list of images being tracked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * by this media tracker. The image will eventually be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * rendered at the indicated width and height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param     image   the image to be tracked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param     id   an identifier that can be used to track this image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param     w    the width at which the image is rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param     h    the height at which the image is rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public synchronized void addImage(Image image, int id, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        head = MediaEntry.insert(head,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                 new ImageMediaEntry(this, image, id, w, h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Flag indicating that media is currently being loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @see         java.awt.MediaTracker#statusAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @see         java.awt.MediaTracker#statusID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public static final int LOADING = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Flag indicating that the downloading of media was aborted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @see         java.awt.MediaTracker#statusAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @see         java.awt.MediaTracker#statusID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static final int ABORTED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Flag indicating that the downloading of media encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @see         java.awt.MediaTracker#statusAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see         java.awt.MediaTracker#statusID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public static final int ERRORED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Flag indicating that the downloading of media was completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * successfully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @see         java.awt.MediaTracker#statusAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @see         java.awt.MediaTracker#statusID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public static final int COMPLETE = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    static final int DONE = (ABORTED | ERRORED | COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Checks to see if all images being tracked by this media tracker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * have finished loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * This method does not start loading the images if they are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * already loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * If there is an error while loading or scaling an image, then that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * image is considered to have finished loading. Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * check for errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return      <code>true</code> if all images have finished loading,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *                       have been aborted, or have encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *                       an error; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see         java.awt.MediaTracker#checkAll(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @see         java.awt.MediaTracker#checkID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see         java.awt.MediaTracker#isErrorAny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @see         java.awt.MediaTracker#isErrorID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public boolean checkAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return checkAll(false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Checks to see if all images being tracked by this media tracker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * have finished loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * If the value of the <code>load</code> flag is <code>true</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * then this method starts loading any images that are not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * being loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * If there is an error while loading or scaling an image, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * image is considered to have finished loading. Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <code>isErrorAny</code> and <code>isErrorID</code> methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * check for errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param       load   if <code>true</code>, start loading any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *                       images that are not yet being loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return      <code>true</code> if all images have finished loading,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *                       have been aborted, or have encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *                       an error; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @see         java.awt.MediaTracker#checkID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @see         java.awt.MediaTracker#checkAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see         java.awt.MediaTracker#isErrorAny()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see         java.awt.MediaTracker#isErrorID(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public boolean checkAll(boolean load) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return checkAll(load, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private synchronized boolean checkAll(boolean load, boolean verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        boolean done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if ((cur.getStatus(load, verify) & DONE) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Checks the error status of all of the images.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @return   <code>true</code> if any of the images tracked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *                  by this media tracker had an error during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *                  loading; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @see      java.awt.MediaTracker#isErrorID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see      java.awt.MediaTracker#getErrorsAny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public synchronized boolean isErrorAny() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if ((cur.getStatus(false, true) & ERRORED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Returns a list of all media that have encountered an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return       an array of media objects tracked by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *                        media tracker that have encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *                        an error, or <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *                        there are none with errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see          java.awt.MediaTracker#isErrorAny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see          java.awt.MediaTracker#getErrorsID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public synchronized Object[] getErrorsAny() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        int numerrors = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if ((cur.getStatus(false, true) & ERRORED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                numerrors++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (numerrors == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        Object errors[] = new Object[numerrors];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        numerrors = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if ((cur.getStatus(false, false) & ERRORED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                errors[numerrors++] = cur.getMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return errors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Starts loading all images tracked by this media tracker. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * method waits until all the images being tracked have finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * If there is an error while loading or scaling an image, then that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * image is considered to have finished loading. Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * check for errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see         java.awt.MediaTracker#waitForID(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @see         java.awt.MediaTracker#waitForAll(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @see         java.awt.MediaTracker#isErrorAny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @see         java.awt.MediaTracker#isErrorID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @exception   InterruptedException  if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *                                     interrupted this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public void waitForAll() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        waitForAll(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Starts loading all images tracked by this media tracker. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * method waits until all the images being tracked have finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * loading, or until the length of time specified in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * by the <code>ms</code> argument has passed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * If there is an error while loading or scaling an image, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * that image is considered to have finished loading. Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * check for errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param       ms       the number of milliseconds to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *                       for the loading to complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @return      <code>true</code> if all images were successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *                       loaded; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @see         java.awt.MediaTracker#waitForID(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @see         java.awt.MediaTracker#waitForAll(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @see         java.awt.MediaTracker#isErrorAny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see         java.awt.MediaTracker#isErrorID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @exception   InterruptedException  if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *                                     interrupted this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public synchronized boolean waitForAll(long ms)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        long end = System.currentTimeMillis() + ms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            int status = statusAll(first, first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if ((status & LOADING) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                return (status == COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            long timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (ms == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                timeout = end - System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            wait(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Calculates and returns the bitwise inclusive <b>OR</b> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * status of all media that are tracked by this media tracker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Possible flags defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * <code>MediaTracker</code> class are <code>LOADING</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <code>ABORTED</code>, <code>ERRORED</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <code>COMPLETE</code>. An image that hasn't started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * loading has zero as its status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * If the value of <code>load</code> is <code>true</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * this method starts loading any images that are not yet being loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param        load   if <code>true</code>, start loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *                            any images that are not yet being loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @return       the bitwise inclusive <b>OR</b> of the status of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *                            all of the media being tracked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @see          java.awt.MediaTracker#statusID(int, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @see          java.awt.MediaTracker#LOADING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @see          java.awt.MediaTracker#ABORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @see          java.awt.MediaTracker#ERRORED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @see          java.awt.MediaTracker#COMPLETE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public int statusAll(boolean load) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return statusAll(load, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private synchronized int statusAll(boolean load, boolean verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        int status = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            status = status | cur.getStatus(load, verify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Checks to see if all images tracked by this media tracker that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * are tagged with the specified identifier have finished loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * This method does not start loading the images if they are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * already loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * If there is an error while loading or scaling an image, then that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * image is considered to have finished loading. Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * check for errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param       id   the identifier of the images to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return      <code>true</code> if all images have finished loading,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *                       have been aborted, or have encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *                       an error; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @see         java.awt.MediaTracker#checkID(int, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @see         java.awt.MediaTracker#checkAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @see         java.awt.MediaTracker#isErrorAny()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @see         java.awt.MediaTracker#isErrorID(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public boolean checkID(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        return checkID(id, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Checks to see if all images tracked by this media tracker that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * are tagged with the specified identifier have finished loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * If the value of the <code>load</code> flag is <code>true</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * then this method starts loading any images that are not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * being loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * If there is an error while loading or scaling an image, then that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * image is considered to have finished loading. Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <code>isErrorAny</code> or <code>isErrorID</code> methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * check for errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param       id       the identifier of the images to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @param       load     if <code>true</code>, start loading any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *                       images that are not yet being loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @return      <code>true</code> if all images have finished loading,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *                       have been aborted, or have encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *                       an error; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @see         java.awt.MediaTracker#checkID(int, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @see         java.awt.MediaTracker#checkAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @see         java.awt.MediaTracker#isErrorAny()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @see         java.awt.MediaTracker#isErrorID(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public boolean checkID(int id, boolean load) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return checkID(id, load, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    private synchronized boolean checkID(int id, boolean load, boolean verify)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        boolean done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            if (cur.getID() == id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                && (cur.getStatus(load, verify) & DONE) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Checks the error status of all of the images tracked by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * media tracker with the specified identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @param        id   the identifier of the images to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @return       <code>true</code> if any of the images with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *                          specified identifier had an error during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *                          loading; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @see          java.awt.MediaTracker#isErrorAny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @see          java.awt.MediaTracker#getErrorsID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    public synchronized boolean isErrorID(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (cur.getID() == id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                && (cur.getStatus(false, true) & ERRORED) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Returns a list of media with the specified ID that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * have encountered an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @param       id   the identifier of the images to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @return      an array of media objects tracked by this media
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *                       tracker with the specified identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *                       that have encountered an error, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *                       <code>null</code> if there are none with errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @see         java.awt.MediaTracker#isErrorID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @see         java.awt.MediaTracker#isErrorAny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @see         java.awt.MediaTracker#getErrorsAny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public synchronized Object[] getErrorsID(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        int numerrors = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (cur.getID() == id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                && (cur.getStatus(false, true) & ERRORED) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                numerrors++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (numerrors == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        Object errors[] = new Object[numerrors];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        numerrors = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (cur.getID() == id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                && (cur.getStatus(false, false) & ERRORED) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                errors[numerrors++] = cur.getMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return errors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Starts loading all images tracked by this media tracker with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * specified identifier. This method waits until all the images with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * the specified identifier have finished loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * If there is an error while loading or scaling an image, then that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * image is considered to have finished loading. Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <code>isErrorAny</code> and <code>isErrorID</code> methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * check for errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @param         id   the identifier of the images to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @see           java.awt.MediaTracker#waitForAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @see           java.awt.MediaTracker#isErrorAny()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @see           java.awt.MediaTracker#isErrorID(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @exception     InterruptedException  if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *                          interrupted this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    public void waitForID(int id) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        waitForID(id, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * Starts loading all images tracked by this media tracker with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * specified identifier. This method waits until all the images with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * the specified identifier have finished loading, or until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * length of time specified in milliseconds by the <code>ms</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * argument has passed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * If there is an error while loading or scaling an image, then that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * image is considered to have finished loading. Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <code>statusID</code>, <code>isErrorID</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <code>isErrorAny</code> methods to check for errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @param         id   the identifier of the images to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param         ms   the length of time, in milliseconds, to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *                           for the loading to complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @see           java.awt.MediaTracker#waitForAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @see           java.awt.MediaTracker#waitForID(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @see           java.awt.MediaTracker#statusID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @see           java.awt.MediaTracker#isErrorAny()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @see           java.awt.MediaTracker#isErrorID(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @exception     InterruptedException  if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *                          interrupted this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public synchronized boolean waitForID(int id, long ms)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        long end = System.currentTimeMillis() + ms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            int status = statusID(id, first, first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            if ((status & LOADING) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                return (status == COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            long timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (ms == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                timeout = end - System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            wait(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * Calculates and returns the bitwise inclusive <b>OR</b> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * status of all media with the specified identifier that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * tracked by this media tracker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * Possible flags defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * <code>MediaTracker</code> class are <code>LOADING</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * <code>ABORTED</code>, <code>ERRORED</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <code>COMPLETE</code>. An image that hasn't started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * loading has zero as its status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * If the value of <code>load</code> is <code>true</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * this method starts loading any images that are not yet being loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @param        id   the identifier of the images to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @param        load   if <code>true</code>, start loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *                            any images that are not yet being loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @return       the bitwise inclusive <b>OR</b> of the status of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *                            all of the media with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *                            identifier that are being tracked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @see          java.awt.MediaTracker#statusAll(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @see          java.awt.MediaTracker#LOADING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @see          java.awt.MediaTracker#ABORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @see          java.awt.MediaTracker#ERRORED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @see          java.awt.MediaTracker#COMPLETE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public int statusID(int id, boolean load) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        return statusID(id, load, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    private synchronized int statusID(int id, boolean load, boolean verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        int status = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            if (cur.getID() == id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                status = status | cur.getStatus(load, verify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        return status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * Removes the specified image from this media tracker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * All instances of the specified image are removed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * regardless of scale or ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @param   image     the image to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    public synchronized void removeImage(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        MediaEntry prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            MediaEntry next = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            if (cur.getMedia() == image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    head = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    prev.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                cur.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                prev = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            cur = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        notifyAll();    // Notify in case remaining images are "done".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * Removes the specified image from the specified tracking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * ID of this media tracker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * All instances of <code>Image</code> being tracked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * under the specified ID are removed regardless of scale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @param      image the image to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @param      id the tracking ID frrom which to remove the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @see        java.awt.MediaTracker#removeImage(java.awt.Image)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @see        java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public synchronized void removeImage(Image image, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        MediaEntry prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            MediaEntry next = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            if (cur.getID() == id && cur.getMedia() == image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    head = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    prev.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                cur.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                prev = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            cur = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        notifyAll();    // Notify in case remaining images are "done".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Removes the specified image with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * width, height, and ID from this media tracker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Only the specified instance (with any duplicates) is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @param   image the image to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @param   id the tracking ID from which to remove the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @param   width the width to remove (-1 for unscaled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @param   height the height to remove (-1 for unscaled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    public synchronized void removeImage(Image image, int id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                         int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        MediaEntry prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            MediaEntry next = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            if (cur.getID() == id && cur instanceof ImageMediaEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                && ((ImageMediaEntry) cur).matches(image, width, height))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    head = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    prev.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                cur.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                prev = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            cur = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        notifyAll();    // Notify in case remaining images are "done".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    synchronized void setDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
abstract class MediaEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    MediaTracker tracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    int ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    MediaEntry next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    int status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    boolean cancelled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    MediaEntry(MediaTracker mt, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        tracker = mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        ID = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    abstract Object getMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    static MediaEntry insert(MediaEntry head, MediaEntry me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        MediaEntry cur = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        MediaEntry prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        while (cur != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            if (cur.ID > me.ID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            prev = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        me.next = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            head = me;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            prev.next = me;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        return head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    int getID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        return ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    abstract void startLoad();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    void cancel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        cancelled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    static final int LOADING = MediaTracker.LOADING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    static final int ABORTED = MediaTracker.ABORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    static final int ERRORED = MediaTracker.ERRORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    static final int COMPLETE = MediaTracker.COMPLETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    static final int LOADSTARTED = (LOADING | ERRORED | COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    static final int DONE = (ABORTED | ERRORED | COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    synchronized int getStatus(boolean doLoad, boolean doVerify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if (doLoad && ((status & LOADSTARTED) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            status = (status & ~ABORTED) | LOADING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            startLoad();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        return status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    void setStatus(int flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            status = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        tracker.setDone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
class ImageMediaEntry extends MediaEntry implements ImageObserver,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    Image image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    private static final long serialVersionUID = 4739377000350280650L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    ImageMediaEntry(MediaTracker mt, Image img, int c, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        super(mt, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        image = img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        width = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        height = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    boolean matches(Image img, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        return (image == img && width == w && height == h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    Object getMedia() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    synchronized int getStatus(boolean doLoad, boolean doVerify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (doVerify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            int flags = tracker.target.checkImage(image, width, height, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            int s = parseflags(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            if (s == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                if ((status & (ERRORED | COMPLETE)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    setStatus(ABORTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            } else if (s != status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                setStatus(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        return super.getStatus(doLoad, doVerify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    void startLoad() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        if (tracker.target.prepareImage(image, width, height, this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            setStatus(COMPLETE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    int parseflags(int infoflags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        if ((infoflags & ERROR) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            return ERRORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        } else if ((infoflags & ABORT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            return ABORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        } else if ((infoflags & (ALLBITS | FRAMEBITS)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            return COMPLETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    public boolean imageUpdate(Image img, int infoflags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                               int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if (cancelled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        int s = parseflags(infoflags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        if (s != 0 && s != status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            setStatus(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        return ((status & LOADING) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
}