home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / com / next / gt / ImageManager.java < prev    next >
Text File  |  1998-04-15  |  2KB  |  98 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.   Gamelication    owner;
  24.   
  25. /**
  26.   Cache those images which are listed in images/.cache.
  27. */
  28. public ImageManager(Gamelication 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(Gamelication theOwner, String cacheFile) {
  39.   //URL             myURL= null;
  40.   File            myfile;
  41.   FileReader        myReader;
  42.   BufferedReader    data;
  43.   String        line;
  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.   // create URL that points to cache file.  the cache file lists all images
  58.   // that are to be preloaded.
  59.   //
  60.   myfile= new File ( owner.getCodeBase().toString()+"/images/" + cacheFile );
  61.     
  62.   //
  63.   // cycle through all images
  64.   //
  65.   tracker= new java.awt.MediaTracker(owner);
  66.   try {
  67.     myReader = new FileReader( myfile );
  68.     data= new BufferedReader( myReader );
  69.     while ((line= data.readLine())!=null) {
  70.           imageCount++;
  71.       theImage = owner.getImage(owner.getCodeBase(), "images/"+line+".gif");
  72.           
  73.           tracker.addImage(theImage, imageCount);
  74.           owner.showStatus ("GT: Caching image: " + line + "." );
  75.  
  76.       //
  77.       // wait for images to be cached
  78.       //
  79.         try{
  80.         tracker.waitForID(imageCount);
  81.         } 
  82.         catch (InterruptedException e) {
  83.         System.out.println("GT: ImageManager ridiculous image; " + e.getMessage());
  84.         }
  85.         offScreenBuffer = owner.createImage ( 1, 1 );
  86.         offScreenBuffer.getGraphics().drawImage (theImage, 0, 0, owner);
  87.     } /*endWhile*/
  88.   }
  89.   catch(IOException e ){
  90.     System.out.println("GOOF: ImageManager cannot getImage; " + e.getMessage());
  91.   }
  92.   
  93. } /*ImageManager(,)*/
  94.  
  95.   
  96. } /*ImageManager*/
  97.  
  98.