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

📄 imagelibrary.java

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

  /** To get the File object that represents an ImageIdentifier of our database.
   *  We don't load any images, just return the associated File object.
   *
   *  This is a utility method which is not used in the ImageLibrary class.
   *
   * @param imId complete image identifier representing an image of the database.
   * @return if found, the complete path to the image on disk, null otherwise.
   * @exception ImageLibraryException if the imId is invalid.
   */
   public String getImageFile( ImageIdentifier imId )
   throws ImageLibraryException {

           ImageLibDir dir = rootDir;

        // we select the directory where the image is supposed to be
           try{
             for( int index=0; index<imId.dirIds.length; index++ )
                  dir = dir.childDirs[imId.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

            // Is it the image we wanted ?
               if( id==imId.imageId )
                   return imageFiles[l];
           }

       return null; // not found
    }

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

  /** Given a directory name of our database ( format <name>-<number>-<option> )
   *  we return the <number> part.
   *
   * @param name directory name
   * @return ID>=0 if valid, -1 if not valid.
   */
   protected static int getDirectoryID( String dirName ) {

       String name = dirName.toLowerCase();
       String s_val = null;

       if(name.endsWith("/"))
          name = name.substring(0,name.length()-1);
       else if( name.endsWith(File.separator) )
          name = name.substring(0,name.length()-File.separator.length());


    // 1 - we retrieve the substring
       if(name.endsWith("-jit")||name.endsWith("-exc")) {
           int last = name.lastIndexOf('-');
           if(last<2) return -1; // error: invalid index

           int first = name.lastIndexOf('-', last-1);
           if(first<0) return -1; // error: invalid format

           s_val = name.substring( first+1, last );
       }
       else {
           int first = name.lastIndexOf('-');
           if( first<0 || first==name.length()-1 ) return -1;

           s_val = name.substring( first+1, name.length() );
       }

    // 2 - We parse the substring
       try{
           return Integer.parseInt( s_val );
       }catch(NumberFormatException bne) {
       	   return -1; // invalid format
       }
   }

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

  /** Given an image name of our database ( format <name>-<number>.<jpg|gif|*> )
   *  we return the <number> part.
   *
   * @param name image name
   * @return ID>=0 if valid, -1 if not valid.
   */
   static protected int getImageID( String imageName ) {

       String name = imageName.toLowerCase();

    // 1 - we retrieve the substring
       int lastPoint = name.lastIndexOf('.');
       if(lastPoint<2) return -1; // error: invalid index

       int first = name.lastIndexOf('-', lastPoint-1);
       if(first<0) return -1; // error: invalid format

    // 2 - We parse the substring            
       try{
           return Integer.parseInt( name.substring( first+1, lastPoint ) );
       }catch(NumberFormatException bne) {
       	   return -1; // invalid format
       }
   }

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

  /** Given a directory of our database and a number ( format <name>-<number>-<option> )
   *  we return the immediate sub-directory name that has the specified number.
   *
   * @param path directory path where to search
   * @param idToFind <number> part of the format to find.
   * @return directory name that has the given ID, null if not found
   *
   static protected String getDirectoryNameFromID( String path, int idToFind ) {

      File list[] = new File(path).listFiles();
      if(list==null) return null;

      for( int i=0; i<list.length; i++ ) {
             if( !list[i].isDirectory() )
                 continue;

             if( getDirectoryID( list[i].getName() )==idToFind )
                 return list[i].getName();
      }

      return null; // not found
   }
*/
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To load an image given its name. We don't check for any name format, we just try
   *  to load the image.
   *
   * @param the path to the image
   * @return the loaded image...
   */
    public Image loadImage( String path ) {
       Image im;
       MediaTracker tracker = new MediaTracker(new Label());

         im = resourceLocator.getLibraryImage( path );
         tracker.addImage(im,0);

         try{
              tracker.waitForID(0);
         }
         catch(InterruptedException e) {
              e.printStackTrace();
         }

       return im;
   }

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

  /** To load all the images of a directory. The image name must follow the format :
   *  <name>-<number>.<ext> where ext can be anything ("jpg", "gif", "png", etc.).
   *
   *  Important : the returned array can have null fields if non-image files were found
   *              in the specified directory.
   *
   * @param path the path to the images
   * @return the loaded images... null if there are none
   *
    static public Image[] loadImages( String path ) {
    	return loadImages( new File(path) );
    }
*/
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To load all the images of a directory. The image name must follow the format :
   *  <name>-<number>.<ext> where ext can be anything ("jpg", "gif", "png", etc. ).
   *
   *  Important : the returned array can have null fields if non-image files were found
   *              in the specified directory.
   *
   * @param path the path to the images
   * @return the loaded images... null if there are none
   *
    static public Image[] loadImages( File path ) {
      File list[] = path.listFiles();
      
      if(list==null)
         return null;

      Image im[] = new Image[list.length];
      MediaTracker tracker = new MediaTracker(new Label());
      Toolkit tk = Toolkit.getDefaultToolkit();

         for( int i=0; i<list.length; i++) {

            if( list[i].isDirectory() )
                continue;

            int id = getImageID( list[i].getName() );

            if(id<0) {
               if(DEBUG_MODE) System.out.println( "Warning: Bad Image Name Format ! ( "+list[i]+" )");
               continue;
            }
            
            try{
               im[id] = tk.getImage( path.getPath()+File.separator+list[i].getName() );
            }catch(Exception e) {
              // Exception during getImage()
              // this image will be ignored
               im[id] = null;
               e.printStackTrace();
               continue;
            }
            
            tracker.addImage(im[id],i);
         }

         try{
             tracker.waitForAll();
         }catch(InterruptedException e) { e.printStackTrace(); }
          
       return im;
    }
*/
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To load an image and transform it into a ARGB buffered image.
   *
   * @param the path to the image
   * @return the loaded buffered image...
   */
    public BufferedImage loadBufferedImage( String path ) {
    	return loadBufferedImage( path, BufferedImage.TYPE_INT_ARGB );
    }

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

  /** To load an image and transform it into a buffered image of the specified type.
   *
   * @param the path to the image
   * @return the loaded buffered image...
   */
    public BufferedImage loadBufferedImage( String path, int imageType ) {
     // We load the image.
        Image im = loadImage( path );
        
        if(im==null)
           return null;

     // We transform this image into a buffered image
        BufferedImage bufIm = new BufferedImage( im.getWidth(null), im.getHeight(null), imageType );
        Graphics2D offBf = bufIm.createGraphics();
        offBf.drawImage( im,0,0,null );

      return bufIm;
   }

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

  /** To load all the images of a directory. The image name must follow the format :
   *  <name>-<number>.<ext> where ext is either "jpg" or "gif". The returned array has
   *  no holes.
   *
   * @param path the path to the images
   * @return the loaded images transformed into buffered images...
   *
    static public BufferedImage[] loadBufferedImages( String path ) {
        return loadBufferedImages( new File(path) );
    }
*/
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To load all the images of a directory. The image name must follow the format :
   *  <name>-<number>.<ext> where ext  can be anything ("jpg", "gif", "png", etc...).
   *  The returned array has no holes. The <number> must start at 0.
   *
   *  Important : the returned array can have null fields if the image numbers have jump.
   *
   * @param path the path to the images
   * @return the loaded images transformed into buffered images... a 0 length array if
   *         there are no images. We never return null.
   *
    static public BufferedImage[] loadBufferedImages( File path ) {
     // We load all the images.
        Image im[] = loadImages( path );
        
        if(im==null)
           return new BufferedImage[0];

     // We count how many images we have...
        int imageCount=0;

        for( int c=0; c<im.length; c++ )
           if( im[c]!=null ) imageCount=c+1; // we keep the max

        if(imageCount==0)
           return new BufferedImage[0];

     // We transform these images into buffered images
        BufferedImage bufIm[] = new BufferedImage[imageCount];
      
        for( int c=0; c<im.length; c++ )
           if( im[c]!=null ) {
       	       bufIm[c] = new BufferedImage( im[c].getWidth(null),
                                             im[c].getHeight(null), BufferedImage.TYPE_INT_ARGB );

               Graphics2D offBf = bufIm[c].createGraphics();

⌨️ 快捷键说明

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