ImageImport.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package util;
  2. import java.awt.Image;
  3. import java.awt.image.BufferedImage;
  4. import java.awt.image.ColorModel;
  5. import java.awt.image.WritableRaster;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.util.HashMap;
  12. import javax.imageio.ImageIO;
  13. /**
  14. *
  15. * @author TU-Darmstadt BP Gruppe 7 WS17/18 Centralized resource loading methods
  16. * for improved performance and easier troubleshooting.
  17. */
  18. public class ImageImport {
  19. /*
  20. * Save nothing: Reload every image whenever requested. Save external: Only save
  21. * images that were found as external file. Save raw: Save all non-scaled
  22. * images. Save everything: self-explanatory.
  23. */
  24. private static HashMap<String, Image> imgStorage;
  25. private static ImageImport xmp = new ImageImport();// Used to load resources from the JAR.
  26. static {
  27. imgStorage = new HashMap<String, Image>();
  28. }
  29. /**
  30. * The rawest function replacing the old method without any default parameters.
  31. * Currently not used in HOLEG at all(aside form calls from the convenience
  32. * methods).
  33. *
  34. * @param url Path to the image to be loaded.
  35. * @param w Width the loaded image should be scaled to.
  36. * @param h Height the loaded image should be scaled to.
  37. * @param hints Hints for the scaling algorithm to be used. Same as the
  38. * parameter in the getScaledInstance function of Image.
  39. * @return A loaded and scaled image from the requested path.
  40. */
  41. static Image loadImage(String url, int w, int h, int hints) {
  42. String key = url + "?" + w + "?" + h + "?" + hints;
  43. if (imgStorage.containsKey(key))
  44. return (imgStorage.get(key));
  45. else {
  46. Image img = loadImage(url).getScaledInstance(w, h, hints);
  47. imgStorage.put(key, img);
  48. return img;
  49. }
  50. }
  51. /**
  52. * Loads an image from the given path and scales it using the smooth algorithm.
  53. *
  54. * @param url Path to the image to be loaded.
  55. * @param w Width the loaded image should be scaled to.
  56. * @param h Height the loaded image should be scaled to.
  57. * @return A loaded and (smoothly) scaled image from the requested path.
  58. */
  59. public static Image loadImage(String url, int w, int h) {
  60. return loadImage(url, w, h, Image.SCALE_SMOOTH);
  61. }
  62. /**
  63. * Loads an image from the given path and scales it using the smooth algorithm.
  64. *
  65. * @param url Path to the image to be loaded.
  66. * @param w Width the loaded image should be scaled to.
  67. * @param h Height the loaded image should be scaled to.
  68. * @return An image loaded from the requested path.
  69. */
  70. public static Image loadImage(String url) {
  71. if (imgStorage.containsKey(url))
  72. return imgStorage.get(url);
  73. else {
  74. Image img;
  75. try {
  76. img = ImageIO.read(loadStream(url));
  77. } catch (IOException e) {
  78. System.err.println(e);
  79. return null;
  80. }
  81. imgStorage.put(url, img);
  82. return img;
  83. }
  84. }
  85. /**
  86. * Loads any resource with a given path, regardless of whether it is inside the
  87. * jar or an external resource. Every loadImage() function uses this as a basis.
  88. *
  89. * @param url The path (and file name) of the requested resource.
  90. * @return An InputStream from the requested resource.
  91. * @throws FileNotFoundException
  92. */
  93. public static InputStream loadStream(String url) throws FileNotFoundException {
  94. InputStream o = xmp.getClass().getResourceAsStream(url);
  95. if (o != null)
  96. return o;
  97. else {
  98. boolean rootSymbol = false; // Whether url starts with a / or \
  99. switch (url.charAt(0)) { // So we can make sure to construct res/path correctly.
  100. case '/':
  101. case '\\':
  102. rootSymbol = true;
  103. }
  104. File f = new File(url);
  105. if (!f.exists()) {
  106. url = "res" + (rootSymbol ? "" : "/") + url;
  107. }
  108. f = new File(url);// Possible bug with duplicate names.
  109. if (!f.exists()) {
  110. url = "res/Images/image_not_found.png";
  111. }
  112. return new FileInputStream(url);
  113. }
  114. }
  115. // Nobody needs an instance of this. I do, because I use it to load images from
  116. // inside the JAR.
  117. private ImageImport() {
  118. }
  119. }