Import.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 final HashMap<String, Image> imgStorage;
  24. static {
  25. imgStorage = new HashMap<>();
  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. * @return An image loaded from the requested path.
  65. */
  66. public static Image loadImage(String url) {
  67. if (imgStorage.containsKey(url))
  68. return imgStorage.get(url);
  69. else {
  70. Image img;
  71. try {
  72. img = ImageIO.read(loadStream(url));
  73. } catch (IOException e) {
  74. return new BufferedImage(0, 0,
  75. BufferedImage.TYPE_INT_RGB);
  76. }
  77. imgStorage.put(url, img);
  78. return img;
  79. }
  80. }
  81. /**
  82. * Loads any resource with a given path, regardless of whether it is inside the
  83. * jar or an external resource. Every loadImage() function uses this as a basis.
  84. *
  85. * @param url The path (and file name) of the requested resource.
  86. * @return An InputStream from the requested resource.
  87. * @throws FileNotFoundException if file cannot be found
  88. */
  89. public static InputStream loadStream(String url) throws FileNotFoundException {
  90. InputStream o = InputStream.class.getResourceAsStream(url);
  91. if (o != null)
  92. return o;
  93. else {
  94. boolean rootSymbol = switch (url.charAt(0)) { // So we can make sure to construct res/path correctly.
  95. case '/', '\\' -> true;
  96. default -> false; // Whether url starts with a / or \
  97. };
  98. File f = new File(url);
  99. if (!f.exists()) {
  100. url = "res" + (rootSymbol ? "" : "/") + url;
  101. }
  102. f = new File(url);// Possible bug with duplicate names.
  103. if (!f.exists()) {
  104. url = "res" + ImagePreference.Canvas.ImageNotFound;
  105. }
  106. return new FileInputStream(url);
  107. }
  108. }
  109. private Import() {
  110. }
  111. }