Import.java 3.8 KB

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