home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / com / next / gt / RCS / ImageManager.java,v < prev    next >
Text File  |  1998-04-22  |  3KB  |  121 lines

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