|
@@ -3,11 +3,15 @@ package ui.view;
|
|
import java.awt.Image;
|
|
import java.awt.Image;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
public class Util {
|
|
public class Util {
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 30x30 pixel FileNotFound Icon, which can be shown without using I/O operations
|
|
* 30x30 pixel FileNotFound Icon, which can be shown without using I/O operations
|
|
*/
|
|
*/
|
|
@@ -47,7 +51,7 @@ public class Util {
|
|
|
|
|
|
static Image loadImage(Object origin, String url){
|
|
static Image loadImage(Object origin, String url){
|
|
try {
|
|
try {
|
|
- return ImageIO.read(loadFile(origin, url));
|
|
|
|
|
|
+ return ImageIO.read(loadStream(origin, url));
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
System.err.println("Warning: '" + url + "' loading failed!");
|
|
System.err.println("Warning: '" + url + "' loading failed!");
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
@@ -55,15 +59,26 @@ public class Util {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- static File loadFile(Object origin, String url){
|
|
|
|
- try{
|
|
|
|
- return new File(origin.getClass().getResource(url).getFile());
|
|
|
|
- }catch(Exception e){
|
|
|
|
- boolean rootSymbol=false;//Whether url starts with a / or \
|
|
|
|
- switch(url.charAt(0)){
|
|
|
|
|
|
+ static InputStream loadStream(Object origin, String url){
|
|
|
|
+ InputStream o=origin.getClass().getResourceAsStream(url);
|
|
|
|
+ if(o!=null)return o;
|
|
|
|
+ else{
|
|
|
|
+ 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 construct res/path correctly.
|
|
case '/':case '\\':rootSymbol=true;
|
|
case '/':case '\\':rootSymbol=true;
|
|
}
|
|
}
|
|
- return new File("res"+(rootSymbol?"":"/")+url);
|
|
|
|
|
|
+ try {
|
|
|
|
+ //I am checking, whether the requested File is an internal resource.
|
|
|
|
+ //If it is, res/url has to be used, otherwise url.
|
|
|
|
+ //Mixed separators might 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();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|