ImageImport.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package utility;
  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 javax.swing.JLabel;
  12. /**
  13. *
  14. * @author TU-Darmstadt BP Gruppe 7 WS17/18
  15. * Centralized resource loading methods
  16. * for improved performance and easier troubleshooting.
  17. */
  18. public class ImageImport {
  19. /**
  20. * 30x30 pixel FileNotFound Icon, which can be shown without using I/O operations
  21. * a red cross.
  22. */
  23. static private BufferedImage defaultImage;
  24. /*
  25. * Save nothing: Reload every image whenever requested.
  26. * Save external: Only save images that were found as external file.
  27. * Save raw: Save all non-scaled images.
  28. * Save everything: self-explanatory.
  29. */
  30. private static final byte SAVE_NOTHING=0, /*SAVE_EXTERNAL=1,*/ SAVE_RAW=2, SAVE_EVERYTHING=4,
  31. SAVE_MODE=SAVE_EVERYTHING;
  32. private static HashMap<String, Image> imgStorage;
  33. private static ImageImport xmp=new ImageImport();//Used to load resources from the JAR.
  34. static{
  35. /*
  36. * creates the default Image
  37. */
  38. defaultImage = new BufferedImage(30,30,BufferedImage.TYPE_BYTE_INDEXED);
  39. for(int x = 0; x < 30; x++)
  40. for(int y = 0; y < 30; y++){
  41. if(x == 29 || y == 29)
  42. //Border Bottom/Left -> Light Grey
  43. (defaultImage).setRGB(x, y, 13158600);
  44. else if(x == 0 || y == 0)
  45. //Border Top/Right -> Dark Grey
  46. (defaultImage).setRGB(x, y, 6316128);
  47. else if((x == y || x == 31 - y) && 6 < x && x < 25)
  48. //Red Cross in the middle
  49. (defaultImage).setRGB(x, y, 13107200);
  50. else //all other Pixels are white
  51. (defaultImage).setRGB(x, y, 16777215);
  52. }
  53. if(SAVE_MODE!=SAVE_NOTHING)imgStorage=new HashMap<String, Image>();
  54. }
  55. /**
  56. * The rawest function replacing the old method without any default parameters.
  57. * Currently not used in HOLEG at all(aside form calls from the convenience methods).
  58. * @param url Path to the image to be loaded.
  59. * @param w Width the loaded image should be scaled to.
  60. * @param h Height the loaded image should be scaled to.
  61. * @param hints Hints for the scaling algorithm to be used.
  62. * Same as the parameter in the getScaledInstance function of Image.
  63. * @return A loaded and scaled image from the requested path.
  64. */
  65. static Image loadImage(String url, int w, int h, int hints){
  66. if(SAVE_MODE==SAVE_EVERYTHING){
  67. String key=url+"?"+w+"?"+h+"?"+hints;
  68. if(imgStorage.containsKey(key))return(imgStorage.get(key));
  69. else{
  70. Image img=loadImage(url).getScaledInstance(w, h, hints);
  71. imgStorage.put(key, img);
  72. return img;
  73. }
  74. }
  75. return loadImage(url).getScaledInstance(w, h, hints);
  76. }
  77. /**
  78. * Loads an image from the given path and scales it using the smooth algorithm.
  79. * @param url Path to the image to be loaded.
  80. * @param w Width the loaded image should be scaled to.
  81. * @param h Height the loaded image should be scaled to.
  82. * @return A loaded and (smoothly) scaled image from the requested path.
  83. */
  84. public static Image loadImage(String url, int w, int h){
  85. return loadImage(url,w,h, Image.SCALE_SMOOTH);
  86. }
  87. /**
  88. * Loads an image from the given path and scales it using the smooth algorithm.
  89. * @param url Path to the image to be loaded.
  90. * @return An image loaded from the requested path.
  91. */
  92. public static Image loadImage(String url){
  93. boolean save = SAVE_MODE!=SAVE_NOTHING;
  94. if (save && imgStorage.containsKey(url))
  95. return imgStorage.get(url);
  96. else {
  97. Image img;
  98. try {
  99. try (InputStream stream = loadStream(url)) {
  100. if (stream == null)
  101. return defaultImage;
  102. img = ImageIO.read(stream);
  103. }
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. return defaultImage;
  107. }
  108. if (img == null)
  109. return defaultImage;
  110. if (save)
  111. imgStorage.put(url, img);
  112. return img;
  113. }
  114. }
  115. /**
  116. * Loads any resource with a given path,
  117. * regardless of whether it is inside the jar or an external resource.
  118. * Every loadImage() function uses this as a basis.
  119. * @param url The path (and file name) of the requested resource.
  120. * @return An InputStream from the requested resource.
  121. */
  122. public static InputStream loadStream(String url){
  123. InputStream o=xmp.getClass().getResourceAsStream(url);
  124. if(o!=null)return o;
  125. else{
  126. boolean rootSymbol=false; //Whether url starts with a / or \
  127. switch(url.charAt(0)){ //So we can make sure to construct res/path correctly.
  128. case '/':case '\\':rootSymbol=true;
  129. }
  130. try {
  131. //Mixed separators might cause problems. Will fix later.
  132. File f=new File(url);//Possible bug with duplicate names.
  133. if(!f.exists())url="res"+(rootSymbol?"":"/")+url;
  134. return new FileInputStream(url);
  135. } catch (FileNotFoundException e1) {
  136. e1.printStackTrace();
  137. }
  138. return null;
  139. }
  140. }
  141. //Nobody needs an instance of this. I do, because I use it to load images from inside the JAR.
  142. private ImageImport(){}
  143. }