Util.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Util {
  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 Util xmp=new Util();//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. * @param w Width the loaded image should be scaled to.
  91. * @param h Height the loaded image should be scaled to.
  92. * @return An image loaded from the requested path.
  93. */
  94. public static Image loadImage(String url){
  95. if(SAVE_MODE!=SAVE_NOTHING){
  96. if(imgStorage.containsKey(url))return imgStorage.get(url);
  97. else{
  98. Image img;
  99. try {
  100. img= ImageIO.read(loadStream(url));
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. return defaultImage;
  104. }
  105. imgStorage.put(url, img);
  106. return img;
  107. }
  108. }
  109. try {
  110. return ImageIO.read(loadStream(url));
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. return defaultImage;
  114. }
  115. }
  116. /**
  117. * Loads any resource with a given path,
  118. * regardless of whether it is inside the jar or an external resource.
  119. * Every loadImage() function uses this as a basis.
  120. * @param url The path (and file name) of the requested resource.
  121. * @return An InputStream from the requested resource.
  122. */
  123. public static InputStream loadStream(String url){
  124. InputStream o=xmp.getClass().getResourceAsStream(url);
  125. if(o!=null)return o;
  126. else{
  127. boolean rootSymbol=false; //Whether url starts with a / or \
  128. switch(url.charAt(0)){ //So we can make sure to construct res/path correctly.
  129. case '/':case '\\':rootSymbol=true;
  130. }
  131. try {
  132. //Mixed separators might cause problems. Will fix later.
  133. File f=new File(url);//Possible bug with duplicate names.
  134. if(!f.exists())url="res"+(rootSymbol?"":"/")+url;
  135. return new FileInputStream(url);
  136. } catch (FileNotFoundException e1) {
  137. e1.printStackTrace();
  138. }
  139. return null;
  140. }
  141. }
  142. //Nobody needs an instance of this. I do, because I use it to load images from inside the JAR.
  143. private Util(){}
  144. }