Browse Source

Overhauled resource loading method, making it even less redundant.

Ludwig Tietze 7 years ago
parent
commit
c10f620747
1 changed files with 18 additions and 15 deletions
  1. 18 15
      src/ui/view/Util.java

+ 18 - 15
src/ui/view/Util.java

@@ -1,34 +1,37 @@
 package ui.view;
 
 import java.awt.Image;
-import java.awt.Toolkit;
 import java.io.File;
+import java.io.IOException;
 
-import javax.swing.ImageIcon;
+import javax.imageio.ImageIO;
 
 public class Util {
 	
-	static Image loadImage(Object origin, String url, int w, int h, int scale){//note: this may have to be a different class.
-		return loadImage(origin,url)
-				.getScaledInstance(w, h, scale);
+	static Image loadImage(Object origin, String url, int w, int h, int scale){
+		System.out.println("Scaled call: "+url);
+		return loadImage(origin,url).getScaledInstance(w, h, scale);
 	}
-	static Image loadImage(Object origin, String url, int w, int h){//note: this may have to be a different class.
+	static Image loadImage(Object origin, String url, int w, int h){
 		return loadImage(origin,url,w,h, Image.SCALE_SMOOTH);
 	}
-	static Image loadImage(Object origin, String url){//note: this may have to be a different class.
-		System.out.println(url);
-		try{
-			return new ImageIcon(origin.getClass().getResource(url)).getImage();
-		}catch(Exception e){
-			return new ImageIcon(Toolkit.getDefaultToolkit().createImage("res"+url)).getImage();
+	static Image loadImage(Object origin, String url){
+		try {
+			return ImageIO.read(loadFile(origin, url));
+		} catch (IOException e) {
+			e.printStackTrace();
+			return null;
 		}
 	}
-	//TODO: This is a temporary solution. Fix.
-	static File loadFile(Object origin, String url){//note: this may have to be a different class.
+	static File loadFile(Object origin, String url){
 		try{
 			return new File(origin.getClass().getResource(url).getFile());
 		}catch(Exception e){
-			return new File("res"+url);
+			boolean rootSymbol=false;//Whether url starts with a / or \
+			switch(url.charAt(0)){
+			case '/':case '\\':rootSymbol=true;
+			}
+			return new File("res"+(rootSymbol?"":"/")+url);
 		}
 	}
 }