home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / de86gnzn / examples / boink / com / next / gt / imagemanager.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.4 KB  |  104 lines

  1. /**
  2.  *
  3.  * ImageManager.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Mar 19/1996
  7.  *
  8.  * ImageManager is used to force-load images at once to avoid taking
  9.  * the hit during gameplay and distrupting game flow.  Images to be cached
  10.  * are listed in a cache file located in the <codebase>/images directory.  The
  11.  * default cache file is images/.cache.
  12.  *
  13.  */
  14.  
  15. package com.next.gt;
  16.  
  17. import java.net.*;
  18. import java.io.*;
  19. import java.awt.*;
  20. import java.awt.image.*;
  21.  
  22. public class ImageManager extends java.lang.Object{
  23.   Gamelet    owner;
  24.   
  25. /**
  26.   Cache those images which are listed in images/.cache.
  27. */
  28. public ImageManager(Gamelet theOwner) {
  29.   this(theOwner, ".cache");
  30. } /*ImageManager()*/
  31.  
  32.  
  33.  
  34. /**
  35.   Cache those images which are listed in the specified cache file.
  36.   This cache file should exist under the images directory.
  37. */
  38. public ImageManager(Gamelet theOwner, String cacheFile) {
  39.   URL                 myURL= null;
  40.   InputStream        myStream= null;
  41.   DataInputStream    data=null;
  42.   String            line;
  43.  
  44.  Image            theImage;
  45.   Image         offScreenBuffer;
  46.   MediaTracker        tracker;
  47.   int                imageCount= 0;
  48.   
  49.   owner= theOwner;
  50.   
  51.   //
  52.   // create the offscreen buffer
  53.   //
  54.   offScreenBuffer= owner.createImage (1, 1);
  55.  
  56.  
  57.   //
  58.   // create URL that points to cache file.  the cache file lists all images
  59.   // that are to be preloaded.
  60.   //
  61.   try {
  62.     myURL= new URL (owner.getCodeBase().toString()+"/images/" + cacheFile);
  63.   }
  64.   catch(MalformedURLException e) {
  65.     System.out.println("GT: ImageManager cannot read cache file; " + e.getMessage());
  66.   }
  67.   
  68.   
  69.   //
  70.   // cycle through all images
  71.   //
  72.   try {
  73.     myStream= myURL.openStream();
  74.     data= new DataInputStream(new BufferedInputStream(myStream));
  75.     while ((line= data.readLine())!=null) {
  76.       imageCount++;
  77.       theImage = owner.getImage(owner.getCodeBase(), "images/"+line+".gif");
  78.       tracker= new java.awt.MediaTracker(owner);
  79.  
  80.       tracker.addImage(theImage, imageCount);
  81.       owner.showStatus ("GT: Caching image: " + line + ".");
  82.  
  83.     //
  84.     // wait for images to be cached
  85.     //
  86.     try
  87. {
  88.         tracker.waitForID(imageCount);
  89.     } 
  90.     catch (InterruptedException e) {
  91.         System.out.println("GT: ImageManager ridiculous image; " + e.getMessage());
  92.     }
  93.  
  94.      offScreenBuffer.getGraphics ().drawImage (theImage, 0, 0, owner);
  95.     } /*endWhile*/
  96.   } 
  97.   catch(IOException e ){
  98.     System.out.println("GOOF: ImageManager cannot getImage; " + e.getMessage());
  99.   }
  100.   
  101. } /*ImageManager(,)*/
  102.  
  103.   
  104. } /*ImageManager*/
  105.  
  106.