|
@@ -12,6 +12,12 @@ import java.util.HashMap;
|
|
|
import javax.imageio.ImageIO;
|
|
|
import javax.swing.JLabel;
|
|
|
|
|
|
+
|
|
|
+ *
|
|
|
+ * @author TU-Darmstadt BP Gruppe 7 WS17/18
|
|
|
+ * Centralized resource loading methods
|
|
|
+ * for improved performance and easier troubleshooting.
|
|
|
+ */
|
|
|
public class Util {
|
|
|
|
|
|
|
|
@@ -28,6 +34,7 @@ public class Util {
|
|
|
private static final byte SAVE_NOTHING=0, SAVE_RAW=2, SAVE_EVERYTHING=4,
|
|
|
SAVE_MODE=SAVE_EVERYTHING;
|
|
|
private static HashMap<String, Image> imgStorage;
|
|
|
+ private static Util xmp=new Util();
|
|
|
|
|
|
static{
|
|
|
|
|
@@ -51,30 +58,54 @@ public class Util {
|
|
|
if(SAVE_MODE!=SAVE_NOTHING)imgStorage=new HashMap<String, Image>();
|
|
|
}
|
|
|
|
|
|
- static Image loadImage(Object origin, String url, int w, int h, int scale){
|
|
|
+
|
|
|
+ * The rawest function replacing the old method without any default parameters.
|
|
|
+ * Currently not used in HOLEG at all(aside form calls from the convenience methods).
|
|
|
+ * @param url Path to the image to be loaded.
|
|
|
+ * @param w Width the loaded image should be scaled to.
|
|
|
+ * @param h Height the loaded image should be scaled to.
|
|
|
+ * @param hints Hints for the scaling algorithm to be used.
|
|
|
+ * Same as the parameter in the getScaledInstance function of Image.
|
|
|
+ * @return A loaded and scaled image from the requested path.
|
|
|
+ */
|
|
|
+ static Image loadImage(String url, int w, int h, int hints){
|
|
|
if(SAVE_MODE==SAVE_EVERYTHING){
|
|
|
- String key=url+"?"+w+"?"+h+"?"+scale;
|
|
|
+ String key=url+"?"+w+"?"+h+"?"+hints;
|
|
|
if(imgStorage.containsKey(key))return(imgStorage.get(key));
|
|
|
else{
|
|
|
- Image img=loadImage(origin,url).getScaledInstance(w, h, scale);
|
|
|
+ Image img=loadImage(url).getScaledInstance(w, h, hints);
|
|
|
imgStorage.put(key, img);
|
|
|
return img;
|
|
|
}
|
|
|
}
|
|
|
- return loadImage(origin,url).getScaledInstance(w, h, scale);
|
|
|
+ return loadImage(url).getScaledInstance(w, h, hints);
|
|
|
}
|
|
|
|
|
|
- static Image loadImage(Object origin, String url, int w, int h){
|
|
|
- return loadImage(origin,url,w,h, Image.SCALE_SMOOTH);
|
|
|
+
|
|
|
+ * Loads an image from the given path and scales it using the smooth algorithm.
|
|
|
+ * @param url Path to the image to be loaded.
|
|
|
+ * @param w Width the loaded image should be scaled to.
|
|
|
+ * @param h Height the loaded image should be scaled to.
|
|
|
+ * @return A loaded and (smoothly) scaled image from the requested path.
|
|
|
+ */
|
|
|
+ static Image loadImage(String url, int w, int h){
|
|
|
+ return loadImage(url,w,h, Image.SCALE_SMOOTH);
|
|
|
}
|
|
|
|
|
|
- static Image loadImage(Object origin, String url){
|
|
|
+
|
|
|
+ * Loads an image from the given path and scales it using the smooth algorithm.
|
|
|
+ * @param url Path to the image to be loaded.
|
|
|
+ * @param w Width the loaded image should be scaled to.
|
|
|
+ * @param h Height the loaded image should be scaled to.
|
|
|
+ * @return An image loaded from the requested path.
|
|
|
+ */
|
|
|
+ static Image loadImage(String url){
|
|
|
if(SAVE_MODE!=SAVE_NOTHING){
|
|
|
if(imgStorage.containsKey(url))return imgStorage.get(url);
|
|
|
else{
|
|
|
Image img;
|
|
|
try {
|
|
|
- img= ImageIO.read(loadStream(origin, url));
|
|
|
+ img= ImageIO.read(loadStream(url));
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
return defaultImage;
|
|
@@ -84,15 +115,22 @@ public class Util {
|
|
|
}
|
|
|
}
|
|
|
try {
|
|
|
- return ImageIO.read(loadStream(origin, url));
|
|
|
+ return ImageIO.read(loadStream(url));
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
return defaultImage;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- static InputStream loadStream(Object origin, String url){
|
|
|
- InputStream o=origin.getClass().getResourceAsStream(url);
|
|
|
+
|
|
|
+ * Loads any resource with a given path,
|
|
|
+ * regardless of whether it is inside the jar or an external resource.
|
|
|
+ * Every loadImage() function uses this as a basis.
|
|
|
+ * @param url The path (and file name) of the requested resource.
|
|
|
+ * @return An InputStream from the requested resource.
|
|
|
+ */
|
|
|
+ static InputStream loadStream(String url){
|
|
|
+ InputStream o=xmp.getClass().getResourceAsStream(url);
|
|
|
if(o!=null)return o;
|
|
|
else{
|
|
|
boolean rootSymbol=false;
|
|
@@ -109,5 +147,8 @@ public class Util {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Util(){}
|
|
|
}
|