/* @(#)ImageTools.java 1.0 12 October 2004 */ import java.awt.*; import javax.swing.*; import java.io.*; /** *

Class ImageTools is a special purpose class for * reading images from a directory.

*/ public class ImageTools { /** Prevent instantiation. */ private ImageTools() { } /** The filter to pick out jpg, gif, and png files. */ public static final FilenameFilter imageFilter = new FilenameFilter() { public boolean accept(File directory, String name) { if (name == null) return false; return name.startsWith(".jpg", name.length() - 4) || name.startsWith(".JPG", name.length() - 4) || name.startsWith(".gif", name.length() - 4) || name.startsWith(".GIF", name.length() - 4) || name.startsWith(".png", name.length() - 4) || name.startsWith(".PNG", name.length() - 4); } }; /** *

The method that reads the image files in the given directory.

* *

If no image files are available, returns an empty image array.

*/ public static Image[] readImageFiles(String directory) { String[] files = readImageFileNames(directory); int length = files.length; Image[] images = new Image[length]; for (int i = 0; i < length; i++) images[i] = new ImageIcon(directory + "/" + files[i]).getImage(); return images; } /** *

The method that reads the image file names in the given directory.

* *

If no image files are available, returns an empty string array.

*/ public static String[] readImageFileNames(String directory) { if (directory == null) return new String[0]; File imagesDirectory = new File(directory); String[] files = imagesDirectory.list(imageFilter); if (files == null) return new String[0]; return files; } }