📄 imagelibrary.java
字号:
offBf.drawImage( im[c],0,0,null);
}
return bufIm;
}
*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To find an image with a given index ( filename format is <name>-<index>.<jpg | gif | *>)
* in a given file list.
*
* @param imageFiles list of files to investigate...
* @param imageIndex index of the image to find
* @param imageType if you don't know what to put here set it to BufferedImage.TYPE_INT_ARGB
* it's the type of the image we are going to create.
* @return null if the image is not found
*/
public BufferedImage findImageIn( String imageNames[], int imageIndex, int imageType ){
if(imageNames==null) return null;
for( int l=0; l<imageNames.length; l++ ) {
int id = getImageID( imageNames[l] );
if(id<0) continue;
// Is it the image we wanted ?
if( id == imageIndex )
return loadBufferedImage( imageNames[l], imageType );
}
return null; // not found
}
/*------------------------------------------------------------------------------------*/
/** Represents a directory of the Image Library. It can contains other ImageLibDir and/or
* images. There are no GETTERS/SETTERS because this class is for internal use only. Data
* can be accessed directly.
*/
class ImageLibDir {
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Directory Option : no option
*/
public final static byte OPT_NONE = 0;
/** Directory Option : JIT = just in time, directory content loaded only when needed.
*/
public final static byte OPT_JIT = 1;
/** Directory Option : EXC = exclude, directory content loaded only by the user, one subdir
* only at the same time. Other unused directories are automatically unloaded.
*/
public final static byte OPT_EXC = 2;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Directory File Path.
*/
protected String dir;
/** Directory Option (OPT_NONE, OPT_JIT, OPT_EXC).
*/
protected byte dirOption;
/** Parent directory. If null it means this dir is root.
*/
protected ImageLibDir parentDir;
/** Eventual Sub-directories
*/
protected ImageLibDir childDirs[];
/** Eventual images of this directory.
*/
protected BufferedImage images[];
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Constructor.
* @param dir path representing the directory of this ImageLibDir object.
* @param parentDir parent directory, null if this is the root directory
* @exception ImageLibraryException if ignoreBadFormatEntries=false and the image db has bad entries.
*/
public ImageLibDir( String dir, ImageLibDir parentDir )
throws ImageLibraryException {
this.dir = dir;
this.parentDir = parentDir;
String options = dir.toLowerCase();
if(options.endsWith("/"))
options = options.substring(0,options.length()-1);
else if( options.endsWith(File.separator) )
options = options.substring(0,options.length()-File.separator.length());
// 1 - We get the eventual option of this directory
if( options.endsWith("-jit") )
dirOption = OPT_JIT;
else if( options.endsWith("-exc") )
dirOption = OPT_EXC;
else if( parentDir!=null && parentDir.dirOption==OPT_JIT )
dirOption = OPT_JIT; // this option is always propagated to sub-directories.
else
dirOption = OPT_NONE;
// 2 - Is there a child structure to create ?
String childDirNames[] = resourceLocator.listDirectories(dir);
int nbChildDirs = 0;
if( childDirNames==null )
childDirNames = new String[0];
for( int c=0; c<childDirNames.length; c++ ) {
int id = ImageLibrary.getDirectoryID( childDirNames[c] );
if( id<0 ) {
if( ignoreBadFormatEntries ) {
if(DEBUG_MODE)
System.out.println("ERROR: ImageLibrary directory has a bad format : "+childDirNames[c]);
}
else
throw new ImageLibraryException("ImageLibrary directory has a bad format : "+childDirNames[c]);
}
else if( id>=nbChildDirs )
nbChildDirs = id+1;
}
childDirs = new ImageLibDir[nbChildDirs];
// 3 - is there images in this directory
int nbImages = 0;
String imageNames[] = resourceLocator.listFiles(dir);
if( imageNames==null )
imageNames = new String[0];
for( int c=0; c<imageNames.length; c++ ) {
int id = ImageLibrary.getImageID( imageNames[c] );
if( id<0 ) {
if( ignoreBadFormatEntries )
if(DEBUG_MODE)
System.out.println("ERROR: ImageLibrary image name has a bad format : "+imageNames[c]);
else
throw new ImageLibraryException("ImageLibrary image name has a bad format : "+imageNames[c]);
}
else if( id>=nbImages )
nbImages = id+1;
}
images = new BufferedImage[nbImages];
// 4 - We can now create our child structure. Images will be loaded later.
for( int c=0; c<childDirNames.length; c++ ) {
// We recursively construct the child structure
int id = ImageLibrary.getDirectoryID( childDirNames[c] );
if( id<0 ) continue;
childDirs[id] = new ImageLibDir( childDirNames[c], this );
// 5 - Diego: i'll need to check what's loaded to understand better if it loads
// all the tilemap images.
if( ImageLibrary.DEBUG_MODE )
System.out.println("\t ChildDir " + c + " dir="+dir );
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Called to load the image library the first time. We propagate the call to
* our childs. We stop the load of childs if we encounter a JIT or EXC directory.
* @return the number of images loaded
*/
public int initImageLoad() {
int loaded=0;
// No image to load for these options
if( dirOption==OPT_JIT || dirOption==OPT_EXC )
return 0;
if(images.length!=0) {
String imageNames[] = resourceLocator.listFiles(dir);
for( int c=0; c<imageNames.length; c++ ) {
int id = ImageLibrary.getImageID( imageNames[c] );
if(id<0)
continue;
images[id] = loadBufferedImage( imageNames[c] );
if(images[id]!=null)
loaded++;
}
}
// Child directories init propagation
for( int i=0; i<childDirs.length; i++ )
loaded += childDirs[i].initImageLoad();
return loaded;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get an image of our directory. We don't perform any checks.
* @param index index of the image.
* @return the image that has the wanted index
*/
public BufferedImage getImage( int index ) {
return images[index];
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To load an image of our directory.
* @param index index of the image.
* @return the image we just loaded.
*/
public BufferedImage loadImage( int index ) {
if(images.length==0)
return null;
if(images[index]==null)
images[index] = findImageIn( resourceLocator.listFiles(dir), index, BufferedImage.TYPE_INT_ARGB );
return images[index];
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To load all the image of our directory, and if suddirs=true of all our
* sub-directories (unless we find JIT or EXC options on their directories !).
* @param loadSubDirsAlso if true we propagate an initImageLoad() to sub-directories.
* @return the number of images loaded
*/
public int loadAllImages( boolean loadSubDirsAlso ) {
// We load valid images
int loaded = 0;
if(images.length!=0) {
String imageNames[] = resourceLocator.listFiles(dir);
for( int c=0; c<imageNames.length; c++ ) {
int id = ImageLibrary.getImageID( imageNames[c] );
if(id<0)
continue;
images[id] = loadBufferedImage( imageNames[c] );
if(images[id]!=null)
loaded++;
}
}
// Child directories load propagation ( by init() call )
if(loadSubDirsAlso)
for( int i=0; i<childDirs.length; i++ )
loaded += childDirs[i].initImageLoad();
return loaded;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To unload an image of our directory.
* @param index index of the image.
*/
public void unloadImage( int index ) {
if(images[index]!=null) {
images[index].flush();
images[index] = null;
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To unload all the images of our directory. If unloadSubDirsAlso=true we also
* unload sub directories.
* @param unloadSubDirsAlso if true we propagate the unloadAll call to sub-directories.
* @return the numbers of images unloaded from the library.
*/
public int unloadAllImages( boolean unloadSubDirsAlso ) {
int unloaded = 0;
for( int i=0; i<images.length; i++ )
if(images[i]!=null) {
images[i].flush();
images[i] = null;
unloaded++;
}
if(unloadSubDirsAlso)
for( int i=0; i<childDirs.length; i++ )
unloaded += childDirs[i].unloadAllImages(true);
return unloaded;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -