home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / com / next / gt / backup / ImageManager.java < prev    next >
Encoding:
Java Source  |  1998-04-07  |  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.  Image            theImage;
  44.   Image         offScreenBuffer;
  45.   MediaTracker        tracker;
  46.   int                imageCount= 0;
  47.   
  48.   owner= theOwner;
  49.   
  50.   //
  51.   // create the offscreen buffer
  52.   //
  53.   offScreenBuffer= owner.createImage (1, 1);
  54.  
  55.  
  56.   //
  57.   // create URL that points to cache file.  the cache file lists all images
  58.   // that are to be preloaded.
  59.   //
  60.   try {
  61.     myURL= new URL (owner.getCodeBase().toString()+"/images/" + cacheFile);
  62.   }
  63.   catch(MalformedURLException e) {
  64.     System.out.println("GT: ImageManager cannot read cache file; " + e.getMessage());
  65.   }
  66.   
  67.   
  68.   //
  69.   // cycle through all images
  70.   //
  71.   try {
  72.     myStream= myURL.openStream();
  73.     data= new DataInputStream(new BufferedInputStream(myStream));
  74.     while ((line= data.readLine())!=null) {
  75.       imageCount++;
  76.       theImage = owner.getImage(owner.getCodeBase(), "images/"+line+".gif");
  77.       tracker= new java.awt.MediaTracker(owner);
  78.  
  79.       tracker.addImage(theImage, imageCount);
  80.       owner.showStatus ("GT: Caching image: " + line + ".");
  81.  
  82.     //
  83.     // wait for images to be cached
  84.     //
  85.     try{
  86.         tracker.waitForID(imageCount);
  87.     } 
  88.     catch (InterruptedException e) {
  89.         System.out.println("GT: ImageManager ridiculous image; " + e.getMessage());
  90.     }
  91.  
  92.      offScreenBuffer.getGraphics ().drawImage (theImage, 0, 0, owner);
  93.     } /*endWhile*/
  94.   } 
  95.   catch(IOException e ){
  96.     System.out.println("GOOF: ImageManager cannot getImage; " + e.getMessage());
  97.   }
  98.   
  99. } /*ImageManager(,)*/
  100.  
  101.   
  102. } /*ImageManager*/
  103.  
  104.