API java : MediaTracker


java.awt
Class MediaTracker

java.lang.Object
  extended by java.awt.MediaTracker
All Implemented Interfaces:
Serializable

public class MediaTracker
extends Object
implements Serializable

The MediaTracker class is a utility class to track the status of a number of media objects. Media objects could include audio clips as well as images, though currently only images are supported.

To use a media tracker, create an instance of MediaTracker and call its addImage method for each image to be tracked. In addition, each image can be assigned a unique identifier. This identifier controls the priority order in which the images are fetched. It can also be used to identify unique subsets of the images that can be waited on independently. Images with a lower ID are loaded in preference to those with a higher ID number.

Tracking an animated image might not always be useful due to the multi-part nature of animated image loading and painting, but it is supported. MediaTracker treats an animated image as completely loaded when the first frame is completely loaded. At that point, the MediaTracker signals any waiters that the image is completely loaded. If no ImageObservers are observing the image when the first frame has finished loading, the image might flush itself to conserve resources (see Image.flush()).

Here is an example of using MediaTracker:


  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Image;
  4. import java.awt.Graphics;
  5. import java.awt.MediaTracker;
  6.  
  7. public class ImageBlaster extends Applet implements Runnable {
  8. MediaTracker tracker;
  9. Image bg;
  10. Image anim[] = new Image[5];
  11. int index;
  12. Thread animator;
  13.  
  14. // Get the images for the background (id == 0)
  15. // and the animation frames (id == 1)
  16. // and add them to the MediaTracker
  17. public void init() {
  18. tracker = new MediaTracker(this);
  19. bg = getImage(getDocumentBase(),
  20. "images/background.gif");
  21. tracker.addImage(bg, 0);
  22. for (int i = 0; i < 5; i++) {
  23. anim[i] = getImage(getDocumentBase(),
  24. "images/anim"+i+".gif");
  25. tracker.addImage(anim[i], 1);
  26. }
  27. }
  28.  
  29. // Start the animation thread.
  30. public void start() {
  31. animator = new Thread(this);
  32. animator.start();
  33. }
  34.  
  35. // Stop the animation thread.
  36. public void stop() {
  37. animator = null;
  38. }
  39.  
  40. // Run the animation thread.
  41. // First wait for the background image to fully load
  42. // and paint. Then wait for all of the animation
  43. // frames to finish loading. Finally, loop and
  44. // increment the animation frame index.
  45. public void run() {
  46. try {
  47. tracker.waitForID(0);
  48. tracker.waitForID(1);
  49. } catch (InterruptedException e) {
  50. return;
  51. }
  52. Thread me = Thread.currentThread();
  53. while (animator == me) {
  54. try {
  55. Thread.sleep(100);
  56. } catch (InterruptedException e) {
  57. break;
  58. }
  59. synchronized (this) {
  60. index++;
  61. if (index >= anim.length) {
  62. index = 0;
  63. }
  64. }
  65. repaint();
  66. }
  67. }
  68.  
  69. // The background image fills the frame so we
  70. // don't need to clear the applet on repaints.
  71. // Just call the paint method.
  72. public void update(Graphics g) {
  73. paint(g);
  74. }
  75.  
  76. // Paint a large red rectangle if there are any errors
  77. // loading the images. Otherwise always paint the
  78. // background so that it appears incrementally as it
  79. // is loading. Finally, only paint the current animation
  80. // frame if all of the frames (id == 1) are done loading,
  81. // so that we don't get partial animations.
  82. public void paint(Graphics g) {
  83. if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
  84. g.setColor(Color.red);
  85. g.fillRect(0, 0, size().width, size().height);
  86. return;
  87. }
  88. g.drawImage(bg, 0, 0, this);
  89. if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
  90. g.drawImage(anim[index], 10, 10, this);
  91. }
  92. }
  93. }

Since:
JDK1.0
See Also:
Serialized Form

Field Summary
static int ABORTED
          Flag indicating that the downloading of media was aborted.
static int COMPLETE
          Flag indicating that the downloading of media was completed successfully.
static int ERRORED
          Flag indicating that the downloading of media encountered an error.
static int LOADING
          Flag indicating that media is currently being loaded.
 
Constructor Summary
MediaTracker(Component comp)
          Creates a media tracker to track images for a given component.
 
Method Summary
 void addImage(Image image, int id)
          Adds an image to the list of images being tracked by this media tracker.
 void addImage(Image image, int id, int w, int h)
          Adds a scaled image to the list of images being tracked by this media tracker.
 boolean checkAll()
          Checks to see if all images being tracked by this media tracker have finished loading.
 boolean checkAll(boolean load)
          Checks to see if all images being tracked by this media tracker have finished loading.
 boolean checkID(int id)
          Checks to see if all images tracked by this media tracker that are tagged with the specified identifier have finished loading.
 boolean checkID(int id, boolean load)
          Checks to see if all images tracked by this media tracker that are tagged with the specified identifier have finished loading.
 Object[] getErrorsAny()
          Returns a list of all media that have encountered an error.
 Object[] getErrorsID(int id)
          Returns a list of media with the specified ID that have encountered an error.
 boolean isErrorAny()
          Checks the error status of all of the images.
 boolean isErrorID(int id)
          Checks the error status of all of the images tracked by this media tracker with the specified identifier.
 void removeImage(Image image)
          Removes the specified image from this media tracker.
 void removeImage(Image image, int id)
          Removes the specified image from the specified tracking ID of this media tracker.
 void removeImage(Image image, int id, int width, int height)
          Removes the specified image with the specified width, height, and ID from this media tracker.
 int statusAll(boolean load)
          Calculates and returns the bitwise inclusive OR of the status of all media that are tracked by this media tracker.
 int statusID(int id, boolean load)
          Calculates and returns the bitwise inclusive OR of the status of all media with the specified identifier that are tracked by this media tracker.
 void waitForAll()
          Starts loading all images tracked by this media tracker.
 boolean waitForAll(long ms)
          Starts loading all images tracked by this media tracker.
 void waitForID(int id)
          Starts loading all images tracked by this media tracker with the specified identifier.
 boolean waitForID(int id, long ms)
          Starts loading all images tracked by this media tracker with the specified identifier.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOADING

public static final int LOADING
Flag indicating that media is currently being loaded.

See Also:
statusAll(boolean), statusID(int, boolean), Constant Field Values

ABORTED

public static final int ABORTED
Flag indicating that the downloading of media was aborted.

See Also:
statusAll(boolean), statusID(int, boolean), Constant Field Values

ERRORED

public static final int ERRORED
Flag indicating that the downloading of media encountered an error.

See Also:
statusAll(boolean), statusID(int, boolean), Constant Field Values

COMPLETE

public static final int COMPLETE
Flag indicating that the downloading of media was completed successfully.

See Also:
statusAll(boolean), statusID(int, boolean), Constant Field Values
Constructor Detail

MediaTracker

public MediaTracker(Component comp)
Creates a media tracker to track images for a given component.

Parameters:
comp - the component on which the images will eventually be drawn
Method Detail

addImage

public void addImage(Image image,
                     int id)
Adds an image to the list of images being tracked by this media tracker. The image will eventually be rendered at its default (unscaled) size.

Parameters:
image - the image to be tracked
id - an identifier used to track this image

addImage

public void addImage(Image image,
                     int id,
                     int w,
                     int h)
Adds a scaled image to the list of images being tracked by this media tracker. The image will eventually be rendered at the indicated width and height.

Parameters:
image - the image to be tracked
id - an identifier that can be used to track this image
w - the width at which the image is rendered
h - the height at which the image is rendered

checkAll

public boolean checkAll()
Checks to see if all images being tracked by this media tracker have finished loading.

This method does not start loading the images if they are not already loading.

If there is an error while loading or scaling an image, then that image is considered to have finished loading. Use the isErrorAny or isErrorID methods to check for errors.

Returns:
true if all images have finished loading, have been aborted, or have encountered an error; false otherwise
See Also:
checkAll(boolean), checkID(int), isErrorAny(), isErrorID(int)

checkAll

public boolean checkAll(boolean load)
Checks to see if all images being tracked by this media tracker have finished loading.

If the value of the load flag is true, then this method starts loading any images that are not yet being loaded.

If there is an error while loading or scaling an image, that image is considered to have finished loading. Use the isErrorAny and isErrorID methods to check for errors.

Parameters:
load - if true, start loading any images that are not yet being loaded
Returns:
true if all images have finished loading, have been aborted, or have encountered an error; false otherwise
See Also:
checkID(int), checkAll(), isErrorAny(), isErrorID(int)

isErrorAny

public boolean isErrorAny()
Checks the error status of all of the images.

Returns:
true if any of the images tracked by this media tracker had an error during loading; false otherwise
See Also:
isErrorID(int), getErrorsAny()

getErrorsAny

public Object[] getErrorsAny()
Returns a list of all media that have encountered an error.

Returns:
an array of media objects tracked by this media tracker that have encountered an error, or null if there are none with errors
See Also:
isErrorAny(), getErrorsID(int)

waitForAll

public void waitForAll()
                throws InterruptedException
Starts loading all images tracked by this media tracker. This method waits until all the images being tracked have finished loading.

If there is an error while loading or scaling an image, then that image is considered to have finished loading. Use the isErrorAny or isErrorID methods to check for errors.

Throws:
InterruptedException - if another thread has interrupted this thread
See Also:
waitForID(int), waitForAll(long), isErrorAny(), isErrorID(int)

waitForAll

public boolean waitForAll(long ms)
                   throws InterruptedException
Starts loading all images tracked by this media tracker. This method waits until all the images being tracked have finished loading, or until the length of time specified in milliseconds by the ms argument has passed.

If there is an error while loading or scaling an image, then that image is considered to have finished loading. Use the isErrorAny or isErrorID methods to check for errors.

Parameters:
ms - the number of milliseconds to wait for the loading to complete
Returns:
true if all images were successfully loaded; false otherwise
Throws:
InterruptedException - if another thread has interrupted this thread.
See Also:
waitForID(int), waitForAll(long), isErrorAny(), isErrorID(int)

statusAll

public int statusAll(boolean load)
Calculates and returns the bitwise inclusive OR of the status of all media that are tracked by this media tracker.

Possible flags defined by the MediaTracker class are LOADING, ABORTED, ERRORED, and COMPLETE. An image that hasn't started loading has zero as its status.

If the value of load is true, then this method starts loading any images that are not yet being loaded.

Parameters:
load - if true, start loading any images that are not yet being loaded
Returns:
the bitwise inclusive OR of the status of all of the media being tracked
See Also:
statusID(int, boolean), LOADING, ABORTED, ERRORED, COMPLETE

checkID

public boolean checkID(int id)
Checks to see if all images tracked by this media tracker that are tagged with the specified identifier have finished loading.

This method does not start loading the images if they are not already loading.

If there is an error while loading or scaling an image, then that image is considered to have finished loading. Use the isErrorAny or isErrorID methods to check for errors.

Parameters:
id - the identifier of the images to check
Returns:
true if all images have finished loading, have been aborted, or have encountered an error; false otherwise
See Also:
checkID(int, boolean), checkAll(), isErrorAny(), isErrorID(int)

checkID

public boolean checkID(int id,
                       boolean load)
Checks to see if all images tracked by this media tracker that are tagged with the specified identifier have finished loading.

If the value of the load flag is true, then this method starts loading any images that are not yet being loaded.

If there is an error while loading or scaling an image, then that image is considered to have finished loading. Use the isErrorAny or isErrorID methods to check for errors.

Parameters:
id - the identifier of the images to check
load - if true, start loading any images that are not yet being loaded
Returns:
true if all images have finished loading, have been aborted, or have encountered an error; false otherwise
See Also:
checkID(int, boolean), checkAll(), isErrorAny(), isErrorID(int)

isErrorID

public boolean isErrorID(int id)
Checks the error status of all of the images tracked by this media tracker with the specified identifier.

Parameters:
id - the identifier of the images to check
Returns:
true if any of the images with the specified identifier had an error during loading; false otherwise
See Also:
isErrorAny(), getErrorsID(int)

getErrorsID

public Object[] getErrorsID(int id)
Returns a list of media with the specified ID that have encountered an error.

Parameters:
id - the identifier of the images to check
Returns:
an array of media objects tracked by this media tracker with the specified identifier that have encountered an error, or null if there are none with errors
See Also:
isErrorID(int), isErrorAny(), getErrorsAny()

waitForID

public void waitForID(int id)
               throws InterruptedException
Starts loading all images tracked by this media tracker with the specified identifier. This method waits until all the images with the specified identifier have finished loading.

If there is an error while loading or scaling an image, then that image is considered to have finished loading. Use the isErrorAny and isErrorID methods to check for errors.

Parameters:
id - the identifier of the images to check
Throws:
InterruptedException - if another thread has interrupted this thread.
See Also:
waitForAll(), isErrorAny(), isErrorID(int)

waitForID

public boolean waitForID(int id,
                         long ms)
                  throws InterruptedException
Starts loading all images tracked by this media tracker with the specified identifier. This method waits until all the images with the specified identifier have finished loading, or until the length of time specified in milliseconds by the ms argument has passed.

If there is an error while loading or scaling an image, then that image is considered to have finished loading. Use the statusID, isErrorID, and isErrorAny methods to check for errors.

Parameters:
id - the identifier of the images to check
ms - the length of time, in milliseconds, to wait for the loading to complete
Throws:
InterruptedException - if another thread has interrupted this thread.
See Also:
waitForAll(), waitForID(int), statusID(int, boolean), isErrorAny(), isErrorID(int)

statusID

public int statusID(int id,
                    boolean load)
Calculates and returns the bitwise inclusive OR of the status of all media with the specified identifier that are tracked by this media tracker.

Possible flags defined by the MediaTracker class are LOADING, ABORTED, ERRORED, and COMPLETE. An image that hasn't started loading has zero as its status.

If the value of load is true, then this method starts loading any images that are not yet being loaded.

Parameters:
id - the identifier of the images to check
load - if true, start loading any images that are not yet being loaded
Returns:
the bitwise inclusive OR of the status of all of the media with the specified identifier that are being tracked
See Also:
statusAll(boolean), LOADING, ABORTED, ERRORED, COMPLETE

removeImage

public void removeImage(Image image)
Removes the specified image from this media tracker. All instances of the specified image are removed, regardless of scale or ID.

Parameters:
image - the image to be removed
Since:
JDK1.1
See Also:
removeImage(java.awt.Image, int), removeImage(java.awt.Image, int, int, int)

removeImage

public void removeImage(Image image,
                        int id)
Removes the specified image from the specified tracking ID of this media tracker. All instances of Image being tracked under the specified ID are removed regardless of scale.

Parameters:
image - the image to be removed
id - the tracking ID frrom which to remove the image
Since:
JDK1.1
See Also:
removeImage(java.awt.Image), removeImage(java.awt.Image, int, int, int)

removeImage

public void removeImage(Image image,
                        int id,
                        int width,
                        int height)
Removes the specified image with the specified width, height, and ID from this media tracker. Only the specified instance (with any duplicates) is removed.

Parameters:
image - the image to be removed
id - the tracking ID from which to remove the image
width - the width to remove (-1 for unscaled)
height - the height to remove (-1 for unscaled)
Since:
JDK1.1
See Also:
removeImage(java.awt.Image), removeImage(java.awt.Image, int)

Ces informations proviennent du site de http://java.sun.com

Remarques

Contenu

Le contenu de cette page provient du site de Sun, et est généré depuis un cache sur l'infobrol après certains traitements automatisés. La présentation peut donc différer du document original, mais le contenu aussi. Vous pouvez utiliser ce bouton pour afficher la page originale du site de Sun :

Quels sont les motivations de cette démarche?

Maintenir les pages en cache sur différents sites peut offrir plus de disponibilité.

Chaque page est indexée dans la base de donnée, ce qui permet de retrouver facilement les informations, au moyen des sommaires, du moteur de recherche interne, etc.

Des facilités sont mises en place pour que les membres de l'infobrol puissent effectuer des traductions en français des différents documents. Ceci devrait permettre aux débutants en programmation Java de consulter les API en français s'ils maîtrisent moins bien la langue de Shakespeare. Dans le cas où une traduction a été soumise, elle est disponible au moyen d'un lien en bas de page. Si la traduction a été validée, la page s'affiche par défaut en français, et un lien en bas de page permet d'atteindre la version en anglais.

Le code sur l'infobrol est automatiquement coloré selon la syntaxe, et les différents mots clés sont transformés en liens pour accéder rapidement aux informations.

Vous avez la possibilité de partager vos expériences en proposant vos propres extraits de code en utilisant le bouton "ajouter un commentaire" en bas de page. Si vous visitez simplement l'infobrol, vous avez déjà accès à cette fonction, mais si vous étes membre du brol, vous pouvez en plus utiliser des boutons supplémentaires de mise en forme, dont la coloration automatique de vos extraits de codes.

Réseaux sociaux

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.

 

Nuage de mots clés

6 mots clés dont 0 définis manuellement (plus d'information...).

Avertissement

Cette page ne possède pas encore de mots clés manuels, ceci est donc un exemple automatique (les niveaux de pertinence sont fictifs, mais les liens sont valables). Pour tester le nuage avec une page qui contient des mots définis manuellement, vous pouvez cliquer ici.

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher le nuage de mots clés.

 

Astuce pour imprimer les couleurs des cellules de tableaux : http://www.gaudry.be/ast-rf-450.html
Aucun commentaire pour cette page

© Ce document issu de l′infobrol est enregistré sous le certificat Cyber PrInterDeposit Digital Numbertection. Enregistrement IDDN n° 5329-1294
Document créé le 29/08/06 23:49, dernière modification le Vendredi 17 Juin 2011, 12:12
Source du document imprimé : http://www.gaudry.be/java-api-rf-java/awt/MediaTracker.html Document affiché 1 fois ce mois de Juin.
St.Gaudry©07.01.02
Outils (masquer)
||
Recherche (afficher)
Recherche :

Utilisateur (masquer)
Navigation (masquer)
Apparence (afficher)
Stats (afficher)
15832 documents
452 astuces.
549 niouzes.
3099 definitions.
447 membres.
8115 messages.

Document genere en :
0,62 seconde

Mises à jour :
Mises à jour du site
Citation (masquer)
Le cercle est le plus long chemin d'un point au même point.

Tom Stoppard
 
l'infobrol
Nous sommes le Vendredi 01 Juin 2012, 11:52, toutes les heures sont au format GMT+1.00 Heure, heure d'été (+1)