ImageImport.java 4.7 KB

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