This site requires JavaScript, please enable it in your browser!
Greenfoot back
Commentator
Commentator wrote ...

2021/1/23

Loading external images

Commentator Commentator

2021/1/23

#
Is there any way of loading images, not from the ./images folder? (as folder structures in the image folder don't work when exporting to a jar file
MrCohen MrCohen

2021/1/23

#
A few things ... 1. I wouldn't suggest aiming for a .jar file, at least not if you're using the latest version of Greenfoot. It's just ... not ideal. 2. I can't speak to .jar files particularly, but I know that a lot of file opening issues come from the symbols. Different OS' use different slashes. Here is some code that helps ensure that the right slashes are used:
// Thanks to MKYong at https://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/ for the code that I based this on
class OSValidator {
    private static String OS;

    public static String getOSSymbol (){
        if (!isWindows()){
            return "/";
        }
        return "\\";
    }

    public static boolean isMac() {
        OS = System.getProperty("os.name").toLowerCase();
        //System.out.println(OS);

        if (isWindows()) {
            return false;
        } else  {
            return true;
        }
    }
    public static boolean isWindows() {
        return (OS.indexOf("win") >= 0);
    }
}
danpost danpost

2021/1/23

#
@MrCohen, with the code provided, isMac MUST always be called first else OS will be null and a NullPointerException will ensue. Line 13 should be the continuation after "public static String " on line 3. The isMac method can then be simplified to:
public boolean isMac()
{
    return ! isWindows();
}
MrCohen MrCohen

2021/1/23

#
Sorry I hastily chopped two parts together. Thanks for the correction. I'm curious if this will solve OP's problem
You need to login to post a reply.