Esempio n. 1
0
public class Images {
  private static final Image[] images = new Image[ImageType.values().length];

  public static void loadImageResources() throws IOException {
    final ClassLoader cl = Images.class.getClassLoader();
    for (ImageType imageType : ImageType.values()) {
      images[imageType.ordinal()] = readFromImageDir(imageType.getFileName());
    }
  }

  public static Image get(ImageType imageType) {
    assertNotNull(imageType, "iamgeType");
    final Image image = images[imageType.ordinal()];
    assertNotNull(image, imageType.toString());

    return image;
  }

  private static void assertNotNull(Object o, String msg) {
    if (o == null) {
      throw new AssertionError("object was null: " + msg);
    }
  }

  private static Image readFromImageDir(String fileName) {
    final String relativePath = "image/" + fileName;
    try {
      InputStream imageStream = ResourceFinder.getResourceStream(relativePath);
      final Image img = ImageIO.read(imageStream);
      return img;
    } catch (Exception e) {
      throw new IllegalStateException("couldn't find image: " + relativePath);
    }
  }
}
Esempio n. 2
0
 public static void loadImageResources() throws IOException {
   final ClassLoader cl = Images.class.getClassLoader();
   for (ImageType imageType : ImageType.values()) {
     images[imageType.ordinal()] = readFromImageDir(imageType.getFileName());
   }
 }