⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagelibrary.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To load an image in the database. You can use this method to load images that are
   *  in "-JIT" directories or "-EXC" directories. If a directory that has the '-exc' option
   *  is found on the path to the image, we unload all the other sub-directories starting
   *  from this directory.
   *
   * @param imId complete image identifier
   * @return the image just loaded in the library.
   * @exception ImageLibraryException if the imId is invalid.
   */
   public BufferedImage loadImage( ImageIdentifier imId )
   throws ImageLibraryException {

      ImageLibDir dir = rootDir;

       try{
        // we select the directory where the image is
           for( int index=0; index<imId.dirIds.length; index++ ) {
                dir = dir.childDirs[imId.dirIds[index]];

             // We check for the "-EXC" option                
                if(dir.dirOption==ImageLibDir.OPT_EXC && index<imId.dirIds.length-1) {
                   // we unload other sub-directories
                      for( int i=0; i<dir.childDirs.length; i++ )
                           if(i!=imId.dirIds[index+1])
                              dir.childDirs[i].unloadAllImages(true);
                }
           }

        // is the image already available ?
           BufferedImage bufIm = dir.images[ imId.imageId ];

           if(bufIm!=null)
              return bufIm; // success !

        // We try to load the image ( must be a JIT image ).
           if( dir.dirOption==ImageLibDir.OPT_JIT && loadAllJITDirectoryImages ) {
               dir.loadAllImages( false ); // we load all the images of that directory
               return dir.images[imId.imageId];
           }

        // We try to load the image...
           return dir.loadImage( imId.imageId );
       }
       catch( Exception e ) {
         throw new ImageLibraryException(""+e);
       }
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To load all the images of a directory in the database. You can use this method to load
   * images that are in "-JIT" directories or "-EXC" directories. If a directory that has the
   * '-exc' option is found on the path to the image, we unload all the other sub-directories
   * starting from this directory.
   *
   * @param imId complete image identifier pointing to a directory of the database
   * @param loadSubDirsAlso if true we also load the sub-directories
   * @return the number of images loaded in the library.
   * @exception ImageLibraryException if the imId is invalid.
   */
   public int loadImagesFromDirectory( ImageIdentifier imId, boolean loadSubDirsAlso )
   throws ImageLibraryException {

      ImageLibDir dir = rootDir;

       try{
        // we select the directory where the image is
           for( int index=0; index<imId.dirIds.length; index++ ) {
                dir = dir.childDirs[imId.dirIds[index]];

             // We check for the "-EXC" option                
                if(dir.dirOption==ImageLibDir.OPT_EXC && index<imId.dirIds.length-1) {
                   // we unload other sub-directories
                      for( int i=0; i<dir.childDirs.length; i++ )
                           if(i!=imId.dirIds[index+1])
                              dir.childDirs[i].unloadAllImages(true);
                }
           }
       }
       catch( Exception e ) {
          throw new ImageLibraryException(""+e);
       }

    // is the image already available ?
       return dir.loadAllImages(loadSubDirsAlso);
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To check that the specified ImageIdentifier points out a valid Imagelibrary entry.
   *
   * @param imId complete image identifier
   * @return true if the image is in the library (already loaded), false if not.
   * @exception ImageLibraryException if the imId is invalid.
   */
   public boolean checkImage( ImageIdentifier imId )
   throws ImageLibraryException {

      ImageLibDir dir = rootDir;

       try{
        // we select the directory where the image is supposed to be
           for( int index=0; index<imId.dirIds.length; index++ )
                dir = dir.childDirs[imId.dirIds[index]];

        // is the image available ?
           if( dir.images[ imId.imageId ]!=null)
               return true;
       }
       catch( Exception e ) {
           throw new ImageLibraryException(""+e);
       }

       return false;
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To unload an image from memory.
   * @param imID image identifier pointing the imageSet to unload.
   * @exception ImageLibraryException if the imId is invalid.
   */
   public void unloadImage( ImageIdentifier imId )
   throws ImageLibraryException {

      ImageLibDir dir = rootDir;

       try{
        // we select the directory where the image is
           for( int index=0; index<imId.dirIds.length; index++ )
                dir = dir.childDirs[imId.dirIds[index]];
       }
       catch( Exception e ) {
           throw new ImageLibraryException(""+e);
       }

    // we unload the image.
       dir.unloadImage( imId.imageId );
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To unload all the images of a directory in the database.
   *
   * @param imId complete image identifier pointing to a directory of the database
   * @param unloadSubDirsAlso if true we also load the sub-directories
   * @return the number of images loaded in the library.
   * @exception ImageLibraryException if the imId is invalid.
   */
   public int unloadImagesFromDirectory( ImageIdentifier imId, boolean unloadSubDirsAlso )
   throws ImageLibraryException {

      ImageLibDir dir = rootDir;

       try{
        // we select the directory where the image is
           for( int index=0; index<imId.dirIds.length; index++ )
                dir = dir.childDirs[imId.dirIds[index]];
       }
       catch( Exception e ) {
           throw new ImageLibraryException(""+e);
       }

    // we unload all the images
       return dir.unloadAllImages(unloadSubDirsAlso);
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To get an image's width. IMPORTANT: We suppose the image is in the database
   *  (except if it's a JIT image).
   *
   * @param imId image identifier
   * @return width
   * @exception ImageLibraryException if the imId is invalid.
   */
   public int getWidth( ImageIdentifier imId )
   throws ImageLibraryException {

      BufferedImage bufIm = getImage( imId );
      if( bufIm==null )
          return -1;

      return bufIm.getWidth(null);
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To get an image's height. IMPORTANT: We suppose the image is in the database
   *  (except if it's a JIT image).
   *
   * @param imId image identifier
   * @return height
   * @exception ImageLibraryException if the imId is invalid.
   */
   public int getHeight( ImageIdentifier imId )
   throws ImageLibraryException {

      BufferedImage bufIm = getImage( imId );
      if( bufIm==null )
          return -1;

      return bufIm.getHeight(null);
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To get an image's dimension. IMPORTANT: We suppose the image is in the database
   *  (except if it's a JIT image).
   *
   * @param imId image identifier
   * @return dimension
   * @exception ImageLibraryException if the imId is invalid.
   */
   public Dimension getDimension( ImageIdentifier imId )
   throws ImageLibraryException {

      BufferedImage bufIm = getImage( imId );
      if( bufIm==null )
          return new Dimension(-1,-1);

      return new Dimension( bufIm.getWidth(null), bufIm.getHeight(null) );
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** For Animations. We return the number of images of the specified ImageIdentifier's
   *  directory. Animation objects call this method to initialize.
   *
   * @param imId image identifier
   * @return length of the dir.images[] array given by the imId.
   * @exception ImageLibraryException if the imId is invalid.
   */
   public int getAnimationLength( ImageIdentifier imId )
   throws ImageLibraryException {

      ImageLibDir dir = rootDir;

       try{
        // we select the directory where the image is supposed to be
           for( int index=0; index<imId.dirIds.length; index++ )
                dir = dir.childDirs[imId.dirIds[index]];
       }
       catch( Exception e ) {
           throw new ImageLibraryException(""+e);
       }

      return dir.images.length;
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To get the ImageIdentifier of an image given a key-word. We don't load any images,
   *  we just check the image file names that are in the given directory.
   *
   *  This is especially useful when you are searching for an image's mask. If the image
   *  mask is in the same directory and has the word 'mask' somewhere in its
   *  file name, getImageIdentifier( myDirectoryId, "mask" ) will return its
   *  entire identifier.
   *
   * @param imDirectory complete image identifier representing the directory where to search.
   * @param keyword word to search.
   * @return if found, the complete ImageIdentifier of the first image matching the keyword,
   *         null otherwise.
   * @exception ImageLibraryException if the imId is invalid.
   */
   public ImageIdentifier getImageIdentifier( ImageIdentifier imDirectory, String keyword )
   throws ImageLibraryException {

           ImageLibDir dir = rootDir;

        // we select the directory where the image is supposed to be
           try{
             for( int index=0; index<imDirectory.dirIds.length; index++ )
                  dir = dir.childDirs[imDirectory.dirIds[index]];
           }
           catch( Exception e ) {
               throw new ImageLibraryException("Bad ImageIdentifier: "+e);
           }

        // we search among the images we have.
           String imageFiles[] = resourceLocator.listFiles( dir.dir );
           if( imageFiles==null ) return null;

           for( int l=0; l<imageFiles.length; l++ ) {

             int id = getImageID( imageFiles[l] );
             if( id<0 ) continue; // not a valid image file

            // we try to get the last part of the file path (file name)
             int index = imageFiles[l].lastIndexOf("/");

             if( index < 0 )
                 index = imageFiles[l].lastIndexOf(File.separator);

             String fileName = imageFiles[l];

             if( index >0 )
                 fileName = fileName.substring( index, fileName.length() );

            // Is it the image we wanted ?
               if( fileName.indexOf( keyword ) >=0 ) {
                   ImageIdentifier result = new ImageIdentifier( imDirectory );
                   result.imageId = (short) id;
                   return result;
               }
           }

       return null; // not found
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -