Import.java 3.9 KB

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