home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / client / clientapplet / clientimages.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  9.4 KB  |  397 lines

  1. /*
  2.  * @(#)ClientImages.java
  3.  */
  4.  
  5. package games.Battle.client.ClientApplet;
  6.  
  7. import java.applet.*;
  8. import java.awt.*;
  9. import java.awt.image.*;
  10. import java.net.*;
  11. import java.util.Random;
  12.  
  13. import games.image.*;
  14. import games.Battle.shared.sys.*;
  15.  
  16. /**
  17.  * ClientImages is responsible for loading and building all of the
  18.  * client images for the terrain, troops, water, etc. The class
  19.  * retains a collection of public static images, which are
  20.  * initialized with the call loadAll(applet). 
  21.  *
  22.  * ClientImages loads a minimal set of images from the client URL
  23.  * and builds all of the other images from this original set with
  24.  * image processing routines.
  25.  *
  26.  * @author Alex Nicolaou
  27.  * @author Jay Steele
  28.  */
  29.  
  30. public class ClientImages {
  31.  
  32.     /**
  33.      * The applet representing the source of the URL.
  34.      */
  35.     public static Applet applet = null;
  36.  
  37.     /**
  38.      * The directory containing the images.
  39.      */
  40.     private static String imageDir = "images";
  41.  
  42.     /**
  43.      * The media tracker used to wait for all the images.
  44.      */
  45.     private static MediaTracker tracker1 = null;
  46.  
  47.     /**
  48.      * The URL extracted from the applet.
  49.      */
  50.     private static URL docBase = null;
  51.  
  52.     /**
  53.      * The mediatracker image id.
  54.      */
  55.     static int id = 0;
  56.  
  57.     /**
  58.      * The background earth or floor image
  59.      */
  60.     public static Image earth = null;
  61.  
  62.     /**
  63.      * The 4 horizontal hilite images.
  64.      */
  65.     public static Image hilite_h[] = new Image[4];
  66.  
  67.     /**
  68.      * The 4 vertical hilite images.
  69.      */
  70.     public static Image hilite_v[] = new Image[4];
  71.  
  72.     /**
  73.      * The 4 horizontal shadow images.
  74.      */
  75.     public static Image shadow_h[] = new Image[4];
  76.  
  77.     /**
  78.      * The 4 vertical shadow images.
  79.      */
  80.     public static Image shadow_v[] = new Image[4];
  81.  
  82.     /**
  83.      * The 3 smoke images.
  84.      */
  85.     public static Image smoke[] = new Image[3];
  86.  
  87.     /**
  88.      * The 3 fire images.
  89.      */
  90.     public static Image fire[] = new Image[3];
  91.  
  92.     /**
  93.      * The 4 rotated single water puddle images.
  94.      */
  95.     public static Image water_single[] = new Image[4];
  96.  
  97.     /**
  98.      * The 4 rotated straight shaped water images.
  99.      */
  100.     public static Image water_straight[] = new Image[2];
  101.  
  102.     /**
  103.      * The 4 rotated elbow shaped water images.
  104.      */
  105.     public static Image water_elbow[] = new Image[4];
  106.  
  107.     /**
  108.      * The 4 rotated T shaped water images.
  109.      */
  110.     public static Image water_t[] = new Image[4];
  111.  
  112.     /**
  113.      * The 4 rotated end water images.
  114.      */
  115.     public static Image water_end[] = new Image[4];
  116.  
  117.     /**
  118.      * The single cross shaped water image.
  119.      */
  120.     public static Image water_cross = null;
  121.  
  122.     /**
  123.      * The city image.
  124.      */
  125.     public static Image city = null;
  126.  
  127.     /**
  128.      * A paratrooper image.
  129.      */
  130.     public static Image paratrooper = null;
  131.  
  132.     /**
  133.      * The four rotated gun images.
  134.      */
  135.     public static Image gun[] = new Image[4];
  136.  
  137.     /**
  138.      * The four rotated smudge images.
  139.      */
  140.     public static Image smudge[] = new Image[4];
  141.  
  142.  
  143.     /**
  144.      * A sanity check to absolutely ensure all of the images
  145.      * have been loaded properly before the client is allowed
  146.      * to execute. The method basically just polls the width and
  147.      * height of the image forever until a valid result is returned.
  148.      * @param im the image to poll
  149.      */
  150.     static void pollImage(Image im) {
  151.         while (im.getWidth(applet) == -1) {
  152.             try {
  153.                 Thread.sleep(100);
  154.             }
  155.             catch (Exception e) {}
  156.         }
  157.         while (im.getHeight(applet) == -1) {
  158.             try {
  159.                 Thread.sleep(100);
  160.             }
  161.             catch (Exception e) {}
  162.         }
  163.     }
  164.  
  165.     /**
  166.      * Retrieves an image. This method look after ensuring the 
  167.      * image is loaded from the correct URL, and the pathname
  168.      * of the image.
  169.      * @param name the name of the file to get
  170.      */
  171.     private static Image getImage(String name) {
  172.         Image im = applet.getImage(docBase, imageDir+"/"+name);
  173.         tracker1.addImage(im, id++);
  174.         pollImage(im);
  175.         return im;
  176.     }
  177.  
  178.     /**
  179.      * Load the background "earth" image. This image is 128x128 and
  180.      * placed in a 4x4 grid on the background of the client applet.
  181.      */
  182.     private static void loadEarthImages() {
  183.         earth = getImage("earth.gif");
  184.     }
  185.  
  186.     /**
  187.      * Load the hilite and shadow images.
  188.      */
  189.     private static void loadHiliteAndShadowImages() {
  190.         hilite_h[0] = getImage("hilite.gif");
  191.         shadow_h[0] = getImage("shadow.gif");
  192.     }
  193.  
  194.     /**
  195.      * Load the smoke images.
  196.      */
  197.     private static void loadSmokeImages() {
  198.         for (int i=0; i<3; i++) {
  199.             smoke[i] = getImage("smoke"+i+".gif");
  200.         }
  201.     }
  202.  
  203.     /**
  204.      * Load the fire images.
  205.      */
  206.     private static void loadFireImages() {
  207.         for (int i=0; i<3; i++) {
  208.             fire[i] = getImage("fire"+i+".gif");
  209.         }
  210.     }
  211.  
  212.     /**
  213.      * Load the water images.
  214.      */
  215.     private static void loadWaterImages() {
  216.         water_cross         = getImage("water_cross.gif");
  217.         water_single[0]     = getImage("water_single.gif");
  218.         water_elbow[0]         = getImage("water_elbow.gif");
  219.         water_t[0]             = getImage("water_t.gif");
  220.         water_end[0]         = getImage("water_end.gif");
  221.         water_straight[0]     = getImage("water_straight.gif");
  222.     }
  223.  
  224.     /**
  225.      * Load the city images.
  226.      */
  227.     public static void loadCityImages() {
  228.         city = getImage("city.gif");
  229.     }
  230.  
  231.     /**
  232.      * Load the paratrooper and gun images.
  233.      */
  234.     public static void loadParatrooperAndGunImages() {
  235.         paratrooper = getImage("para.gif");
  236.         gun[0] = getImage("gun.gif");
  237.     }
  238.  
  239.     /**
  240.      * Load the terrain smudge image.
  241.      */
  242.     public static void loadSmudgeImages() {
  243.         smudge[0] = getImage("smudge.gif");
  244.     }
  245.  
  246.     /**
  247.      * From the original horizontal hilite and shadow images, build the 
  248.      * vertical versions by rotating the original.
  249.      */
  250.     private static void buildHiliteAndShadowImages() {
  251.         int w = hilite_h[0].getWidth(applet);
  252.         int h = hilite_h[0].getHeight(applet);
  253.  
  254.         for (int i=1; i<4; i++) {
  255.             hilite_h[i] = applet.createImage(
  256.                             new FilteredImageSource(
  257.                                 hilite_h[0].getSource(),
  258.                                 new CropImageFilter(0, i, w, h-(i*2)) ));
  259.             pollImage(hilite_h[i]);
  260.         }
  261.         for (int i=1; i<4; i++) {
  262.             shadow_h[i] = applet.createImage(
  263.                             new FilteredImageSource(
  264.                                 shadow_h[0].getSource(),
  265.                                 new CropImageFilter(0, i, w, h-(i*2)) ));
  266.             pollImage(shadow_h[i]);
  267.         }
  268.         for (int i=0; i<4; i++) {
  269.             hilite_v[i] = applet.createImage(
  270.                             new FilteredImageSource(
  271.                                 hilite_h[i].getSource(),
  272.                                 new RotateImageFilter(1) ));
  273.  
  274.             pollImage(hilite_v[i]);
  275.             shadow_v[i] = applet.createImage(
  276.                             new FilteredImageSource(
  277.                                 shadow_h[i].getSource(),
  278.                                 new RotateImageFilter(1) ));
  279.  
  280.             pollImage(shadow_v[i]);
  281.         }
  282.     }
  283.  
  284.     /**
  285.      * Build all variations of the water images by rotating the
  286.      * originals to create the new images.
  287.      */
  288.     private static void buildWaterImages() {
  289.         for (int i=1; i<4; i++) {
  290.             water_single[i] = applet.createImage(
  291.                                 new FilteredImageSource(
  292.                                     water_single[i-1].getSource(),
  293.                                     new RotateImageFilter(1) ));
  294.             pollImage(water_single[i]);
  295.             water_elbow[i] = applet.createImage(
  296.                                 new FilteredImageSource(
  297.                                     water_elbow[i-1].getSource(),
  298.                                     new RotateImageFilter(1) ));
  299.             pollImage(water_elbow[i]);
  300.             water_t[i] = applet.createImage(
  301.                                 new FilteredImageSource(
  302.                                     water_t[i-1].getSource(),
  303.                                     new RotateImageFilter(1) ));
  304.             pollImage(water_t[i]);
  305.             water_end[i] = applet.createImage(
  306.                                 new FilteredImageSource(
  307.                                     water_end[i-1].getSource(),
  308.                                     new RotateImageFilter(1) ));
  309.             pollImage(water_end[i]);
  310.         }
  311.  
  312.         for (int i=1; i<2; i++) {
  313.             water_straight[i] = applet.createImage(
  314.                                 new FilteredImageSource(
  315.                                     water_straight[i-1].getSource(),
  316.                                     new RotateImageFilter(1) ));
  317.             pollImage(water_straight[i]);
  318.         }
  319.     }
  320.  
  321.     /**
  322.      * Build all variations of the gun images by rotating
  323.      * the original gun image.
  324.      */
  325.     public static void buildGunImages() {
  326.         for (int i=1; i<4; i++) {
  327.             gun[i] = applet.createImage(
  328.                                 new FilteredImageSource(
  329.                                     gun[i-1].getSource(),
  330.                                     new RotateImageFilter(1) ));
  331.             pollImage(gun[i]);
  332.         }
  333.     }
  334.  
  335.     /**
  336.      * Build all variations of the terrain smudge images by
  337.      * rotating the orginal image.
  338.      */
  339.     public static void buildSmudgeImages() {
  340.         for (int i=1; i<4; i++) {
  341.             smudge[i] = applet.createImage(
  342.                                 new FilteredImageSource(
  343.                                     smudge[i-1].getSource(),
  344.                                     new RotateImageFilter(1) ));
  345.             pollImage(smudge[i]);
  346.         }
  347.     }
  348.  
  349.     /**
  350.      * Build all images. Pass in the applet in order to extract the
  351.      * originating URL. The method processes images which have already
  352.      * been loaded in order to create new images.
  353.      * @param a the applet from which the URL will be extracted
  354.      */
  355.     public static void buildAll(Applet a) {
  356.         applet = a;
  357.         docBase = a.getCodeBase();
  358.         buildHiliteAndShadowImages();
  359.         buildGunImages();
  360.         buildSmudgeImages();
  361.         buildWaterImages();
  362.     }
  363.  
  364.     /**
  365.      * Get all of the images required by the client. Pass in the applet
  366.      * in order to extract the originating URL. The method uses
  367.      * MediaTracker to ensure the images have completely loaded
  368.      * before the client application can execute.
  369.      * @param a the applet from which the URL will be extracted
  370.      */
  371.     public static void loadAll(Applet a) {
  372.         applet = a;
  373.         docBase = a.getCodeBase();
  374.         tracker1 = new MediaTracker(applet);
  375.  
  376.         loadEarthImages();
  377.         loadHiliteAndShadowImages();
  378.         loadSmokeImages();
  379.         loadFireImages();
  380.         loadWaterImages();
  381.         loadCityImages();
  382.         loadParatrooperAndGunImages();
  383.         loadSmudgeImages();
  384.  
  385.         // Wait for all the images to load
  386.         try {
  387.             for (int i=0; i<id; i++) {
  388.                 tracker1.waitForID(i);
  389.             }
  390.         } catch (InterruptedException e) {
  391.             return;
  392.         }
  393.  
  394.         buildAll(a);
  395.     }
  396. }
  397.