Bladeren bron

Built in behavior to load external resources.

I expect this to not work with mixed separators and if loading still
fails the program inexplicably halts. That is bad. Will fix.
Ludwig Tietze 7 jaren geleden
bovenliggende
commit
41eade9526
1 gewijzigde bestanden met toevoegingen van 9 en 4 verwijderingen
  1. 9 4
      src/ui/view/Util.java

+ 9 - 4
src/ui/view/Util.java

@@ -11,6 +11,7 @@ import java.io.InputStream;
 import javax.imageio.ImageIO;
 
 public class Util {
+	
 	/**
 	 * 30x30 pixel FileNotFound Icon, which can be shown without using I/O operations
 	 */
@@ -59,17 +60,21 @@ public class Util {
 	}
 	
 	static InputStream loadStream(Object origin, String url){
-
 		InputStream o=origin.getClass().getResourceAsStream(url);
 		if(o!=null)return o;
 		else{
-			System.out.println("Loading of resource \""+url+"\" as stream failed.\nWill attempt to load as File.");
+			System.out.println("Loading of \""+url+"\" as local resource failed.\nWill attempt to load as File.");
 			boolean rootSymbol=false;	//Whether url starts with a / or \
-			switch(url.charAt(0)){		//So we can make sure to consttruct res/path correctly.
+			switch(url.charAt(0)){		//So we can make sure to construct res/path correctly.
 			case '/':case '\\':rootSymbol=true;
 			}
 			try {
-				return new FileInputStream("res"+(rootSymbol?"":"/")+url);
+				//I am checking, whether the requested File is an internal resource.
+				//If it is, res/url has to be used, otherwise url.
+				//Mixed separators should cause problems. Will fix later.
+				File f=new File(url);
+				if(!f.exists()||!f.getAbsolutePath().equals(url))url="res"+(rootSymbol?"":"/")+url;
+				return new FileInputStream(url);
 			} catch (FileNotFoundException e1) {
 				e1.printStackTrace();
 			}